Skip to content

Commit

Permalink
always include nullable outer most input type (#176)
Browse files Browse the repository at this point in the history
see: #174
  • Loading branch information
yaacovCR committed Apr 12, 2022
1 parent b4a3a2b commit 8426c7b
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 2 deletions.
9 changes: 9 additions & 0 deletions .changeset/quick-pans-tan.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
'graphql-executor': patch
---

Always include nullable outermost input type within schema

Because variables could include a default value.

See https://github.com/yaacovCR/graphql-executor/issues/174 for more details.
36 changes: 35 additions & 1 deletion src/executorSchema/__tests__/toExecutorSchema-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ import { expect } from 'chai';
import { describe, it } from 'mocha';

import type {
GraphQLNonNull,
ListTypeNode,
NamedTypeNode,
NonNullTypeNode,
TypeNode,
} from 'graphql';
import {
GraphQLList,
GraphQLInputObjectType,
GraphQLNonNull,
GraphQLObjectType,
GraphQLSchema,
GraphQLString,
Expand All @@ -27,6 +28,16 @@ describe('ExecutorSchema:', () => {
},
},
});
const nonNullableInput = new GraphQLNonNull(
new GraphQLInputObjectType({
name: 'AnotherInput',
fields: {
inputField: {
type: GraphQLString,
},
},
}),
);
const query = new GraphQLObjectType({
name: 'Query',
fields: {
Expand All @@ -38,6 +49,14 @@ describe('ExecutorSchema:', () => {
},
},
},
fieldWithNonNullInputArg: {
type: GraphQLString,
args: {
arg: {
type: nonNullableInput,
},
},
},
fieldWithListInputArg: {
type: GraphQLString,
args: {
Expand Down Expand Up @@ -102,6 +121,21 @@ describe('ExecutorSchema:', () => {
expect((type as GraphQLNonNull<any>).ofType).to.equal(input);
});

it('allows retrieving non-nullable input types defined in schema when unwrapped', () => {
const executorSchema = toExecutorSchema(schema);
const nullableTypeNode: TypeNode = {
kind: Kind.NAMED_TYPE,
name: {
kind: Kind.NAME,
value: 'AnotherInput',
},
};
const type = executorSchema.getType(nullableTypeNode);
expect(type).to.equal(nonNullableInput.ofType);
expect(executorSchema.isNonNullType(type)).to.equal(false);
expect(executorSchema.isInputType(type)).to.equal(true);
});

it('allows retrieving list input types defined in schema', () => {
const executorSchema = toExecutorSchema(schema);
const listTypeNode: ListTypeNode = {
Expand Down
9 changes: 8 additions & 1 deletion src/executorSchema/getPossibleInputTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,17 @@ export function getPossibleInputTypes(
isNonNullType: (type: unknown) => type is GraphQLNonNull<any>,
type: GraphQLInputType,
): Array<GraphQLInputType> {
// See: https://github.com/yaacovCR/graphql-executor/issues/174
// Unwrap any non-null modifier to the outermost type because a variable
// on the outermost type can be nullable if a default value is supplied.
// Non-null versions will then be allowed by the algorithm below as at all
// levels.
const nullableOuterType = isNonNullType(type) ? type.ofType : type;

const { nonNullListWrappers, nonNull, namedType } = getInputTypeInfo(
isListType,
isNonNullType,
type,
nullableOuterType,
);
const sequences = getPossibleSequences(nonNullListWrappers);

Expand Down

0 comments on commit 8426c7b

Please sign in to comment.