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
2 changes: 1 addition & 1 deletion packages/gatsby/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@
"glob": "^7.2.0",
"got": "^11.8.2",
"graphql": "^15.7.2",
"graphql-compose": "^9.0.6",
"graphql-compose": "^9.0.7",
"graphql-playground-middleware-express": "^1.7.22",
"hasha": "^5.2.2",
"http-proxy": "^1.18.1",
Expand Down
67 changes: 67 additions & 0 deletions packages/gatsby/src/schema/__tests__/__snapshots__/print.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,24 @@ interface ITest implements Node {
}

type BarChild implements Node @childOf(types: [\\"Test\\"]) @dontInfer {
fieldWithArgsAndDescription(
\\"\\"\\"This is description\\"\\"\\"
withDefault: String = \\"Test\\"

\\"\\"\\"This is description too\\"\\"\\"
withoutDefault: String
usingDerivedType: BarChildSortInput
): String
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

all snapshot changes are additive - I expanded tests to include more cases

bar: String
}

enum BuilderEnum {
One
Two
}

scalar BuilderScalar

type Test implements Node @dontInfer {
foo: Int
}
Expand Down Expand Up @@ -187,6 +202,14 @@ input Baz {
}

type BarChild implements Node @childOf(types: [\\"Test\\"]) @dontInfer {
fieldWithArgsAndDescription(
\\"\\"\\"This is description\\"\\"\\"
withDefault: String = \\"Test\\"

\\"\\"\\"This is description too\\"\\"\\"
withoutDefault: String
usingDerivedType: BarChildSortInput
): String
bar: String
}"
`;
Expand Down Expand Up @@ -339,6 +362,13 @@ interface ITest2 implements Node {

union ThisOrThat = AnotherTest | OneMoreTest

enum SDLEnumSimple {
One
Two
}

scalar SDLScalar

type Inline {
foo: Nested
}
Expand All @@ -348,9 +378,24 @@ input Baz {
}

type BarChild implements Node @childOf(types: [\\"Test\\"]) @dontInfer {
fieldWithArgsAndDescription(
\\"\\"\\"This is description\\"\\"\\"
withDefault: String = \\"Test\\"

\\"\\"\\"This is description too\\"\\"\\"
withoutDefault: String
usingDerivedType: BarChildSortInput
): String
bar: String
}

enum BuilderEnum {
One
Two
}

scalar BuilderScalar

type Test implements Node @dontInfer {
foo: Int
}
Expand Down Expand Up @@ -532,6 +577,13 @@ interface ITest2 implements Node {

union ThisOrThat = AnotherTest | OneMoreTest

enum SDLEnumSimple {
One
Two
}

scalar SDLScalar

type Inline {
foo: Nested
}
Expand All @@ -546,9 +598,24 @@ type InlineTest implements Node & ITest @childOf(types: [\\"OneMoreTest\\"]) @do
}

type BarChild implements Node @childOf(types: [\\"Test\\"]) @dontInfer {
fieldWithArgsAndDescription(
\\"\\"\\"This is description\\"\\"\\"
withDefault: String = \\"Test\\"

\\"\\"\\"This is description too\\"\\"\\"
withoutDefault: String
usingDerivedType: BarChildSortInput
): String
bar: String
}

enum BuilderEnum {
One
Two
}

scalar BuilderScalar

type Test implements Node @dontInfer {
foo: Int
}
Expand Down
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
48 changes: 47 additions & 1 deletion packages/gatsby/src/schema/__tests__/print.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
const { build } = require(`..`)
import { buildObjectType } from "../types/type-builders"
import {
buildObjectType,
buildEnumType,
buildScalarType,
} from "../types/type-builders"
const { store } = require(`../../redux`)
const { actions } = require(`../../redux/actions/restricted`)
const { actions: publicActions } = require(`../../redux/actions/public`)
Expand All @@ -18,6 +22,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 @@ -98,6 +103,13 @@ describe(`Print type definitions`, () => {
date: Date @dateformat(formatString: "YYYY")
}
union ThisOrThat = AnotherTest | OneMoreTest

enum SDLEnumSimple {
One
Two
}

scalar SDLScalar
`)
typeDefs.push(
buildObjectType({
Expand Down Expand Up @@ -128,13 +140,42 @@ describe(`Print type definitions`, () => {
name: `BarChild`,
fields: {
id: `ID!`,
fieldWithArgsAndDescription: {
type: `String`,
args: {
withDefault: {
type: `String`,
description: `This is description`,
defaultValue: `Test`,
},
withoutDefault: {
type: `String`,
description: `This is description too`,
},
usingDerivedType: `BarChildSortInput`,
},
},
},
interfaces: [`Node`],
extensions: {
childOf: {
types: [`Test`],
},
},
}),
buildEnumType({
name: `BuilderEnum`,
values: {
One: {
value: `One`,
},
Two: {
value: `Two`,
},
},
}),
buildScalarType({
name: `BuilderScalar`,
})
)
store.dispatch({
Expand All @@ -152,6 +193,11 @@ describe(`Print type definitions`, () => {
payload: typeDefs[2],
plugin: { name: `gatsby-plugin-another-test` },
})
store.dispatch({
type: `CREATE_TYPES`,
payload: [typeDefs[3], typeDefs[4]],
plugin: { name: `gatsby-plugin-shared` },
})
})

it(`saves type definitions to default file`, async () => {
Expand Down
Loading