From d237ef98615dc013b985a9f28148cc4807e1ad21 Mon Sep 17 00:00:00 2001 From: Ivan Goncharov Date: Thu, 12 Sep 2019 18:13:38 +0300 Subject: [PATCH] Allow empty string as valid 'deprecationReason' (#2167) --- src/type/__tests__/definition-test.js | 36 +++++++++++-------- src/type/definition.js | 4 +-- .../__tests__/buildClientSchema-test.js | 14 ++++++++ src/utilities/schemaPrinter.js | 2 +- 4 files changed, 39 insertions(+), 17 deletions(-) diff --git a/src/type/__tests__/definition-test.js b/src/type/__tests__/definition-test.js index d6bcc5a0c8..e1f5c5a484 100644 --- a/src/type/__tests__/definition-test.js +++ b/src/type/__tests__/definition-test.js @@ -183,20 +183,23 @@ describe('Type System: Objects', () => { type: ScalarType, deprecationReason: 'A terrible reason', }, + baz: { + type: ScalarType, + deprecationReason: '', + }, }, }); - expect(TypeWithDeprecatedField.getFields().bar).to.deep.equal({ + expect(TypeWithDeprecatedField.getFields().bar).to.include({ name: 'bar', - description: undefined, - type: ScalarType, - args: [], - resolve: undefined, - subscribe: undefined, isDeprecated: true, deprecationReason: 'A terrible reason', - extensions: undefined, - astNode: undefined, + }); + + expect(TypeWithDeprecatedField.getFields().baz).to.include({ + name: 'baz', + isDeprecated: true, + deprecationReason: '', }); }); @@ -519,17 +522,22 @@ describe('Type System: Enums', () => { it('defines an enum type with deprecated value', () => { const EnumTypeWithDeprecatedValue = new GraphQLEnumType({ name: 'EnumWithDeprecatedValue', - values: { foo: { deprecationReason: 'Just because' } }, + values: { + foo: { deprecationReason: 'Just because' }, + bar: { deprecationReason: '' }, + }, }); - expect(EnumTypeWithDeprecatedValue.getValues()[0]).to.deep.equal({ + expect(EnumTypeWithDeprecatedValue.getValues()[0]).to.include({ name: 'foo', - description: undefined, isDeprecated: true, deprecationReason: 'Just because', - value: 'foo', - extensions: undefined, - astNode: undefined, + }); + + expect(EnumTypeWithDeprecatedValue.getValues()[1]).to.include({ + name: 'bar', + isDeprecated: true, + deprecationReason: '', }); }); diff --git a/src/type/definition.js b/src/type/definition.js index ed717a5e15..6dfe1ea3aa 100644 --- a/src/type/definition.js +++ b/src/type/definition.js @@ -797,7 +797,7 @@ function defineFieldMap( args, resolve: fieldConfig.resolve, subscribe: fieldConfig.subscribe, - isDeprecated: Boolean(fieldConfig.deprecationReason), + isDeprecated: fieldConfig.deprecationReason != null, deprecationReason: fieldConfig.deprecationReason, extensions: fieldConfig.extensions && toObjMap(fieldConfig.extensions), astNode: fieldConfig.astNode, @@ -1291,7 +1291,7 @@ function defineEnumValues( name: valueName, description: valueConfig.description, value: valueConfig.value !== undefined ? valueConfig.value : valueName, - isDeprecated: Boolean(valueConfig.deprecationReason), + isDeprecated: valueConfig.deprecationReason != null, deprecationReason: valueConfig.deprecationReason, extensions: valueConfig.extensions && toObjMap(valueConfig.extensions), astNode: valueConfig.astNode, diff --git a/src/utilities/__tests__/buildClientSchema-test.js b/src/utilities/__tests__/buildClientSchema-test.js index ce3ad739c9..b67feecb84 100644 --- a/src/utilities/__tests__/buildClientSchema-test.js +++ b/src/utilities/__tests__/buildClientSchema-test.js @@ -509,6 +509,20 @@ describe('Type System: build schema from introspection', () => { expect(cycleIntrospection(sdl)).to.equal(sdl); }); + it('builds a schema with empty deprecation reasons', () => { + const sdl = dedent` + type Query { + someField: String @deprecated(reason: "") + } + + enum SomeEnum { + SOME_VALUE @deprecated(reason: "") + } + `; + + expect(cycleIntrospection(sdl)).to.equal(sdl); + }); + it('can use client schema for limited execution', () => { const schema = buildSchema(` scalar CustomScalar diff --git a/src/utilities/schemaPrinter.js b/src/utilities/schemaPrinter.js index 3f35e96623..6d50a62e5e 100644 --- a/src/utilities/schemaPrinter.js +++ b/src/utilities/schemaPrinter.js @@ -305,7 +305,7 @@ function printDeprecated(fieldOrEnumVal) { } const reason = fieldOrEnumVal.deprecationReason; const reasonAST = astFromValue(reason, GraphQLString); - if (reasonAST && reason !== '' && reason !== DEFAULT_DEPRECATION_REASON) { + if (reasonAST && reason !== DEFAULT_DEPRECATION_REASON) { return ' @deprecated(reason: ' + print(reasonAST) + ')'; } return ' @deprecated';