Skip to content

Commit

Permalink
fix: Add GraphQLInterfaceType inside buildInfoForAllScalars
Browse files Browse the repository at this point in the history
fix: Add GraphQLInterfaceType inside buildInfoForAllScalars

fix: Maybe type from codegen folder
  • Loading branch information
XBeg9 committed Dec 21, 2018
1 parent e6cb3ce commit 8ea063c
Show file tree
Hide file tree
Showing 4 changed files with 618 additions and 373 deletions.
1 change: 1 addition & 0 deletions src/codegen/TypescriptGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
} from 'graphql'

import { Generator } from './Generator'

import { Maybe } from './types'

export class TypescriptGenerator extends Generator {
Expand Down
25 changes: 25 additions & 0 deletions src/info.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,31 @@ test('buildInfoForAllScalars: excludes object type fields', t => {
t.is(info.fieldName, 'book')
})

test('buildInfoForAllScalars: support interfaces', t => {
const schema = buildSchema(`
type Query {
book: IBook
}
type Book implements IBook {
title: String
number: Float
otherBook: IBook
}
interface IBook {
title: String
number: Float
otherBook: Book
}
`)
const info = buildInfoForAllScalars('book', schema, 'query')
const selections = info.fieldNodes[0].selectionSet!.selections

assertFields(t, selections, ['title', 'number'])
t.is(info.fieldName, 'book')
})

test('buildInfoForAllScalars: enums', t => {
const schema = buildSchema(`
type Query {
Expand Down
11 changes: 7 additions & 4 deletions src/info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ import {
print,
parse,
validate,
isObjectType,
isInterfaceType,
isScalarType,
} from 'graphql'

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

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

Expand All @@ -178,7 +181,7 @@ export function makeSubInfo(

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

const currentFieldType = fields[currentFieldName].type
if (!(currentFieldType instanceof GraphQLObjectType)) {
if (!isObjectType(currentFieldType)) {
throw new Error(
`Can't get subInfo for type ${currentFieldType} of field ${currentFieldName} on type ${currentType.toString()}`,
)
Expand Down
Loading

0 comments on commit 8ea063c

Please sign in to comment.