Skip to content

Commit

Permalink
Add isDeprecated value to field and enum value definitions
Browse files Browse the repository at this point in the history
  • Loading branch information
asiandrummer committed Sep 20, 2016
1 parent 89f3f1a commit 4084ba1
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 7 deletions.
35 changes: 35 additions & 0 deletions src/type/__tests__/definition-test.js
Expand Up @@ -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',
Expand Down
14 changes: 9 additions & 5 deletions src/type/definition.js
Expand Up @@ -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 ` +
Expand Down Expand Up @@ -526,6 +527,7 @@ export type GraphQLFieldDefinition = {
type: GraphQLOutputType;
args: Array<GraphQLArgument>;
resolve?: GraphQLFieldResolveFn<*>;
isDeprecated?: boolean;
deprecationReason?: ?string;
};

Expand Down Expand Up @@ -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,
};
Expand All @@ -856,6 +859,7 @@ export type GraphQLEnumValueConfig/* <T> */ = {
export type GraphQLEnumValueDefinition/* <T> */ = {
name: string;
description: ?string;
isDeprecated?: boolean;
deprecationReason: ?string;
value: any/* T */;
};
Expand Down
4 changes: 2 additions & 2 deletions src/type/introspection.js
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
34 changes: 34 additions & 0 deletions src/utilities/__tests__/buildASTSchema-test.js
Expand Up @@ -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');
});
});

Expand Down
5 changes: 5 additions & 0 deletions src/utilities/__tests__/buildClientSchema-test.js
Expand Up @@ -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, },
]);
});
Expand Down

0 comments on commit 4084ba1

Please sign in to comment.