Gatsby-node error: "Must Provide Source" error being thrown

Viewed 401

I'm creating dynamic pages in Gatsby, pulling data from Fauna. I've got a query in gastby-node that is throwing an error "Must provide Source", but the query works in GraphiQL. I've include gatsby-node.js below.

exports.createPages = async function({actions, graphql}){
    const {data} = await graphql`
    query {
        fauna {
            allCompanies {
                data {
                    slug
                }
            }
        }
    }
    `

data.fauna.allCompanies.data.forEach(edge => {
    const slug = edge.slug
    actions.createPages({
        path: slug,
        component: require.resolve("./src/components/products.js"),
        context:{
            slug
        },
    })
})
}
1 Answers

Today I encountered the same error and after some time figured it out. A stupid mistake tbh.

graphql is a function that needs to be called with the query as the parameter ( graphql(`...`) ). I was mistaking it for graphql-tag which I used in apollo as gql`...`

This should work

exports.createPages = async function ({ actions, graphql }) {
  /* 
     you have to pass the template literal query
     in the function graphql(`...`)

  */
  const { data } = await graphql(`
    query {
      fauna {
        allCompanies {
          data {
            slug
          }
        }
      }
    }
  `)

  data.fauna.allCompanies.data.forEach(edge => {
    const slug = edge.slug
    actions.createPages({
      path: slug,
      component: require.resolve("./src/components/products.js"),
      context: {
        slug,
      },
    })
  })
}

Hope this helps !

Related