Skip to content

Commit

Permalink
fix(schema): Preserve resolvers on ___NODE fields (gatsbyjs#12806)
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanprobst authored and freiksenet committed Mar 25, 2019
1 parent a9b6d68 commit e8816bf
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 8 deletions.
42 changes: 42 additions & 0 deletions packages/gatsby/src/schema/infer/__tests__/merge-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ const nodes = [
},
],
},
{
id: `id2`,
internal: { type: `LinkTest` },
link___NODE: `id1`,
links___NODE: [`id1`],
},
]

describe(`merges explicit and inferred type definitions`, () => {
Expand Down Expand Up @@ -276,4 +282,40 @@ describe(`merges explicit and inferred type definitions`, () => {
// Date resolvers
expect(fields.explicitDate.resolve).toBeUndefined()
})

it(`preserves foreign-key resolvers on ___NODE fields when noDefaultResolvers: false`, async () => {
const typeDefs = `
type LinkTest implements Node {
link: Test!
links: [Test!]!
}
`
store.dispatch({ type: `CREATE_TYPES`, payload: typeDefs })

await build({})
const { schema } = store.getState()
const { link, links } = schema.getType(`LinkTest`).getFields()
expect(link.type.toString()).toBe(`Test!`)
expect(links.type.toString()).toBe(`[Test!]!`)
expect(link.resolve).toBeDefined()
expect(links.resolve).toBeDefined()
})

it(`ignores foreign-key resolvers on ___NODE fields when noDefaultResolvers: true`, async () => {
const typeDefs = `
type LinkTest implements Node @infer(noDefaultResolvers: true) {
link: Test!
links: [Test!]!
}
`
store.dispatch({ type: `CREATE_TYPES`, payload: typeDefs })

await build({})
const { schema } = store.getState()
const { link, links } = schema.getType(`LinkTest`).getFields()
expect(link.type.toString()).toBe(`Test!`)
expect(links.type.toString()).toBe(`[Test!]!`)
expect(link.resolve).toBeUndefined()
expect(links.resolve).toBeUndefined()
})
})
18 changes: 10 additions & 8 deletions packages/gatsby/src/schema/infer/add-inferred-fields.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,25 +102,27 @@ const addInferredFieldsImpl = ({
let lists = 0
let namedFieldType = fieldType
while (namedFieldType.ofType) {
namedFieldType = namedFieldType.ofType
if (namedFieldType instanceof GraphQLList) {
lists++
}
namedFieldType = namedFieldType.ofType
}

if (arrays === lists) {
const namedInferredTypeName =
typeof namedInferredType === `string`
? namedInferredType
: namedInferredType.getTypeName()

if (arrays === lists && namedFieldType.name === namedInferredTypeName) {
if (
namedFieldType instanceof GraphQLObjectType &&
typeof namedInferredType !== `string` &&
namedFieldType.name === namedInferredType.getTypeName()
namedInferredType instanceof ObjectTypeComposer
) {
const fieldTypeComposer = typeComposer.getFieldTC(key)
const inferredFields = namedInferredType.getFields()
fieldTypeComposer.addFields(inferredFields)
} else if (
addDefaultResolvers &&
namedFieldType.name === namedInferredType
) {
}
if (addDefaultResolvers) {
let field = typeComposer.getField(key)
if (!field.type) {
field = {
Expand Down

0 comments on commit e8816bf

Please sign in to comment.