From abaddfb62691d173e1976651099d8228c4715894 Mon Sep 17 00:00:00 2001 From: Jonathan Steele Date: Fri, 24 Jul 2020 12:37:22 +0100 Subject: [PATCH] fix: Use correct root query fields (#96) * fix: Build queries for node sourcing from GraphCMS project schema fields * chore(deps): Remove pluralize * refactor: Remove unnecessary query variable --- gatsby-source-graphcms/gatsby-node.js | 23 +++++++++++++++++------ gatsby-source-graphcms/package.json | 3 +-- yarn.lock | 5 ----- 3 files changed, 18 insertions(+), 13 deletions(-) diff --git a/gatsby-source-graphcms/gatsby-node.js b/gatsby-source-graphcms/gatsby-node.js index 3596bbb..e572859 100644 --- a/gatsby-source-graphcms/gatsby-node.js +++ b/gatsby-source-graphcms/gatsby-node.js @@ -10,7 +10,6 @@ const { } = require('gatsby-graphql-source-toolkit') const { createRemoteFileNode } = require('gatsby-source-filesystem') const fetch = require('node-fetch') -const pluralize = require('pluralize') exports.onPreBootstrap = ({ reporter }, pluginOptions) => { if (!pluginOptions || !pluginOptions.endpoint) @@ -33,18 +32,30 @@ const createSourcingConfig = async (gatsbyApi, { endpoint, token }) => { const schema = await loadSchema(execute) const nodeInterface = schema.getType('Node') + const query = schema.getType('Query') + const queryFields = query.getFields() const possibleTypes = schema.getPossibleTypes(nodeInterface) + const singularRootFieldName = (type) => + Object.keys(queryFields).find( + (fieldName) => queryFields[fieldName].type === type + ) + + const pluralRootFieldName = (type) => + Object.keys(queryFields).find( + (fieldName) => String(queryFields[fieldName].type) === `[${type.name}!]!` + ) + const gatsbyNodeTypes = possibleTypes.map((type) => ({ remoteTypeName: type.name, remoteIdFields: ['__typename', 'id'], queries: ` - query LIST_${pluralize(type.name).toUpperCase()} { ${pluralize( - type.name.toLowerCase() + query LIST_${pluralRootFieldName(type)} { ${pluralRootFieldName( + type )}(first: $limit, skip: $offset) } - query NODE_${type.name.toUpperCase()}($where: ${ - type.name - }WhereUniqueInput!) { ${type.name.toLowerCase()}(where: $where) }`, + query NODE_${singularRootFieldName(type)} { ${singularRootFieldName( + type + )}(where: $where) }`, nodeQueryVariables: ({ id }) => ({ where: { id } }), })) diff --git a/gatsby-source-graphcms/package.json b/gatsby-source-graphcms/package.json index 29252c8..3cd557b 100644 --- a/gatsby-source-graphcms/package.json +++ b/gatsby-source-graphcms/package.json @@ -15,7 +15,6 @@ "dependencies": { "gatsby-graphql-source-toolkit": "0.2.2", "gatsby-source-filesystem": "2.3.18", - "node-fetch": "2.6.0", - "pluralize": "8.0.0" + "node-fetch": "2.6.0" } } diff --git a/yarn.lock b/yarn.lock index 91f418f..82356e7 100644 --- a/yarn.lock +++ b/yarn.lock @@ -9888,11 +9888,6 @@ pkg-dir@^4.2.0: dependencies: find-up "^4.0.0" -pluralize@8.0.0: - version "8.0.0" - resolved "https://registry.yarnpkg.com/pluralize/-/pluralize-8.0.0.tgz#1a6fa16a38d12a1901e0320fa017051c539ce3b1" - integrity sha512-Nc3IT5yHzflTfbjgqWcCPpo7DaKy4FnpB0l/zCAW0Tc7jxAiuqSxHasntB3D7887LSrA93kDJ9IXovxJYxyLCA== - pngjs@^3.0.0, pngjs@^3.3.3: version "3.4.0" resolved "https://registry.yarnpkg.com/pngjs/-/pngjs-3.4.0.tgz#99ca7d725965fb655814eaf65f38f12bbdbf555f"