From 901c4e78cd211f34307d4f80d8beed5fe04808a0 Mon Sep 17 00:00:00 2001 From: Ivan Goncharov Date: Thu, 15 Aug 2019 23:57:31 +0300 Subject: [PATCH] definitions: make constructed fields non-optional (#2091) Reflects changes in #2089 --- src/type/__tests__/predicate-test.js | 70 ++++++++++++++++------------ src/type/definition.js | 20 ++++---- src/type/introspection.js | 16 ++++++- 3 files changed, 65 insertions(+), 41 deletions(-) diff --git a/src/type/__tests__/predicate-test.js b/src/type/__tests__/predicate-test.js index 7e655a7bab..2a5f885b62 100644 --- a/src/type/__tests__/predicate-test.js +++ b/src/type/__tests__/predicate-test.js @@ -573,77 +573,87 @@ describe('Type predicates', () => { }); describe('isRequiredArgument', () => { - it('returns true for required arguments', () => { - const requiredArg = { + function buildArg(config) { + return { name: 'someArg', - type: GraphQLNonNull(GraphQLString), + description: undefined, + defaultValue: undefined, + astNode: undefined, + ...config, }; + } + + it('returns true for required arguments', () => { + const requiredArg = buildArg({ + type: GraphQLNonNull(GraphQLString), + }); expect(isRequiredArgument(requiredArg)).to.equal(true); }); it('returns false for optional arguments', () => { - const optArg1 = { - name: 'someArg', + const optArg1 = buildArg({ type: GraphQLString, - }; + }); expect(isRequiredArgument(optArg1)).to.equal(false); - const optArg2 = { - name: 'someArg', + const optArg2 = buildArg({ type: GraphQLString, defaultValue: null, - }; + }); expect(isRequiredArgument(optArg2)).to.equal(false); - const optArg3 = { - name: 'someArg', + const optArg3 = buildArg({ type: GraphQLList(GraphQLNonNull(GraphQLString)), - }; + }); expect(isRequiredArgument(optArg3)).to.equal(false); - const optArg4 = { - name: 'someArg', + const optArg4 = buildArg({ type: GraphQLNonNull(GraphQLString), defaultValue: 'default', - }; + }); expect(isRequiredArgument(optArg4)).to.equal(false); }); }); describe('isRequiredInputField', () => { + function buildInputField(config) { + return { + name: 'someInputField', + description: undefined, + defaultValue: undefined, + astNode: undefined, + ...config, + }; + } + it('returns true for required input field', () => { - const requiredField = { - name: 'someField', + const requiredField = buildInputField({ type: GraphQLNonNull(GraphQLString), - }; + }); expect(isRequiredInputField(requiredField)).to.equal(true); }); it('returns false for optional input field', () => { - const optField1 = { - name: 'someField', + const optField1 = buildInputField({ type: GraphQLString, - }; + }); expect(isRequiredInputField(optField1)).to.equal(false); - const optField2 = { - name: 'someField', + const optField2 = buildInputField({ type: GraphQLString, defaultValue: null, - }; + }); expect(isRequiredInputField(optField2)).to.equal(false); - const optField3 = { - name: 'someField', + const optField3 = buildInputField({ type: GraphQLList(GraphQLNonNull(GraphQLString)), - }; + }); expect(isRequiredInputField(optField3)).to.equal(false); - const optField4 = { - name: 'someField', + const optField4 = buildInputField({ type: GraphQLNonNull(GraphQLString), defaultValue: 'default', - }; + }); expect(isRequiredInputField(optField4)).to.equal(false); }); }); diff --git a/src/type/definition.js b/src/type/definition.js index ce151b6d89..4c15ff69e5 100644 --- a/src/type/definition.js +++ b/src/type/definition.js @@ -909,16 +909,16 @@ export type GraphQLField< resolve?: GraphQLFieldResolver, subscribe?: GraphQLFieldResolver, isDeprecated?: boolean, - deprecationReason?: ?string, - astNode?: ?FieldDefinitionNode, + deprecationReason: ?string, + astNode: ?FieldDefinitionNode, |}; export type GraphQLArgument = {| name: string, - description?: ?string, + description: ?string, type: GraphQLInputType, - defaultValue?: mixed, - astNode?: ?InputValueDefinitionNode, + defaultValue: mixed, + astNode: ?InputValueDefinitionNode, |}; export function isRequiredArgument(arg: GraphQLArgument): boolean %checks { @@ -1284,9 +1284,9 @@ export type GraphQLEnumValue /* */ = {| name: string, description: ?string, value: any /* T */, - isDeprecated?: boolean, + isDeprecated: boolean, deprecationReason: ?string, - astNode?: ?EnumValueDefinitionNode, + astNode: ?EnumValueDefinitionNode, |}; /** @@ -1407,10 +1407,10 @@ export type GraphQLInputFieldConfigMap = ObjMap; export type GraphQLInputField = {| name: string, - description?: ?string, + description: ?string, type: GraphQLInputType, - defaultValue?: mixed, - astNode?: ?InputValueDefinitionNode, + defaultValue: mixed, + astNode: ?InputValueDefinitionNode, |}; export function isRequiredInputField( diff --git a/src/type/introspection.js b/src/type/introspection.js index eff1e0c42e..d74354e471 100644 --- a/src/type/introspection.js +++ b/src/type/introspection.js @@ -438,14 +438,26 @@ export const SchemaMetaFieldDef: GraphQLField = { description: 'Access the current type schema of this server.', args: [], resolve: (source, args, context, { schema }) => schema, + deprecationReason: undefined, + astNode: undefined, }; export const TypeMetaFieldDef: GraphQLField = { name: '__type', type: __Type, description: 'Request the type information of a single type.', - args: [{ name: 'name', type: GraphQLNonNull(GraphQLString) }], + args: [ + { + name: 'name', + description: undefined, + type: GraphQLNonNull(GraphQLString), + defaultValue: undefined, + astNode: undefined, + }, + ], resolve: (source, { name }, context, { schema }) => schema.getType(name), + deprecationReason: undefined, + astNode: undefined, }; export const TypeNameMetaFieldDef: GraphQLField = { @@ -454,6 +466,8 @@ export const TypeNameMetaFieldDef: GraphQLField = { description: 'The name of the current Object type at runtime.', args: [], resolve: (source, args, context, { parentType }) => parentType.name, + deprecationReason: undefined, + astNode: undefined, }; export const introspectionTypes = Object.freeze([