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: Ensure we can modify nullability of an interface field #662

Merged
merged 3 commits into from
Nov 24, 2020
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
57 changes: 43 additions & 14 deletions src/builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ import {
finalizeWrapping,
AllNexusNamedInputTypeDefs,
AllNexusNamedOutputTypeDefs,
AllNexusArgsDefs,
isNexusWrappingType,
NexusFinalWrapKind,
} from './definitions/wrapping'
import {
MissingType,
Expand Down Expand Up @@ -1184,11 +1187,21 @@ export class SchemaBuilder {
...rest,
}
if (typeof type !== 'undefined') {
const { wrapping } = unwrapGraphQLDef(config.fields[field].type)
interfaceFieldsMap[field].type = rewrapAsGraphQLType(
this.getOrBuildType(type),
wrapping
) as GraphQLOutputType
let interfaceReplacement: GraphQLOutputType
if (isNexusWrappingType(type)) {
const { wrapping, namedType } = unwrapNexusDef(type)
interfaceReplacement = rewrapAsGraphQLType(
this.getOrBuildType(namedType as any),
wrapping as NexusFinalWrapKind[]
) as GraphQLOutputType
} else {
const { wrapping } = unwrapGraphQLDef(config.fields[field].type)
interfaceReplacement = rewrapAsGraphQLType(
this.getOutputType(type),
wrapping
) as GraphQLOutputType
}
interfaceFieldsMap[field].type = interfaceReplacement
}
if (typeof args !== 'undefined') {
interfaceFieldsMap[field].args = {
Expand Down Expand Up @@ -1565,7 +1578,7 @@ export class SchemaBuilder {
},
addField: (f) => this.maybeTraverseOutputFieldType(f),
addDynamicOutputMembers: (block, wrapping) => this.addDynamicOutputMembers(block, 'walk', wrapping),
addModification: (o) => (o.type && typeof o.type !== 'string' ? this.addType(o.type) : null),
addModification: (o) => this.maybeTraverseModification(o),
warn: () => {},
})
obj.definition(definitionBlock)
Expand All @@ -1575,7 +1588,7 @@ export class SchemaBuilder {
protected walkInterfaceType(obj: NexusInterfaceTypeConfig<any>) {
const definitionBlock = new InterfaceDefinitionBlock({
typeName: obj.name,
addModification: (o) => (o.type && typeof o.type !== 'string' ? this.addType(o.type) : null),
addModification: (o) => this.maybeTraverseModification(o),
addInterfaces: (i) => {
i.forEach((j) => {
if (typeof j !== 'string') {
Expand All @@ -1591,26 +1604,42 @@ export class SchemaBuilder {
return obj
}

protected maybeTraverseModification(mod: FieldModificationDef<any, any>) {
const { type, args } = mod
if (type) {
const namedFieldType = getNexusNamedType(mod.type)
if (typeof namedFieldType !== 'string') {
this.addType(namedFieldType)
}
}
if (args) {
this.traverseArgs(args)
}
}

protected maybeTraverseOutputFieldType(type: NexusOutputFieldDef) {
const { args, type: fieldType } = type
const namedFieldType = getNexusNamedType(fieldType)
if (typeof namedFieldType !== 'string') {
this.addType(namedFieldType)
}
if (args) {
eachObj(args, (val) => {
const namedArgType = getArgNamedType(val)
if (typeof namedArgType !== 'string') {
this.addType(namedArgType)
}
})
this.traverseArgs(args)
}
}

private traverseArgs(args: Record<string, AllNexusArgsDefs>) {
eachObj(args, (val) => {
const namedArgType = getArgNamedType(val)
if (typeof namedArgType !== 'string') {
this.addType(namedArgType)
}
})
}

protected maybeTraverseInputFieldType(type: NexusInputFieldDef) {
const { type: fieldType } = type
const namedFieldType = getNexusNamedType(fieldType)

if (typeof namedFieldType !== 'string') {
this.addType(namedFieldType)
}
Expand Down
2 changes: 1 addition & 1 deletion src/definitions/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export class NexusListDef<TypeName extends NexusListableTypes> {

constructor(readonly ofNexusType: TypeName) {
/* istanbul ignore if */
if (!isNexusStruct(ofNexusType) && !isType(ofNexusType) && typeof ofNexusType !== 'string') {
if (typeof ofNexusType !== 'string' && !isNexusStruct(ofNexusType) && !isType(ofNexusType)) {
throw new Error('Cannot wrap unknown types in list(). Saw ' + ofNexusType)
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/definitions/nonNull.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export class NexusNonNullDef<TypeName extends NexusNonNullableTypes> {
private _isNexusNonNullDef: boolean = true

constructor(readonly ofNexusType: TypeName) {
if (!isNexusStruct(ofNexusType) && !isType(ofNexusType) && typeof ofNexusType !== 'string') {
if (typeof ofNexusType !== 'string' && !isNexusStruct(ofNexusType) && !isType(ofNexusType)) {
throw new Error('Cannot wrap unknown types in a nonNull(). Saw ' + ofNexusType)
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/definitions/nullable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export class NexusNullDef<TypeName extends NexusNullableTypes> {
private _isNexusNullDef: boolean = true

constructor(readonly ofNexusType: TypeName) {
if (!isNexusStruct(ofNexusType) && !isType(ofNexusType) && typeof ofNexusType !== 'string') {
if (typeof ofNexusType !== 'string' && !isNexusStruct(ofNexusType) && !isType(ofNexusType)) {
throw new Error('Cannot wrap unknown types in nullable(). Saw ' + ofNexusType)
}
}
Expand Down
49 changes: 47 additions & 2 deletions tests/modify.spec.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { graphql } from 'graphql'
import {
generateSchema,
idArg,
interfaceType,
makeSchema,
NexusGraphQLSchema,
nonNull,
objectType,
queryField,
stringArg,
nonNull,
generateSchema,
} from '../src/core'

describe('modify', () => {
Expand All @@ -29,6 +29,7 @@ describe('modify', () => {
t.field('subNode', {
type: 'Node',
})
t.string('requiredInSubtype')
},
resolveType: (o) => o.__typename,
}),
Expand All @@ -43,6 +44,9 @@ describe('modify', () => {
t.field('subNode', {
type: 'User',
})
t.modify('requiredInSubtype', {
type: nonNull('String'),
})
},
}),
objectType({
Expand Down Expand Up @@ -186,6 +190,47 @@ describe('modify', () => {
)
})

it('can modify nullability of an abstract type field inherited from an interface', async () => {
const result = await graphql(
schema,
`
{
node: __type(name: "Node") {
fields {
name
description
type {
name
}
}
}
user: __type(name: "User") {
fields {
name
description
type {
kind
ofType {
name
}
}
}
}
}
`
)

expect(
result.data?.node.fields.find((f: { name: string }) => f.name === 'requiredInSubtype').type.name
).toEqual('String')
expect(
result.data?.user.fields.find((f: { name: string }) => f.name === 'requiredInSubtype').type.kind
).toEqual('NON_NULL')
expect(
result.data?.user.fields.find((f: { name: string }) => f.name === 'requiredInSubtype').type.ofType.name
).toEqual('String')
})

describe('interfaces implementing interfaces', () => {
let schemaTypes: string
beforeAll(async () => {
Expand Down