Skip to content

Commit a725499

Browse files
authored
Add isDeprecated value to field and enum value definitions (#496)
1 parent 89f3f1a commit a725499

File tree

5 files changed

+85
-7
lines changed

5 files changed

+85
-7
lines changed

src/type/__tests__/definition-test.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,41 @@ describe('Type System: Example', () => {
169169

170170
});
171171

172+
it('defines an enum type with deprecated value', () => {
173+
const EnumTypeWithDeprecatedValue = new GraphQLEnumType({
174+
name: 'EnumWithDeprecatedValue',
175+
values: { foo: { deprecationReason: 'Just because' } }
176+
});
177+
178+
expect(EnumTypeWithDeprecatedValue.getValues()[0]).to.deep.equal({
179+
name: 'foo',
180+
description: undefined,
181+
isDeprecated: true,
182+
deprecationReason: 'Just because',
183+
value: 'foo'
184+
});
185+
});
186+
187+
it('defines an object type with deprecated field', () => {
188+
const TypeWithDeprecatedField = new GraphQLObjectType({
189+
name: 'foo',
190+
fields: {
191+
bar: {
192+
type: GraphQLString,
193+
deprecationReason: 'A terrible reason'
194+
}
195+
}
196+
});
197+
198+
expect(TypeWithDeprecatedField.getFields().bar).to.deep.equal({
199+
type: GraphQLString,
200+
deprecationReason: 'A terrible reason',
201+
isDeprecated: true,
202+
name: 'bar',
203+
args: []
204+
});
205+
});
206+
172207
it('includes nested input objects in the map', () => {
173208
const NestedInputObject = new GraphQLInputObjectType({
174209
name: 'NestedInputObject',

src/type/definition.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -410,15 +410,16 @@ function defineFieldMap(
410410
fieldNames.forEach(fieldName => {
411411
assertValidName(fieldName);
412412
const fieldConfig = fieldMap[fieldName];
413-
const field = {
414-
...fieldConfig,
415-
name: fieldName
416-
};
417413
invariant(
418-
!field.hasOwnProperty('isDeprecated'),
414+
!fieldConfig.hasOwnProperty('isDeprecated'),
419415
`${type.name}.${fieldName} should provide "deprecationReason" instead ` +
420416
'of "isDeprecated".'
421417
);
418+
const field = {
419+
...fieldConfig,
420+
isDeprecated: Boolean(fieldConfig.deprecationReason),
421+
name: fieldName
422+
};
422423
invariant(
423424
isOutputType(field.type),
424425
`${type.name}.${fieldName} field type must be Output Type but ` +
@@ -526,6 +527,7 @@ export type GraphQLFieldDefinition = {
526527
type: GraphQLOutputType;
527528
args: Array<GraphQLArgument>;
528529
resolve?: GraphQLFieldResolveFn<*>;
530+
isDeprecated?: boolean;
529531
deprecationReason?: ?string;
530532
};
531533

@@ -831,6 +833,7 @@ function defineEnumValues(
831833
return {
832834
name: valueName,
833835
description: value.description,
836+
isDeprecated: Boolean(value.deprecationReason),
834837
deprecationReason: value.deprecationReason,
835838
value: isNullish(value.value) ? valueName : value.value,
836839
};
@@ -856,6 +859,7 @@ export type GraphQLEnumValueConfig/* <T> */ = {
856859
export type GraphQLEnumValueDefinition/* <T> */ = {
857860
name: string;
858861
description: ?string;
862+
isDeprecated?: boolean;
859863
deprecationReason: ?string;
860864
value: any/* T */;
861865
};

src/type/introspection.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ export const __Field = new GraphQLObjectType({
313313
type: { type: new GraphQLNonNull(__Type) },
314314
isDeprecated: {
315315
type: new GraphQLNonNull(GraphQLBoolean),
316-
resolve: field => !isNullish(field.deprecationReason),
316+
resolve: field => field.isDeprecated,
317317
},
318318
deprecationReason: {
319319
type: GraphQLString,
@@ -354,7 +354,7 @@ export const __EnumValue = new GraphQLObjectType({
354354
description: { type: GraphQLString },
355355
isDeprecated: {
356356
type: new GraphQLNonNull(GraphQLBoolean),
357-
resolve: enumValue => !isNullish(enumValue.deprecationReason),
357+
resolve: enumValue => enumValue.isDeprecated,
358358
},
359359
deprecationReason: {
360360
type: GraphQLString,

src/utilities/__tests__/buildASTSchema-test.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -534,6 +534,40 @@ type Query {
534534
`;
535535
const output = cycleOutput(body);
536536
expect(output).to.equal(body);
537+
538+
const ast = parse(body);
539+
const schema = buildASTSchema(ast);
540+
541+
expect(schema.getType('MyEnum').getValues()).to.deep.equal([
542+
{
543+
name: 'VALUE',
544+
description: '',
545+
isDeprecated: false,
546+
deprecationReason: undefined,
547+
value: 'VALUE'
548+
},
549+
{
550+
name: 'OLD_VALUE',
551+
description: '',
552+
isDeprecated: true,
553+
deprecationReason: 'No longer supported',
554+
value: 'OLD_VALUE'
555+
},
556+
{
557+
name: 'OTHER_VALUE',
558+
description: '',
559+
isDeprecated: true,
560+
deprecationReason: 'Terrible reasons',
561+
value: 'OTHER_VALUE'
562+
}
563+
]);
564+
565+
const rootFields = schema.getType('Query').getFields();
566+
expect(rootFields.field1.isDeprecated).to.equal(true);
567+
expect(rootFields.field1.deprecationReason).to.equal('No longer supported');
568+
569+
expect(rootFields.field2.isDeprecated).to.equal(true);
570+
expect(rootFields.field2.deprecationReason).to.equal('Because I said so');
537571
});
538572
});
539573

src/utilities/__tests__/buildClientSchema-test.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,22 +403,27 @@ describe('Type System: build schema from introspection', () => {
403403
{ name: 'VEGETABLES',
404404
value: 'VEGETABLES',
405405
description: 'Foods that are vegetables.',
406+
isDeprecated: false,
406407
deprecationReason: null, },
407408
{ name: 'FRUITS',
408409
value: 'FRUITS',
409410
description: 'Foods that are fruits.',
411+
isDeprecated: false,
410412
deprecationReason: null, },
411413
{ name: 'OILS',
412414
value: 'OILS',
413415
description: 'Foods that are oils.',
416+
isDeprecated: false,
414417
deprecationReason: null, },
415418
{ name: 'DAIRY',
416419
value: 'DAIRY',
417420
description: 'Foods that are dairy.',
421+
isDeprecated: false,
418422
deprecationReason: null, },
419423
{ name: 'MEAT',
420424
value: 'MEAT',
421425
description: 'Foods that are meat.',
426+
isDeprecated: false,
422427
deprecationReason: null, },
423428
]);
424429
});

0 commit comments

Comments
 (0)