Skip to content

Instantly share code, notes, and snippets.

@vladar
Last active July 24, 2020 10:14
Show Gist options
  • Save vladar/a2a882bac9b899c958f5e1feee4a311a to your computer and use it in GitHub Desktop.
Save vladar/a2a882bac9b899c958f5e1feee4a311a to your computer and use it in GitHub Desktop.
function singularRootFieldName(queryFields, type) {
return Object.keys(queryFields).find(fieldName => queryFields[fieldName].type === type)
}
function pluralRootFieldName(queryFields, type) {
const expectedType = `[${type}!]!`
return Object.keys(queryFields).find(fieldName => String(queryFields[fieldName].type) === expectedType)
}
//////
const query = schema.getType(`Query`)
const queryFields = query.getFields()
const gatsbyNodeTypes = possibleTypes.map((type) => ({
remoteTypeName: type.name,
remoteIdFields: ['__typename', 'id'],
queries: `
query LIST_${pluralRootFieldName(queryFields, type)} { ${pluralRootFieldName(queryFields, type)} }(first: $limit, skip: $offset) }
query NODE_${singularRootFieldName(queryFields, type)} { ${singularRootFieldName(queryFields, type)}(where: $where) }`,
nodeQueryVariables: ({ id }) => ({ where: { id } }),
}))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment