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

fix(gatsby): merge resolveType when merging abstract types #31710

Merged
merged 1 commit into from
Jun 1, 2021
Merged
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
64 changes: 64 additions & 0 deletions packages/gatsby/src/schema/__tests__/build-schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -979,6 +979,70 @@ describe(`Build schema`, () => {
expect(interfaces).toEqual([`Foo`, `Bar`])
})

it(`merges resolveType for abstract types (Type Builder)`, async () => {
createTypes(
[
`interface Foo { foo: String }`,
`
type Fizz { id: ID! }
type Buzz { id: ID! }
union FizzBuzz = Fizz | Buzz
`,
buildInterfaceType({
name: `Foo`,
fields: { id: `ID!` },
resolveType: source => source.expectedType,
}),
buildUnionType({
name: `FizzBuzz`,
resolveType: source => (source.isFizz ? `Fizz` : `Buzz`),
}),
],
{
name: `default-site-plugin`,
}
)
const schema = await buildSchema()
const Foo = schema.getType(`Foo`)
expect(Foo.resolveType({ expectedType: `Bar` })).toEqual(`Bar`)

const FizzBuzz = schema.getType(`FizzBuzz`)
expect(FizzBuzz.resolveType({ isFizz: true })).toEqual(`Fizz`)
expect(FizzBuzz.resolveType({ isFizz: false })).toEqual(`Buzz`)
})

it(`merges resolveType for abstract types (graphql-js)`, async () => {
createTypes(
[
`interface Foo { foo: String }`,
`
type Fizz { id: ID! }
type Buzz { id: ID! }
union FizzBuzz = Fizz | Buzz
`,
new GraphQLInterfaceType({
name: `Foo`,
fields: { foo: { type: GraphQLString } },
resolveType: source => source.expectedType,
}),
new GraphQLUnionType({
name: `FizzBuzz`,
resolveType: source => (source.isFizz ? `Fizz` : `Buzz`),
}),
],
{
name: `default-site-plugin`,
}
)
const schema = await buildSchema()
const Foo = schema.getType(`Foo`)
expect(Foo.resolveType({ expectedType: `Bar` })).toEqual(`Bar`)

const FizzBuzz = schema.getType(`FizzBuzz`)
expect(FizzBuzz.resolveType({ isFizz: true })).toEqual(`Fizz`)
expect(FizzBuzz.resolveType({ isFizz: false })).toEqual(`Buzz`)
})

it(`merges plugin-defined type (Type Builder) with overridable built-in type without warning`, async () => {
createTypes(
[
Expand Down
27 changes: 27 additions & 0 deletions packages/gatsby/src/schema/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const {
GraphQLList,
GraphQLObjectType,
GraphQLInterfaceType,
GraphQLUnionType,
} = require(`graphql`)
const {
ObjectTypeComposer,
Expand Down Expand Up @@ -406,6 +407,15 @@ const mergeTypes = ({
type.getInterfaces().forEach(iface => typeComposer.addInterface(iface))
}

if (
type instanceof GraphQLInterfaceType ||
type instanceof InterfaceTypeComposer ||
type instanceof GraphQLUnionType ||
type instanceof UnionTypeComposer
) {
mergeResolveType({ typeComposer, type })
}

if (isNamedTypeComposer(type)) {
typeComposer.extendExtensions(type.getExtensions())
}
Expand Down Expand Up @@ -1387,3 +1397,20 @@ const mergeFields = ({ typeComposer, fields }) =>
typeComposer.setField(fieldName, fieldConfig)
}
})

const mergeResolveType = ({ typeComposer, type }) => {
if (
(type instanceof GraphQLInterfaceType ||
type instanceof GraphQLUnionType) &&
type.resolveType
) {
typeComposer.setResolveType(type.resolveType)
}
if (
(type instanceof InterfaceTypeComposer ||
type instanceof UnionTypeComposer) &&
type.getResolveType()
) {
typeComposer.setResolveType(type.getResolveType())
}
}