gatsby: data is "undefined" by graphql gatsby
I am using graphql for querying. query is not running in my code. but on graphql it is working fine. Heres my files package.json
{
"gatsby": "latest",
"gatsby-link": "latest",
"gatsby-source-filesystem": "latest",
"gatsby-transformer-remark": "^1.7.40",
"react-helmet": "5.0.3",
}
gatsby-node.js
exports.createLayouts = async ({
boundActionCreators
}) => {
const {
createLayout
} = boundActionCreators;
const token = await sakeAuth.credentials.getToken();
const options = {
headers: {
Authorization: `Bearer ${token.accessToken}`
}
};
exports.onCreateNode = ({ node, getNode, boundActionCreators }) => {
const { createNodeField } = boundActionCreators;
// console.log(node.internal.type);
if (node.internal.type === `FaqsJson`) {
const slug = createFilePath({ node, getNode, basePath: `pages` });
console.log("==========>", slug);
createNodeField({
node,
name: `slug`,
value: slug
});
}
};
exports.createPages = ({
graphql,
boundActionCreators
}) => {
return new Promise(resolve => {
graphql(`
{
allFaqsJson {
edges {
node {
fields {
slug
}
}
}
}
}
`).then(result => {
const { createPage } = boundActionCreators;
console.log("result=================>", result.data.allFaqsJson);
result.data.allFaqsJson.edges.forEach(({ node }) => {
console.log(`create page for=========> ${node.fields.slug}`);
createPage({
path: node.fields.slug,
component: path.resolve(`./src/templates/faqs.tsx`),
context: {
// Data passed to context is available in page queries as GraphQL variables.
slug: node.fields.slug
}
});
});
resolve();
});
});
};
in src/templates/faqs.tsx
import { css, merge } from "glamor";
import * as React from "react";
import { InjectedIntlProps, injectIntl } from "react-intl";
interface OwnProps extends React.HTMLProps<HTMLDivElement> {
styles?: React.CSSProperties;
}
interface State { }
type Props = OwnProps & InjectedIntlProps;
class Faqs extends React.Component<Props, State> {
constructor(props: Props) {
super(props);
}
public componentDidMount() {
const { data } = this.props;
console.log("===>>>>", data); // Here data is undefined :-(
}
public render() {
const { data } = this.props;
console.log("template", data); // Here data is undefined :-(
return (
<div>
faqs
</div>
);
}
}
export const query = graphql`
query fqlQuery($slug: String!) {
allFile {
edges{
node{
childFaqsJson{
title
}
}
}
}
}
`;
export default injectIntl(Faqs);
in .cache/json faqs.json file is not creating. Can any body help in it?
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Comments: 16 (4 by maintainers)
Sorry for being late to the party but the issue you are encountering has to do with the term we call page queries.
When your component is inside the pages or layout directory we execute your data and inject it inside the component. Whenever you move your component to another directory like components you’re including your component inside a page and you have to use graphql fragments or StaticQuery.
Fragments are useful when you want to include your query inside the page query so only one graphql query is done to the server and is most likely what you want.
Sometimes you are dependant on props and you need to do an extra graphql query which you can do by using staticQuery which makes it possible to do graphql queries from inside your component.
OKAY so I figured out what my problem was and I don’t know if this will apply to @Neha-takhi-cipl
My query worked when I put it in a component in the pages folder. I then fed the props into the component I needed the images in. Originally, it was just a regular component.
@wardpeet, will be good put that in page-query docs. https://www.gatsbyjs.org/docs/page-query/
I’m just going to post this for other silly mistakes like mine. Mine query shows undefined because it was in the components folder. When I did the query in Layouts or Pages it worked perfectly.
Note: It also worked perfectly in GraphiQL
I am also experiencing the same situation. The data from the graphql query is showing as undefined. Did anyone else have a solution to this?
When I query the data using the GraphiQl tab, the data pulls correctly.