I'm completely new to WPGraphQL and I'm having some bother with building a Gatsby site from my new Wordpress endpoint. When I execute the following query to my endpoint in Postman, I get the page data back successfully:
query MyQuery {
pages {
edges {
node {
id
slug
status
}
}
}
}
However when I attempt to build the Gatsby site, I get the following:
ERROR #85923 GRAPHQL
There was an error in your GraphQL query:
Cannot query field "pages" on type "Query".
The relevant code in gatsby-node.js is:
const _ = require('lodash')
const path = require('path')
const { createFilePath } = require('gatsby-source-filesystem')
const { paginate } = require('gatsby-awesome-pagination')
const getOnlyPublished = edges =>
_.filter(edges, ({ node }) => node.status === 'publish')
exports.createPages = ({ actions, graphql }) => {
const { createPage } = actions
return graphql(`
{
pages {
edges {
node {
id
slug
status
}
}
}
}
`)
.then(result => {
if (result.errors) {
result.errors.forEach(e => console.error(e.toString()))
return Promise.reject(result.errors)
}
Gatsby config:
module.exports = {
siteMetadata: {
siteUrl: "https://www.yourdomain.tld",
title: "graphql-test",
},
plugins: [
{
resolve: "gatsby-source-wordpress",
options: {
url: "https://*********/graphql",
},
},
"gatsby-plugin-sass",
"gatsby-plugin-react-helmet",
"gatsby-plugin-sitemap",
],
};
This is a brand new test Gatsby site created with gatsby new, all plugins are the latest version.
Any help with this greatly appreciated!
It turns out that Gatsby uses a slightly different format for its graphql queries. What I should have had in gatsby-node.js was this:
return graphql(`
{
allWpPage {
nodes {
id
slug
status
}
}
}
`)