Skip to content

Commit 8ea063c

Browse files
committed
fix: Add GraphQLInterfaceType inside buildInfoForAllScalars
fix: Add GraphQLInterfaceType inside buildInfoForAllScalars fix: Maybe type from codegen folder
1 parent e6cb3ce commit 8ea063c

File tree

4 files changed

+618
-373
lines changed

4 files changed

+618
-373
lines changed

src/codegen/TypescriptGenerator.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {
1818
} from 'graphql'
1919

2020
import { Generator } from './Generator'
21+
2122
import { Maybe } from './types'
2223

2324
export class TypescriptGenerator extends Generator {

src/info.test.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,31 @@ test('buildInfoForAllScalars: excludes object type fields', t => {
6666
t.is(info.fieldName, 'book')
6767
})
6868

69+
test('buildInfoForAllScalars: support interfaces', t => {
70+
const schema = buildSchema(`
71+
type Query {
72+
book: IBook
73+
}
74+
75+
type Book implements IBook {
76+
title: String
77+
number: Float
78+
otherBook: IBook
79+
}
80+
81+
interface IBook {
82+
title: String
83+
number: Float
84+
otherBook: Book
85+
}
86+
`)
87+
const info = buildInfoForAllScalars('book', schema, 'query')
88+
const selections = info.fieldNodes[0].selectionSet!.selections
89+
90+
assertFields(t, selections, ['title', 'number'])
91+
t.is(info.fieldName, 'book')
92+
})
93+
6994
test('buildInfoForAllScalars: enums', t => {
7095
const schema = buildSchema(`
7196
type Query {

src/info.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ import {
1212
print,
1313
parse,
1414
validate,
15+
isObjectType,
16+
isInterfaceType,
17+
isScalarType,
1518
} from 'graphql'
1619

1720
import { Operation } from './types'
@@ -45,7 +48,7 @@ export function buildInfoForAllScalars(
4548
const namedType = getNamedType(type)
4649

4750
let selections: FieldNode[] | undefined
48-
if (namedType instanceof GraphQLObjectType) {
51+
if (isInterfaceType(namedType) || isObjectType(namedType)) {
4952
const fields = (namedType as any).getFields()
5053
selections = Object.keys(fields)
5154
.filter(f => isScalar(fields[f].type))
@@ -161,7 +164,7 @@ export function makeSubInfo(
161164
fragment?: string,
162165
): GraphQLResolveInfo | null {
163166
const returnType = getDeepType(info.returnType)
164-
if (returnType instanceof GraphQLScalarType) {
167+
if (isScalarType(returnType)) {
165168
throw new Error(`Can't make subInfo for type ${info.returnType.toString()}`)
166169
}
167170

@@ -178,7 +181,7 @@ export function makeSubInfo(
178181

179182
while (fieldsToTraverse.length > 0) {
180183
currentFieldName = fieldsToTraverse.shift()!
181-
if (!(currentType instanceof GraphQLObjectType)) {
184+
if (!isObjectType(currentType)) {
182185
throw new Error(
183186
`Can't get subInfo for type ${currentType.toString()} as needs to be a GraphQLObjectType`,
184187
)
@@ -192,7 +195,7 @@ export function makeSubInfo(
192195
}
193196

194197
const currentFieldType = fields[currentFieldName].type
195-
if (!(currentFieldType instanceof GraphQLObjectType)) {
198+
if (!isObjectType(currentFieldType)) {
196199
throw new Error(
197200
`Can't get subInfo for type ${currentFieldType} of field ${currentFieldName} on type ${currentType.toString()}`,
198201
)

0 commit comments

Comments
 (0)