From 4084ba168de28ea9b747e9df6a16a45972e6a768 Mon Sep 17 00:00:00 2001 From: Hyohyeon Jeong Date: Mon, 19 Sep 2016 16:50:24 -0700 Subject: [PATCH] Add isDeprecated value to field and enum value definitions --- src/type/__tests__/definition-test.js | 35 +++++++++++++++++++ src/type/definition.js | 14 +++++--- src/type/introspection.js | 4 +-- .../__tests__/buildASTSchema-test.js | 34 ++++++++++++++++++ .../__tests__/buildClientSchema-test.js | 5 +++ 5 files changed, 85 insertions(+), 7 deletions(-) diff --git a/src/type/__tests__/definition-test.js b/src/type/__tests__/definition-test.js index f536fcbf0f..32a64dfbda 100644 --- a/src/type/__tests__/definition-test.js +++ b/src/type/__tests__/definition-test.js @@ -169,6 +169,41 @@ describe('Type System: Example', () => { }); + it('defines an enum type with deprecated value', () => { + const EnumTypeWithDeprecatedValue = new GraphQLEnumType({ + name: 'EnumWithDeprecatedValue', + values: { foo: { deprecationReason: 'Just because' } } + }); + + expect(EnumTypeWithDeprecatedValue.getValues()[0]).to.deep.equal({ + name: 'foo', + description: undefined, + isDeprecated: true, + deprecationReason: 'Just because', + value: 'foo' + }); + }); + + it('defines an object type with deprecated field', () => { + const TypeWithDeprecatedField = new GraphQLObjectType({ + name: 'foo', + fields: { + bar: { + type: GraphQLString, + deprecationReason: 'A terrible reason' + } + } + }); + + expect(TypeWithDeprecatedField.getFields().bar).to.deep.equal({ + type: GraphQLString, + deprecationReason: 'A terrible reason', + isDeprecated: true, + name: 'bar', + args: [] + }); + }); + it('includes nested input objects in the map', () => { const NestedInputObject = new GraphQLInputObjectType({ name: 'NestedInputObject', diff --git a/src/type/definition.js b/src/type/definition.js index 873d880931..a406001230 100644 --- a/src/type/definition.js +++ b/src/type/definition.js @@ -410,15 +410,16 @@ function defineFieldMap( fieldNames.forEach(fieldName => { assertValidName(fieldName); const fieldConfig = fieldMap[fieldName]; - const field = { - ...fieldConfig, - name: fieldName - }; invariant( - !field.hasOwnProperty('isDeprecated'), + !fieldConfig.hasOwnProperty('isDeprecated'), `${type.name}.${fieldName} should provide "deprecationReason" instead ` + 'of "isDeprecated".' ); + const field = { + ...fieldConfig, + isDeprecated: Boolean(fieldConfig.deprecationReason), + name: fieldName + }; invariant( isOutputType(field.type), `${type.name}.${fieldName} field type must be Output Type but ` + @@ -526,6 +527,7 @@ export type GraphQLFieldDefinition = { type: GraphQLOutputType; args: Array; resolve?: GraphQLFieldResolveFn<*>; + isDeprecated?: boolean; deprecationReason?: ?string; }; @@ -831,6 +833,7 @@ function defineEnumValues( return { name: valueName, description: value.description, + isDeprecated: Boolean(value.deprecationReason), deprecationReason: value.deprecationReason, value: isNullish(value.value) ? valueName : value.value, }; @@ -856,6 +859,7 @@ export type GraphQLEnumValueConfig/* */ = { export type GraphQLEnumValueDefinition/* */ = { name: string; description: ?string; + isDeprecated?: boolean; deprecationReason: ?string; value: any/* T */; }; diff --git a/src/type/introspection.js b/src/type/introspection.js index 0e73803722..d157224548 100644 --- a/src/type/introspection.js +++ b/src/type/introspection.js @@ -313,7 +313,7 @@ export const __Field = new GraphQLObjectType({ type: { type: new GraphQLNonNull(__Type) }, isDeprecated: { type: new GraphQLNonNull(GraphQLBoolean), - resolve: field => !isNullish(field.deprecationReason), + resolve: field => field.isDeprecated, }, deprecationReason: { type: GraphQLString, @@ -354,7 +354,7 @@ export const __EnumValue = new GraphQLObjectType({ description: { type: GraphQLString }, isDeprecated: { type: new GraphQLNonNull(GraphQLBoolean), - resolve: enumValue => !isNullish(enumValue.deprecationReason), + resolve: enumValue => enumValue.isDeprecated, }, deprecationReason: { type: GraphQLString, diff --git a/src/utilities/__tests__/buildASTSchema-test.js b/src/utilities/__tests__/buildASTSchema-test.js index f9ad868b38..3f6395dba6 100644 --- a/src/utilities/__tests__/buildASTSchema-test.js +++ b/src/utilities/__tests__/buildASTSchema-test.js @@ -534,6 +534,40 @@ type Query { `; const output = cycleOutput(body); expect(output).to.equal(body); + + const ast = parse(body); + const schema = buildASTSchema(ast); + + expect(schema.getType('MyEnum').getValues()).to.deep.equal([ + { + name: 'VALUE', + description: '', + isDeprecated: false, + deprecationReason: undefined, + value: 'VALUE' + }, + { + name: 'OLD_VALUE', + description: '', + isDeprecated: true, + deprecationReason: 'No longer supported', + value: 'OLD_VALUE' + }, + { + name: 'OTHER_VALUE', + description: '', + isDeprecated: true, + deprecationReason: 'Terrible reasons', + value: 'OTHER_VALUE' + } + ]); + + const rootFields = schema.getType('Query').getFields(); + expect(rootFields.field1.isDeprecated).to.equal(true); + expect(rootFields.field1.deprecationReason).to.equal('No longer supported'); + + expect(rootFields.field2.isDeprecated).to.equal(true); + expect(rootFields.field2.deprecationReason).to.equal('Because I said so'); }); }); diff --git a/src/utilities/__tests__/buildClientSchema-test.js b/src/utilities/__tests__/buildClientSchema-test.js index d383ce05b5..f311d35d8f 100644 --- a/src/utilities/__tests__/buildClientSchema-test.js +++ b/src/utilities/__tests__/buildClientSchema-test.js @@ -403,22 +403,27 @@ describe('Type System: build schema from introspection', () => { { name: 'VEGETABLES', value: 'VEGETABLES', description: 'Foods that are vegetables.', + isDeprecated: false, deprecationReason: null, }, { name: 'FRUITS', value: 'FRUITS', description: 'Foods that are fruits.', + isDeprecated: false, deprecationReason: null, }, { name: 'OILS', value: 'OILS', description: 'Foods that are oils.', + isDeprecated: false, deprecationReason: null, }, { name: 'DAIRY', value: 'DAIRY', description: 'Foods that are dairy.', + isDeprecated: false, deprecationReason: null, }, { name: 'MEAT', value: 'MEAT', description: 'Foods that are meat.', + isDeprecated: false, deprecationReason: null, }, ]); });