Skip to content

Commit

Permalink
fix(gatsby): Extend fields when merging types (#18500)
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanprobst authored and GatsbyJS Bot committed Oct 14, 2019
1 parent ee70f9d commit 302aa26
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 1 deletion.
27 changes: 27 additions & 0 deletions packages/gatsby/src/schema/__tests__/build-schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -814,6 +814,33 @@ describe(`Build schema`, () => {
)
})

it(`extends fieldconfigs when merging types`, async () => {
createTypes(
buildObjectType({
name: `Mdx`,
interfaces: [`Node`],
fields: {
body: {
type: `String`,
resolve: () => `Mdx!`,
},
},
})
)
createTypes(`
type Mdx implements Node {
body: String!
}
`)

const schema = await buildSchema()
const fields = schema.getType(`Mdx`).getFields()

expect(fields.body.type.toString()).toBe(`String!`)
expect(typeof fields.body.resolve).toBe(`function`)
expect(fields.body.resolve()).toBe(`Mdx!`)
})

it(`displays error message for reserved Node interface`, () => {
const typeDefs = [
`interface Node { foo: Boolean }`,
Expand Down
33 changes: 32 additions & 1 deletion packages/gatsby/src/schema/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ const {
parse,
GraphQLNonNull,
GraphQLList,
GraphQLObjectType,
GraphQLInterfaceType,
} = require(`graphql`)
const {
ObjectTypeComposer,
Expand All @@ -15,6 +17,7 @@ const {
InputTypeComposer,
ScalarTypeComposer,
EnumTypeComposer,
defineFieldMapToConfig,
} = require(`graphql-compose`)

const apiRunner = require(`../utils/api-runner-node`)
Expand Down Expand Up @@ -315,11 +318,30 @@ const mergeTypes = ({
plugin.name === `default-site-plugin` ||
plugin.name === typeOwner
) {
typeComposer.merge(type)
if (type instanceof ObjectTypeComposer) {
mergeFields({ typeComposer, fields: type.getFields() })
type.getInterfaces().forEach(iface => typeComposer.addInterface(iface))
} else if (type instanceof InterfaceTypeComposer) {
mergeFields({ typeComposer, fields: type.getFields() })
} else if (type instanceof GraphQLObjectType) {
mergeFields({
typeComposer,
fields: defineFieldMapToConfig(type.getFields()),
})
type.getInterfaces().forEach(iface => typeComposer.addInterface(iface))
} else if (type instanceof GraphQLInterfaceType) {
mergeFields({
typeComposer,
fields: defineFieldMapToConfig(type.getFields()),
})
}

if (isNamedTypeComposer(type)) {
typeComposer.extendExtensions(type.getExtensions())
}

addExtensions({ schemaComposer, typeComposer, plugin, createdFrom })

return true
} else {
report.warn(
Expand Down Expand Up @@ -1160,3 +1182,12 @@ const checkQueryableInterfaces = ({ schemaComposer }) => {
)
}
}

const mergeFields = ({ typeComposer, fields }) =>
Object.entries(fields).forEach(([fieldName, fieldConfig]) => {
if (typeComposer.hasField(fieldName)) {
typeComposer.extendField(fieldName, fieldConfig)
} else {
typeComposer.setField(fieldName, fieldConfig)
}
})

0 comments on commit 302aa26

Please sign in to comment.