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: Allow themes to modify GraphQL types #16928

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
159 changes: 159 additions & 0 deletions packages/gatsby/src/schema/__tests__/build-schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -808,6 +808,165 @@ describe(`Build schema`, () => {
)
})

it(`does not merge plugin-defined type with type defined by theme`, async () => {
createTypes(
`type PluginDefined implements Node { foo: Int, baz: PluginDefinedNested }
type PluginDefinedNested { foo: Int }`,
{
name: `gatsby-theme-random`,
}
)
createTypes(
`type PluginDefined implements Node { bar: Int, qux: PluginDefinedNested }
type PluginDefinedNested { bar: Int }`,
{
name: `some-other-gatsby-plugin`,
}
)
const schema = await buildSchema()
const nestedFields = schema.getType(`PluginDefinedNested`).getFields()
const fields = schema.getType(`PluginDefined`).getFields()
expect(Object.keys(nestedFields)).toEqual([`foo`])
expect(Object.keys(fields)).toEqual([
`foo`,
`baz`,
`id`,
`parent`,
`children`,
`internal`,
])
expect(report.warn).toHaveBeenCalledWith(
`Plugin \`some-other-gatsby-plugin\` tried to define the GraphQL type ` +
`\`PluginDefinedNested\`, which has already been defined by the plugin ` +
`\`gatsby-theme-random\`.`
)
expect(report.warn).toHaveBeenCalledWith(
`Plugin \`some-other-gatsby-plugin\` tried to define the GraphQL type ` +
`\`PluginDefined\`, which has already been defined by the plugin ` +
`\`gatsby-theme-random\`.`
)
})

it(`merges theme-defined type with plugin-defined type`, async () => {
createTypes(
`type PluginDefined implements Node @infer { foo: Int, baz: PluginDefinedNested }
type PluginDefinedNested { foo: Int }`,
{
name: `some-gatsby-plugin`,
}
)
createTypes(
`type PluginDefined implements Node @dontInfer { bar: Int, qux: PluginDefinedNested }
type PluginDefinedNested { bar: Int }`,
{
name: `gatsby-theme-random`,
}
)
const schema = await buildSchema()
const PluginDefinedNested = schema.getType(`PluginDefinedNested`)
const nestedFields = PluginDefinedNested.getFields()
const PluginDefined = schema.getType(`PluginDefined`)
const fields = PluginDefined.getFields()
expect(Object.keys(nestedFields)).toEqual([`foo`, `bar`])
expect(Object.keys(fields)).toEqual([
`foo`,
`baz`,
`bar`,
`qux`,
`id`,
`parent`,
`children`,
`internal`,
])
expect(PluginDefined._gqcExtensions).toEqual(
expect.objectContaining({
createdFrom: `sdl`,
plugin: `gatsby-theme-random`,
infer: false,
})
)
})

it(`does not merge theme-defined type with type owned by other theme`, async () => {
createTypes(
`type PluginDefined implements Node { foo: Int, baz: PluginDefinedNested }
type PluginDefinedNested { foo: Int }`,
{
name: `gatsby-theme-first`,
}
)
createTypes(
`type PluginDefined implements Node { bar: Int, qux: PluginDefinedNested }
type PluginDefinedNested { bar: Int }`,
{
name: `gatsby-theme-second`,
}
)
const schema = await buildSchema()
const nestedFields = schema.getType(`PluginDefinedNested`).getFields()
const fields = schema.getType(`PluginDefined`).getFields()
expect(Object.keys(nestedFields)).toEqual([`foo`])
expect(Object.keys(fields)).toEqual([
`foo`,
`baz`,
`id`,
`parent`,
`children`,
`internal`,
])
expect(report.warn).toHaveBeenCalledWith(
`Plugin \`gatsby-theme-second\` tried to define the GraphQL type ` +
`\`PluginDefinedNested\`, which has already been defined by the plugin ` +
`\`gatsby-theme-first\`.`
)
expect(report.warn).toHaveBeenCalledWith(
`Plugin \`gatsby-theme-second\` tried to define the GraphQL type ` +
`\`PluginDefined\`, which has already been defined by the plugin ` +
`\`gatsby-theme-first\`.`
)
})

it(`prefers user-defined type over theme-defined type`, async () => {
createTypes(
`type PluginDefined implements Node @infer { bar: String, baz: PluginDefinedNested }
type PluginDefinedNested { bar: String }`,
{
name: `default-site-plugin`,
}
)
createTypes(
`type PluginDefined implements Node @dontInfer { bar: Int, qux: PluginDefinedNested }
type PluginDefinedNested { bar: Int }`,
{
name: `gatsby-theme-random`,
}
)
const schema = await buildSchema()
const PluginDefinedNested = schema.getType(`PluginDefinedNested`)
const nestedFields = PluginDefinedNested.getFields()
const PluginDefined = schema.getType(`PluginDefined`)
const fields = PluginDefined.getFields()
expect(Object.keys(nestedFields)).toEqual([`bar`])
expect(nestedFields.bar.type.toString()).toBe(`String`)
expect(Object.keys(fields)).toEqual([
`bar`,
`qux`,
`baz`,
`id`,
`parent`,
`children`,
`internal`,
])
expect(fields.bar.type.toString()).toBe(`String`)
expect(PluginDefined._gqcExtensions).toEqual(
expect.objectContaining({
createdFrom: `sdl`,
plugin: `default-site-plugin`,
infer: true,
})
)
})

it(`displays error message for reserved Node interface`, () => {
const typeDefs = [
`interface Node { foo: Boolean }`,
Expand Down
11 changes: 8 additions & 3 deletions packages/gatsby/src/schema/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,14 @@ module.exports.build = async ({ parentSpan }) => {
const typeConflictReporter = new TypeConflictReporter()

// Ensure that user-defined types are processed last
const sortedTypes = types.sort(
type => type.plugin && type.plugin.name === `default-site-plugin`
)
const sortedTypes = [
wardpeet marked this conversation as resolved.
Show resolved Hide resolved
...types.filter(
type => type.plugin && type.plugin.name !== `default-site-plugin`
),
...types.filter(
type => !type.plugin || type.plugin.name === `default-site-plugin`
),
]

const schemaComposer = createSchemaComposer({ fieldExtensions })
const schema = await buildSchema({
Expand Down
7 changes: 6 additions & 1 deletion packages/gatsby/src/schema/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,8 @@ const addTypes = ({ schemaComposer, types, parentSpan }) => {
})
}

const GATSBY_THEME = /^(?:@[\w-]+\/)?gatsby-theme-/

const mergeTypes = ({
schemaComposer,
typeComposer,
Expand All @@ -309,11 +311,14 @@ const mergeTypes = ({
parentSpan,
}) => {
// Only allow user or plugin owning the type to extend already existing type.
// Themes are also allowed to modify plugin-owned types, but not types already
// modified by another plugin.
const typeOwner = typeComposer.getExtension(`plugin`)
if (
!plugin ||
plugin.name === typeOwner ||
plugin.name === `default-site-plugin` ||
plugin.name === typeOwner
(plugin.name.match(GATSBY_THEME) && !typeOwner.match(GATSBY_THEME))
) {
typeComposer.merge(type)
if (isNamedTypeComposer(type)) {
Expand Down