gatsbyjs / gatsby-graphql-toolkit

79 stars 20 forks source link

Fails to execute LIST_ query with NoPagination #65

Open Ruben-deBruijn opened 2 years ago

Ruben-deBruijn commented 2 years ago

Following error message is provided:

Error: Failed to execute query LIST_WATCHES.
    at Object.paginate (/Users/rubendebruijn/schaap_citroen_v2/node_modules/gatsby-graphql-source-toolkit/src/source-nodes/fetch-nodes/paginate.ts:60:13)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at fetchNodeList (/Users/rubendebruijn/schaap_citroen_v2/node_modules/gatsby-graphql-source-toolkit/src/source-nodes/fetch-nodes/fetch-lists.ts:61:20)
    at Object.fetchAllNodes
(/Users/rubendebruijn/schaap_citroen_v2/node_modules/gatsby-graphql-source-toolkit/src/source-nodes/fetch-nodes/fetch-lists.ts:36:24)
    at Object.createNodes
(/Users/rubendebruijn/schaap_citroen_v2/node_modules/gatsby-graphql-source-toolkit/src/source-nodes/node-actions/create-nodes.ts:13:20)
    at async Promise.all (index 0)
    at sourceAllNodes (/Users/rubendebruijn/schaap_citroen_v2/node_modules/gatsby-graphql-source-toolkit/src/source-nodes/source-all-nodes.ts:20:3)
    at Object.exports.sourceNodes (/Users/rubendebruijn/schaap_citroen_v2/gatsby-node.js:99:3)
    at runAPI (/Users/rubendebruijn/schaap_citroen_v2/node_modules/gatsby/src/utils/api-runner-node.js:462:16)

As described in the guide, i implemented a PaginationAdapter that doesn't require variables. There is no pagination implemented in the BE i connect with.

Still, i would love to automaticly generate the fragments with generateDefaultFragments This issue doesn't seem to appear when i write a fragment manually and generate it with readOrGenerateDefaultFragments

Approach:

const NoPagination = {
  name: "NoPagination",
  expectedVariableNames: [],
  start() {
      return {
          variables: {},
          hasNextPage: true,
      };
  },
  next() {
      return {
          variables: {},
          hasNextPage: false,
      };
  },
  concat(result) {
      return result;
  },
  getItems(pageOrResult) {
      return pageOrResult;
  },
}

async function createSourcingConfig(gatsbyApi) {
  const execute = createDefaultQueryExecutor(`https://mywatchapi.com/graphql`);
  const schema = await loadSchema(execute)

const gatsbyNodeTypes = [
  {
    remoteTypeName: `Watch`,
    queries: `
      query LIST_WATCHES {
        watches { 
            ..._WatchId_ 
         }
      }
      fragment _WatchId_ on Watch {
          __typename 
          id 
      }
    `,
  },
]

  const fragments = await generateDefaultFragments({ schema, gatsbyNodeTypes })

  const documents = compileNodeQueries({
    schema,
    gatsbyNodeTypes,
    customFragments: fragments,
  })

  return {
    gatsbyApi,
    schema,
    execute,
    gatsbyTypePrefix: `Example`,
    gatsbyNodeDefs: buildNodeDefinitions({ gatsbyNodeTypes, documents }),
    paginationAdapters: [NoPagination],
  }
}

Expected

sourceAllNodes will run succesfuly, while the fragment generated automaticly

Actual

sourceAllNodes runs, but no nodes were created and error is thrown