Skip to content

Commit

Permalink
fix(gatsby): merge resolveType when merging abstract types (#31710)
Browse files Browse the repository at this point in the history
  • Loading branch information
vladar committed Jun 1, 2021
1 parent 30d212d commit 2dde956
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 0 deletions.
64 changes: 64 additions & 0 deletions packages/gatsby/src/schema/__tests__/build-schema.js
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
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())
}
}

0 comments on commit 2dde956

Please sign in to comment.