2021年3月28日星期日

Gatsby: Cannot query field

I get this error when trying: Gatsby Develop:

error There was an error in your GraphQL query:    Cannot query field "allProductType" on type "Query".    If you don't expect "allProductType" to exist on the type "Query" it is most likely a typo.  However, if you expect "allProductType" to exist there are a couple of solutions to common problems:    - If you added a new data source and/or changed something inside gatsby-node.js/gatsby-config.js, please try a restart of your development server  - The field might be accessible in another subfield, please try your query in GraphiQL and use the GraphiQL explorer to see which fields you can query and what shape they have  - You want to optionally use your field "allProductType" and right now it is not used anywhere. Therefore Gatsby can't infer the type and add it to the GraphQL schema. A quick fix is to add at least one entry with that field ("dummy content")  

=== Relevant information ===

I have a custom gatsby source plugin that pulls JSON from an AWS lambda and adds graphql nodes (asynchronously).

=== My investigation: ===

my custom console.log messages (that show during gatsby develop) show me that the async JSON axios requests are completing after this error appears. Which leads me to think that the custom source plugin is not completing it's async requests (and populating the graphQL nodes) before attempting the createPages lifecycle method - hence the error.

=== What I've tried ===

I've tried using async await on the exports.onCreateNode (in gatsby-node.js) as well as the axios JSON fetch commands. but the error persists.

an example of my code below:

  exports.createPages = async ({ graphql, actions }) => {    const { createPage } = actions      console.log ("\n\n\n===== ===== ===== ===== BUILD PAGES ===== ===== ===== =====\n\n")      console.log("\n=== Create Product pages ===")    const allProducts = await graphql(`    {  etc...  

Can anyone help?

=== Edit ===

#1: I should also mention that if I add a settimeout 5000 to the createPages() lifecycle function, that I'm able to get the dev server working. although it throws another type of warning and will not build.

#2: the console provides this warning:

warning The gatsby-source-lambda plugin has generated no Gatsby nodes. Do you need it?  

however, these nodes are in fact visible (and queriable) in graphiQL despite saying no nodes are added. I can also see my console.logs that show that add node was being executed (later on in the gatsby develop).

#3: The template pages use exprt queries, however from what I understand, they're supposed to use static queiries (however static queries cannot accept variables) this is a whole new topic that I don't understand, but static queries may be treated differently than exported page queiries?

#4: Source code - this is a simplified version of the source code.

exports.sourceNodes = async (    { actions, createNodeId, createContentDigest, reporter },    configOptions  ) => {    console.log(`==== Fetch 'products' from AWS lambda (ota_func) ====`)    const { createNode } = actions      // Gatsby adds a configOption that's not needed for this plugin, delete it    delete configOptions.plugins      const getProductsAPIURL = `https://mind456vec.execute-api.ap-southeast-2.amazonaws.com/dev/getProducts`    const response = await axios.get(getProductsAPIURL)    const products = response.data      var nodeData = null    await products.forEach(async curProduct => {      const getAvailabilitiesAPIURL = 'https://mind456vec.execute-api.ap-southeast-2.amazonaws.com/dev/getAvailability';      const response = await axios.get(getAvailabilitiesAPIURL)        const avail = response.data.sessions      nodeData = createProductNodeData(curProduct, avail, createNodeId, createContentDigest)      const result = createNode(nodeData)    });  }      const createProductNodeData = (product, availabilities, createNodeId, createContentDigest) => {    const { productId, name } = product      product.bookingAvailabilities = availabilities        const nodeId = createNodeId(`product-${productId}`)    console.log('-- Creating graphQL node, for product: ' +  name)    const nodeContent = JSON.stringify(product)    const nodeData = Object.assign({}, product, {      id: nodeId,      parent: null,      children: [],      internal: {        type: `ProductType`,        content: nodeContent,        contentDigest: createContentDigest(product),      },    })    return nodeData  }  
https://stackoverflow.com/questions/66832009/gatsby-cannot-query-field-field-on-type-query-an-async-mystery March 27, 2021 at 09:46PM

没有评论:

发表评论