Skip to content

Commit b71a738

Browse files
committed
Merge pull request #216 from graphql/deprecation-introspection
Support deprecation via introspection
2 parents 4355c40 + e1806ce commit b71a738

File tree

4 files changed

+72
-28
lines changed

4 files changed

+72
-28
lines changed

src/type/definition.js

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -838,11 +838,12 @@ function defineEnumValues(
838838
`${type}.${valueName} should provide "deprecationReason" instead ` +
839839
`of "isDeprecated".`
840840
);
841-
value.name = valueName;
842-
if (isNullish(value.value)) {
843-
value.value = valueName;
844-
}
845-
return value;
841+
return {
842+
name: valueName,
843+
description: value.description,
844+
deprecationReason: value.deprecationReason,
845+
value: isNullish(value.value) ? valueName : value.value,
846+
};
846847
});
847848
}
848849

@@ -858,15 +859,15 @@ export type GraphQLEnumValueConfigMap/* <T> */ = {
858859

859860
export type GraphQLEnumValueConfig/* <T> */ = {
860861
value?: any/* T */;
861-
deprecationReason?: string;
862+
deprecationReason?: ?string;
862863
description?: ?string;
863864
}
864865

865866
export type GraphQLEnumValueDefinition/* <T> */ = {
866867
name: string;
867-
value?: any/* T */;
868-
deprecationReason?: string;
869-
description?: ?string;
868+
description: ?string;
869+
deprecationReason: ?string;
870+
value: any/* T */;
870871
}
871872

872873

src/utilities/__tests__/buildClientSchema.js

Lines changed: 58 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -371,21 +371,26 @@ describe('Type System: build schema from introspection', () => {
371371
// Client types do not get server-only values, so `value` mirrors `name`,
372372
// rather than using the integers defined in the "server" schema.
373373
expect(clientFoodEnum.getValues()).to.deep.equal([
374-
{ description: 'Foods that are vegetables.',
375-
name: 'VEGETABLES',
376-
value: 'VEGETABLES' },
377-
{ description: 'Foods that are fruits.',
378-
name: 'FRUITS',
379-
value: 'FRUITS' },
380-
{ description: 'Foods that are oils.',
381-
name: 'OILS',
382-
value: 'OILS' },
383-
{ description: 'Foods that are dairy.',
384-
name: 'DAIRY',
385-
value: 'DAIRY' },
386-
{ description: 'Foods that are meat.',
387-
name: 'MEAT',
388-
value: 'MEAT' },
374+
{ name: 'VEGETABLES',
375+
value: 'VEGETABLES',
376+
description: 'Foods that are vegetables.',
377+
deprecationReason: null, },
378+
{ name: 'FRUITS',
379+
value: 'FRUITS',
380+
description: 'Foods that are fruits.',
381+
deprecationReason: null, },
382+
{ name: 'OILS',
383+
value: 'OILS',
384+
description: 'Foods that are oils.',
385+
deprecationReason: null, },
386+
{ name: 'DAIRY',
387+
value: 'DAIRY',
388+
description: 'Foods that are dairy.',
389+
deprecationReason: null, },
390+
{ name: 'MEAT',
391+
value: 'MEAT',
392+
description: 'Foods that are meat.',
393+
deprecationReason: null, },
389394
]);
390395
});
391396

@@ -503,6 +508,44 @@ describe('Type System: build schema from introspection', () => {
503508
await testSchema(schema);
504509
});
505510

511+
512+
it('builds a schema aware of deprecation', async () => {
513+
514+
var schema = new GraphQLSchema({
515+
query: new GraphQLObjectType({
516+
name: 'Simple',
517+
description: 'This is a simple type',
518+
fields: {
519+
shinyString: {
520+
type: GraphQLString,
521+
description: 'This is a shiny string field'
522+
},
523+
deprecatedString: {
524+
type: GraphQLString,
525+
description: 'This is a deprecated string field',
526+
deprecationReason: 'Use shinyString',
527+
},
528+
color: {
529+
type: new GraphQLEnumType({
530+
name: 'Color',
531+
values: {
532+
RED: { description: 'So rosy' },
533+
GREEN: { description: 'So grassy' },
534+
BLUE: { description: 'So calming' },
535+
MAUVE: {
536+
description: 'So sickening',
537+
deprecationReason: 'No longer in fashion'
538+
},
539+
}
540+
})
541+
}
542+
}
543+
})
544+
});
545+
546+
await testSchema(schema);
547+
});
548+
506549
it('cannot use client schema for general execution', async () => {
507550
var customScalar = new GraphQLScalarType({
508551
name: 'CustomScalar',

src/utilities/buildClientSchema.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,7 @@ export function buildClientSchema(
260260
valueIntrospection => valueIntrospection.name,
261261
valueIntrospection => ({
262262
description: valueIntrospection.description,
263+
deprecationReason: valueIntrospection.deprecationReason,
263264
})
264265
)
265266
});
@@ -281,6 +282,7 @@ export function buildClientSchema(
281282
fieldIntrospection => fieldIntrospection.name,
282283
fieldIntrospection => ({
283284
description: fieldIntrospection.description,
285+
deprecationReason: fieldIntrospection.deprecationReason,
284286
type: getOutputType(fieldIntrospection.type),
285287
args: buildInputValueDefMap(fieldIntrospection.args),
286288
resolve: () => {
@@ -322,8 +324,6 @@ export function buildClientSchema(
322324
});
323325
}
324326

325-
// TODO: deprecation
326-
327327
// Iterate through all types, getting the type definition for each, ensuring
328328
// that any type not directly referenced by a field will get created.
329329
schemaIntrospection.types.forEach(

src/utilities/introspectionQuery.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ export var introspectionQuery = `
3434
kind
3535
name
3636
description
37-
fields {
37+
fields(includeDeprecated: true) {
3838
name
3939
description
4040
args {
@@ -52,7 +52,7 @@ export var introspectionQuery = `
5252
interfaces {
5353
...TypeRef
5454
}
55-
enumValues {
55+
enumValues(includeDeprecated: true) {
5656
name
5757
description
5858
isDeprecated

0 commit comments

Comments
 (0)