Skip to content

Commit

Permalink
fix: Use correct root query fields (#96)
Browse files Browse the repository at this point in the history
* fix: Build queries for node sourcing from GraphCMS project schema fields

* chore(deps): Remove pluralize

* refactor: Remove unnecessary query variable
  • Loading branch information
Jonathan Steele authored Jul 24, 2020
1 parent 9ba86a7 commit abaddfb
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 13 deletions.
23 changes: 17 additions & 6 deletions gatsby-source-graphcms/gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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 } }),
}))

Expand Down
3 changes: 1 addition & 2 deletions gatsby-source-graphcms/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
5 changes: 0 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit abaddfb

Please sign in to comment.