Skip to content

Instantly share code, notes, and snippets.

@vladar
Last active Jul 24, 2020
Embed
What would you like to do?
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