-
-
Notifications
You must be signed in to change notification settings - Fork 100
/
no-deprecated.ts
124 lines (115 loc) · 3.47 KB
/
no-deprecated.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import { getLocation, requireGraphQLSchemaFromContext } from '../utils';
import { GraphQLESLintRule } from '../types';
const NO_DEPRECATED = 'NO_DEPRECATED';
const rule: GraphQLESLintRule<[], true> = {
meta: {
type: 'suggestion',
docs: {
category: 'Best Practices',
description: `Enforce that deprecated fields or enum values are not in use by operations.`,
url: `https://github.com/dotansimha/graphql-eslint/blob/master/docs/rules/no-deprecated.md`,
requiresSchema: true,
examples: [
{
title: 'Incorrect (field)',
code: /* GraphQL */ `
# In your schema
type User {
id: ID!
name: String! @deprecated(reason: "old field, please use fullName instead")
fullName: String!
}
# Query
query user {
user {
name # This is deprecated, so you'll get an error
}
}
`,
},
{
title: 'Incorrect (enum value)',
code: /* GraphQL */ `
# In your schema
type Mutation {
changeSomething(type: SomeType): Boolean!
}
enum SomeType {
NEW
OLD @deprecated(reason: "old field, please use NEW instead")
}
# Mutation
mutation {
changeSomething(
type: OLD # This is deprecated, so you'll get an error
) {
...
}
}
`,
},
{
title: 'Correct',
code: /* GraphQL */ `
# In your schema
type User {
id: ID!
name: String! @deprecated(reason: "old field, please use fullName instead")
fullName: String!
}
# Query
query user {
user {
id
fullName
}
}
`,
},
],
},
messages: {
[NO_DEPRECATED]: `This {{ type }} is marked as deprecated in your GraphQL schema {{ reason }}`,
},
schema: [],
},
create(context) {
return {
EnumValue(node) {
requireGraphQLSchemaFromContext('no-deprecated', context);
const typeInfo = node.typeInfo();
if (typeInfo && typeInfo.enumValue) {
if (typeInfo.enumValue.deprecationReason) {
const enumValueName = node.value;
context.report({
loc: getLocation(node.loc, enumValueName),
messageId: NO_DEPRECATED,
data: {
type: 'enum value',
reason: typeInfo.enumValue.deprecationReason ? `(reason: ${typeInfo.enumValue.deprecationReason})` : '',
},
});
}
}
},
Field(node) {
requireGraphQLSchemaFromContext('no-deprecated', context);
const typeInfo = node.typeInfo();
if (typeInfo && typeInfo.fieldDef) {
if (typeInfo.fieldDef.deprecationReason) {
const fieldName = node.name.value;
context.report({
loc: getLocation(node.loc, fieldName),
messageId: NO_DEPRECATED,
data: {
type: 'field',
reason: typeInfo.fieldDef.deprecationReason ? `(reason: ${typeInfo.fieldDef.deprecationReason})` : '',
},
});
}
}
},
};
},
};
export default rule;