Last active
July 24, 2020 10:14
-
-
Save vladar/a2a882bac9b899c958f5e1feee4a311a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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