简体   繁体   中英

Gatsby-node error: “Must Provide Source” error being thrown

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
        },
    })
})
}

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 !

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM