Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(gatsby): allow referencing derived types in schema customization #34787

Merged
merged 13 commits into from
Feb 15, 2022
63 changes: 63 additions & 0 deletions packages/gatsby/src/schema/__tests__/build-schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ jest.mock(`gatsby-cli/lib/reporter`, () => {
info: jest.fn(),
warn: jest.fn(),
error: jest.fn(),
panic: jest.fn(console.error),
activityTimer: () => {
return {
start: jest.fn(),
Expand Down Expand Up @@ -1395,6 +1396,68 @@ describe(`Build schema`, () => {
}"
`)
})

it(`Can reference derived types when merging types`, async () => {
createTypes(gql`
# create initial type composer
type TypeCreatedBySourcePlugin implements Node {
id: ID!
someField: String
}
`)
createTypes([
buildInterfaceType({
name: `SharedInterface`,
fields: {
id: `ID!`,
child_items: {
type: `[SharedInterface]`,
args: {
// referencing derived type
sort: `SharedInterfaceSortInput`,
},
},
},
interfaces: [`Node`],
}),
buildObjectType({
name: `TypeCreatedBySourcePlugin`,
fields: {
id: `ID!`,
child_items: {
type: `[SharedInterface]`,
args: {
sort: `SharedInterfaceSortInput`,
},
resolve: (_, args) => [],
},
},
interfaces: [`Node`, `SharedInterface`],
}),
])

// implicit assertion is that building schema doesn't throw in the process
const schema = await buildSchema()
expect(printType(schema.getType(`TypeCreatedBySourcePlugin`)))
.toMatchInlineSnapshot(`
"type TypeCreatedBySourcePlugin implements Node & SharedInterface {
id: ID!
someField: String
child_items(sort: SharedInterfaceSortInput): [SharedInterface]
parent: Node
children: [Node!]!
internal: Internal!
}"
`)

expect(printType(schema.getType(`SharedInterfaceSortInput`)))
.toMatchInlineSnapshot(`
"input SharedInterfaceSortInput {
fields: [SharedInterfaceFieldsEnum]
order: [SortOrderEnum] = [ASC]
}"
`)
})
})

it(`allows renaming and merging nested types`, async () => {
Expand Down
30 changes: 15 additions & 15 deletions packages/gatsby/src/schema/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,21 +143,6 @@ const updateSchemaComposer = async ({
parentSpan: parentSpan,
})
activity.start()
if (!process.env.GATSBY_SKIP_WRITING_SCHEMA_TO_FILE) {
await printTypeDefinitions({
config: printConfig,
schemaComposer,
parentSpan: activity.span,
})
if (enginePrintConfig) {
// make sure to print schema that will be used when bundling graphql-engine
await printTypeDefinitions({
config: enginePrintConfig,
schemaComposer,
parentSpan: activity.span,
})
}
}
await addSetFieldsOnGraphQLNodeTypeFields({
schemaComposer,
parentSpan: activity.span,
Expand All @@ -184,6 +169,21 @@ const updateSchemaComposer = async ({
})
await addCustomResolveFunctions({ schemaComposer, parentSpan: activity.span })
attachTracingResolver({ schemaComposer, parentSpan: activity.span })
if (!process.env.GATSBY_SKIP_WRITING_SCHEMA_TO_FILE) {
await printTypeDefinitions({
config: printConfig,
schemaComposer,
parentSpan: activity.span,
})
if (enginePrintConfig) {
// make sure to print schema that will be used when bundling graphql-engine
await printTypeDefinitions({
config: enginePrintConfig,
schemaComposer,
parentSpan: activity.span,
})
}
}
pieh marked this conversation as resolved.
Show resolved Hide resolved
activity.end()
}

Expand Down