Skip to content

Commit

Permalink
fix ExecutorSchema isNullType (#128)
Browse files Browse the repository at this point in the history
  • Loading branch information
yaacovCR authored Jan 6, 2022
1 parent feb7888 commit 0111863
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 1 deletion.
7 changes: 7 additions & 0 deletions .changeset/moody-trees-give.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'graphql-executor': patch
---

Fix ExecutorSchema isNonNullType method

Client documents may wrap input types with non-nullable wrapper types not present in the schema. The ExecutorSchema should recognize these non-nullable types as such.
90 changes: 90 additions & 0 deletions src/execution/__tests__/toExecutorSchema-test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import { expect } from 'chai';
import { describe, it } from 'mocha';

import type { GraphQLNonNull, NamedTypeNode, NonNullTypeNode } from 'graphql';
import {
GraphQLInputObjectType,
GraphQLObjectType,
GraphQLSchema,
GraphQLString,
Kind,
} from 'graphql';

import { toExecutorSchema } from '..';

describe('ExecutorSchema:', () => {
const input = new GraphQLInputObjectType({
name: 'Input',
fields: {
inputField: {
type: GraphQLString,
},
},
});
const query = new GraphQLObjectType({
name: 'Query',
fields: {
field: {
type: GraphQLString,
args: {
arg: {
type: input,
},
},
},
},
});
const schema = new GraphQLSchema({
query,
});

it('does not throw', () => {
expect(() => toExecutorSchema(schema)).not.to.throw();
});

it('allows retrieving output types', () => {
const executorSchema = toExecutorSchema(schema);
const namedTypeNode: NamedTypeNode = {
kind: Kind.NAMED_TYPE,
name: {
kind: Kind.NAME,
value: 'Query',
},
};
const type = executorSchema.getType(namedTypeNode);
expect(type).to.equal(query);
});

it('allows retrieving input types', () => {
const executorSchema = toExecutorSchema(schema);
const namedTypeNode: NamedTypeNode = {
kind: Kind.NAMED_TYPE,
name: {
kind: Kind.NAME,
value: 'Input',
},
};
const type = executorSchema.getType(namedTypeNode);
expect(type).to.equal(input);
expect(executorSchema.isInputType(type)).to.equal(true);
});

it('allows retrieving input types defined in schema wrapped with non-null', () => {
const executorSchema = toExecutorSchema(schema);
const nonNullTypeNode: NonNullTypeNode = {
kind: Kind.NON_NULL_TYPE,
type: {
kind: Kind.NAMED_TYPE,
name: {
kind: Kind.NAME,
value: 'Input',
},
},
};
const type = executorSchema.getType(nonNullTypeNode);
expect(type).to.not.equal(undefined);
expect(executorSchema.isNonNullType(type)).to.equal(true);
expect(executorSchema.isInputType(type)).to.equal(true);
expect((type as GraphQLNonNull<any>).ofType).to.equal(input);
});
});
2 changes: 1 addition & 1 deletion src/execution/toExecutorSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,6 @@ function _toExecutorSchema(schema: GraphQLSchema): ExecutorSchema {

for (const directive of [...schema.getDirectives()]) {
for (const arg of directive.args) {
inputTypes.add(arg.type);
addInputType(arg.type);
processType(arg.type);
}
Expand All @@ -365,6 +364,7 @@ function _toExecutorSchema(schema: GraphQLSchema): ExecutorSchema {
const typeString = possibleInputType.toString();
if (!typeTree.has(typeString)) {
addInputType(possibleInputType);
processType(possibleInputType);
}
}
}
Expand Down

0 comments on commit 0111863

Please sign in to comment.