Skip to content

Add deprecated directive to arguments and input values (rebased) #2464

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/type/__tests__/definition-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,8 @@ describe('Type System: Objects', () => {
description: undefined,
type: ScalarType,
defaultValue: undefined,
deprecationReason: undefined,
isDeprecated: false,
extensions: undefined,
astNode: undefined,
},
Expand Down Expand Up @@ -750,6 +752,8 @@ describe('Type System: Input Objects', () => {
description: undefined,
type: ScalarType,
defaultValue: undefined,
deprecationReason: undefined,
isDeprecated: false,
extensions: undefined,
astNode: undefined,
},
Expand All @@ -770,6 +774,8 @@ describe('Type System: Input Objects', () => {
type: ScalarType,
defaultValue: undefined,
extensions: undefined,
isDeprecated: false,
deprecationReason: undefined,
astNode: undefined,
},
});
Expand Down
57 changes: 54 additions & 3 deletions src/type/__tests__/introspection-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,17 @@ describe('Introspection', () => {
},
{
name: 'inputFields',
args: [],
args: [
{
name: 'includeDeprecated',
type: {
kind: 'SCALAR',
name: 'Boolean',
ofType: null,
},
defaultValue: 'false',
},
],
type: {
kind: 'LIST',
name: null,
Expand Down Expand Up @@ -441,7 +451,17 @@ describe('Introspection', () => {
},
{
name: 'args',
args: [],
args: [
{
name: 'includeDeprecated',
type: {
kind: 'SCALAR',
name: 'Boolean',
ofType: null,
},
defaultValue: 'false',
},
],
type: {
kind: 'NON_NULL',
name: null,
Expand Down Expand Up @@ -565,6 +585,32 @@ describe('Introspection', () => {
isDeprecated: false,
deprecationReason: null,
},
{
name: 'isDeprecated',
args: [],
type: {
kind: 'NON_NULL',
name: null,
ofType: {
kind: 'SCALAR',
name: 'Boolean',
ofType: null,
},
},
isDeprecated: false,
deprecationReason: null,
},
{
name: 'deprecationReason',
args: [],
type: {
kind: 'SCALAR',
name: 'String',
ofType: null,
},
isDeprecated: false,
deprecationReason: null,
},
],
inputFields: null,
interfaces: [],
Expand Down Expand Up @@ -880,7 +926,12 @@ describe('Introspection', () => {
{
name: 'deprecated',
isRepeatable: false,
locations: ['FIELD_DEFINITION', 'ENUM_VALUE'],
locations: [
'FIELD_DEFINITION',
'ENUM_VALUE',
'ARGUMENT_DEFINITION',
'INPUT_FIELD_DEFINITION',
],
args: [
{
defaultValue: '"No longer supported"',
Expand Down
2 changes: 2 additions & 0 deletions src/type/definition.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,8 @@ export interface GraphQLArgumentConfig {
description?: Maybe<string>;
type: GraphQLInputType;
defaultValue?: any;
isDeprecated?: boolean;
deprecationReason?: Maybe<string>;
extensions?: Maybe<Readonly<Record<string, any>>>;
astNode?: Maybe<InputValueDefinitionNode>;
}
Expand Down
19 changes: 19 additions & 0 deletions src/type/definition.js
Original file line number Diff line number Diff line change
Expand Up @@ -825,6 +825,8 @@ function defineFieldMap<TSource, TContext>(
type: argConfig.type,
defaultValue: argConfig.defaultValue,
extensions: argConfig.extensions && toObjMap(argConfig.extensions),
isDeprecated: argConfig.deprecationReason != null,
deprecationReason: argConfig.deprecationReason,
astNode: argConfig.astNode,
}));

Expand Down Expand Up @@ -873,6 +875,8 @@ export function argsToArgsConfig(
description: arg.description,
type: arg.type,
defaultValue: arg.defaultValue,
deprecationReason: arg.deprecationReason,
isDeprecated: arg.deprecationReason != null,
extensions: arg.extensions,
astNode: arg.astNode,
}),
Expand Down Expand Up @@ -949,6 +953,8 @@ export type GraphQLArgumentConfig = {|
type: GraphQLInputType,
defaultValue?: mixed,
extensions?: ?ReadOnlyObjMapLike<mixed>,
deprecationReason?: ?string,
isDeprecated?: boolean,
astNode?: ?InputValueDefinitionNode,
|};

Expand Down Expand Up @@ -978,6 +984,8 @@ export type GraphQLArgument = {|
description: ?string,
type: GraphQLInputType,
defaultValue: mixed,
isDeprecated?: boolean,
deprecationReason?: ?string,
extensions: ?ReadOnlyObjMap<mixed>,
astNode: ?InputValueDefinitionNode,
|};
Expand Down Expand Up @@ -1517,17 +1525,25 @@ function defineInputFieldMap(
isPlainObj(fieldMap),
`${config.name} fields must be an object with field names as keys or a function which returns such an object.`,
);

return mapValue(fieldMap, (fieldConfig, fieldName) => {
devAssert(
!('resolve' in fieldConfig),
`${config.name}.${fieldName} field has a resolve property, but Input Types cannot define resolvers.`,
);
devAssert(
!('isDeprecated' in fieldConfig),
`${config.name}.${fieldName} should provide "deprecationReason" ` +
'instead of "isDeprecated".',
);

return {
name: fieldName,
description: fieldConfig.description,
type: fieldConfig.type,
defaultValue: fieldConfig.defaultValue,
deprecationReason: fieldConfig.deprecationReason,
isDeprecated: fieldConfig.deprecationReason != null,
extensions: fieldConfig.extensions && toObjMap(fieldConfig.extensions),
astNode: fieldConfig.astNode,
};
Expand All @@ -1547,6 +1563,7 @@ export type GraphQLInputFieldConfig = {|
description?: ?string,
type: GraphQLInputType,
defaultValue?: mixed,
deprecationReason?: ?string,
extensions?: ?ReadOnlyObjMapLike<mixed>,
astNode?: ?InputValueDefinitionNode,
|};
Expand All @@ -1558,6 +1575,8 @@ export type GraphQLInputField = {|
description: ?string,
type: GraphQLInputType,
defaultValue: mixed,
isDeprecated?: boolean,
deprecationReason?: ?string,
extensions: ?ReadOnlyObjMap<mixed>,
astNode: ?InputValueDefinitionNode,
|};
Expand Down
7 changes: 6 additions & 1 deletion src/type/directives.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,12 @@ export const DEFAULT_DEPRECATION_REASON = 'No longer supported';
export const GraphQLDeprecatedDirective = new GraphQLDirective({
name: 'deprecated',
description: 'Marks an element of a GraphQL schema as no longer supported.',
locations: [DirectiveLocation.FIELD_DEFINITION, DirectiveLocation.ENUM_VALUE],
locations: [
DirectiveLocation.FIELD_DEFINITION,
DirectiveLocation.ENUM_VALUE,
DirectiveLocation.ARGUMENT_DEFINITION,
DirectiveLocation.INPUT_FIELD_DEFINITION,
],
args: {
reason: {
type: GraphQLString,
Expand Down
39 changes: 36 additions & 3 deletions src/type/introspection.js
Original file line number Diff line number Diff line change
Expand Up @@ -288,9 +288,19 @@ export const __Type = new GraphQLObjectType({
},
inputFields: {
type: GraphQLList(GraphQLNonNull(__InputValue)),
resolve(type) {
args: {
includeDeprecated: {
type: GraphQLBoolean,
defaultValue: false,
},
},
resolve(type, { includeDeprecated }) {
if (isInputObjectType(type)) {
return objectValues(type.getFields());
let values = objectValues(type.getFields());
if (!includeDeprecated) {
values = values.filter(value => !value.deprecationReason);
}
return values;
}
},
},
Expand All @@ -317,7 +327,22 @@ export const __Field = new GraphQLObjectType({
},
args: {
type: GraphQLNonNull(GraphQLList(GraphQLNonNull(__InputValue))),
resolve: field => field.args,
args: {
includeDeprecated: {
type: GraphQLBoolean,
defaultValue: false,
},
},
// resolve: field => field.args || [],
resolve(field, { includeDeprecated }) {
let args = field.args || [];

if (!includeDeprecated) {
args = args.filter(arg => !arg.deprecationReason);
}

return args;
},
},
type: {
type: GraphQLNonNull(__Type),
Expand Down Expand Up @@ -362,6 +387,14 @@ export const __InputValue = new GraphQLObjectType({
return valueAST ? print(valueAST) : null;
},
},
isDeprecated: {
type: GraphQLNonNull(GraphQLBoolean),
resolve: obj => obj.isDeprecated,
},
deprecationReason: {
type: GraphQLString,
resolve: obj => obj.deprecationReason,
},
}: GraphQLFieldConfigMap<GraphQLInputField, mixed>),
});

Expand Down
42 changes: 42 additions & 0 deletions src/utilities/__tests__/buildASTSchema-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -732,10 +732,19 @@ describe('Schema Builder', () => {
OTHER_VALUE @deprecated(reason: "Terrible reasons")
}

input MyInput {
oldInput: String @deprecated
otherInput: String @deprecated(reason: "Use newInput")
newInput: String
}

type Query {
field1: String @deprecated
field2: Int @deprecated(reason: "Because I said so")
enum: MyEnum
field3(oldArg: String @deprecated, arg: String): String
field4(oldArg: String @deprecated(reason: "Why not?"), arg: String): String
field5(arg: MyInput): String
}
`;
expect(cycleSDL(sdl)).to.equal(sdl);
Expand Down Expand Up @@ -768,6 +777,39 @@ describe('Schema Builder', () => {
isDeprecated: true,
deprecationReason: 'Because I said so',
});

const inputFields = assertInputObjectType(
schema.getType('MyInput'),
).getFields();

const newInput = inputFields.newInput;
expect(newInput).to.include({
isDeprecated: false,
});

const oldInput = inputFields.oldInput;
expect(oldInput).to.include({
isDeprecated: true,
deprecationReason: 'No longer supported',
});

const otherInput = inputFields.otherInput;
expect(otherInput).to.include({
isDeprecated: true,
deprecationReason: 'Use newInput',
});

const field3OldArg = rootFields.field3.args[0];
expect(field3OldArg).to.include({
isDeprecated: true,
deprecationReason: 'No longer supported',
});

const field4OldArg = rootFields.field4.args[0];
expect(field4OldArg).to.include({
isDeprecated: true,
deprecationReason: 'Why not?',
});
});

it('Correctly extend scalar type', () => {
Expand Down
Loading