Skip to content

Unhandled Rejection (Error): Expected undefined to be a GraphQL schema

I’m trying to build the post pages imported from WordPress, I run “gatsby develop” and I head to the URL. The front page flashes up and then I get this error:

Unhandled Rejection (Error): Expected undefined to be a GraphQL schema.
invariant
C:/Users/Phil/Repositories/Zym/node_modules/graphql/jsutils/invariant.mjs:12
assertSchema
C:/Users/Phil/Repositories/Zym/node_modules/graphql/type/schema.mjs:25
validateSchema
C:/Users/Phil/Repositories/Zym/node_modules/graphql/type/validate.mjs:31
graphqlImpl
C:/Users/Phil/Repositories/Zym/node_modules/graphql/graphql.mjs:44
(anonymous function)
C:/Users/Phil/Repositories/Zym/node_modules/graphql/graphql.mjs:20
graphql
C:/Users/Phil/Repositories/Zym/node_modules/graphql/graphql.mjs:18

The query which is highlighted in my ‘PostTemplate.js’:

export const query = graphql`
  query($id: String!) {
     wordpressPost(id: { eq: $id }) {
      date
      title
      slug
      content
      categories {
        name
      }
    }    
  }
`;

I run the same query through the GraphiQL interface and it sends me data?

Really unsure as to what I’m doing wrong here, see code bellow from gatsby-node.js

exports.createPages = ({ actions, graphql }) => {
  const { createPage } = actions


      return graphql(`
        {
          allWordpressPost {
            edges {
              node {
                id
                slug
                status
              }
            }
          }
        }
      `)
    .then(result => {
      if (result.errors) {
        result.errors.forEach(e => console.error(e.toString()))
        return Promise.reject(result.errors)
      }

      const postTemplate = path.resolve(`./src/templates/PostTemplate.js`)


      const posts = result.data.allWordpressPost.edges

      _.each(posts, ({ node: post }) => {
        createPage({
          path: `/${post.slug}/`,
          component: postTemplate,
          context: {
            id: post.id,
            slug: post.slug
          },
        })
      })
   })
})

I’ve tried updating gatsby-cli -g, and uninstalled node_modules.

Answer

I’ve encountered the same error and was able to resolve it by making sure I’m importing graphql directly from gatsby:

What caused the error:

// template file
import { graphql } from 'graphql'

How to fix it:

// template file
import { graphql } from 'gatsby'