Skip to content

Commit

Permalink
Merge pull request #174 from AstrumU/fix/interface_type
Browse files Browse the repository at this point in the history
fix: Support GraphQLInterfaceType
  • Loading branch information
maticzav committed Dec 24, 2018
2 parents e6cb3ce + 6004b47 commit 50190cf
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 4 deletions.
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

0 comments on commit 50190cf

Please sign in to comment.