Skip to content

Commit dccf503

Browse files
feature: Adds NoDeprecatedCustomRule
1 parent bda52c5 commit dccf503

File tree

2 files changed

+429
-0
lines changed

2 files changed

+429
-0
lines changed
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
2+
/**
3+
* No deprecated
4+
*
5+
* A GraphQL document is only valid if all selected fields and all used enum values have not been
6+
* deprecated.
7+
*
8+
* Note: This rule is optional and is not part of the Validation section of the GraphQL
9+
* Specification. The main purpose of this rule is detection of deprecated usages and not
10+
* necessarily to forbid their use when querying a service.
11+
*/
12+
public func NoDeprecatedCustomRule(context: ValidationContext) -> Visitor {
13+
return Visitor(
14+
enter: { node, _, _, _, _ in
15+
if let node = node as? Field {
16+
if
17+
let fieldDef = context.fieldDef,
18+
let deprecationReason = fieldDef.deprecationReason,
19+
let parentType = context.parentType
20+
{
21+
context.report(
22+
error: GraphQLError(
23+
message: "The field \(parentType.name).\(fieldDef.name) is deprecated. \(deprecationReason)",
24+
nodes: [node]
25+
)
26+
)
27+
}
28+
}
29+
if let node = node as? Argument {
30+
if
31+
let argDef = context.argument,
32+
let deprecationReason = argDef.deprecationReason
33+
{
34+
if let directiveDef = context.typeInfo.directive {
35+
context.report(
36+
error: GraphQLError(
37+
message: "Directive \"@\(directiveDef.name)\" argument \"\(argDef.name)\" is deprecated. \(deprecationReason)",
38+
nodes: [node]
39+
)
40+
)
41+
} else if
42+
let fieldDef = context.fieldDef,
43+
let parentType = context.parentType
44+
{
45+
context.report(
46+
error: GraphQLError(
47+
message: "Field \"\(parentType.name).\(fieldDef.name)\" argument \"\(argDef.name)\" is deprecated. \(deprecationReason)",
48+
nodes: [node]
49+
)
50+
)
51+
}
52+
}
53+
}
54+
if let node = node as? ObjectField {
55+
let inputObjectDef = context.parentInputType as? GraphQLInputObjectType
56+
57+
if
58+
let inputObjectDef = context.parentInputType as? GraphQLInputObjectType,
59+
let inputFieldDef = inputObjectDef.fields[node.name.value],
60+
let deprecationReason = inputFieldDef.deprecationReason
61+
{
62+
context.report(
63+
error: GraphQLError(
64+
message: "The input field \(inputObjectDef.name).\(inputFieldDef.name) is deprecated. \(deprecationReason)",
65+
nodes: [node]
66+
)
67+
)
68+
}
69+
}
70+
if let node = node as? EnumValue {
71+
if
72+
let enumValueDef = context.typeInfo.enumValue,
73+
let deprecationReason = enumValueDef.deprecationReason,
74+
let enumTypeDef = getNamedType(type: context.inputType)
75+
{
76+
context.report(
77+
error: GraphQLError(
78+
message: "The enum value \"\(enumTypeDef.name).\(enumValueDef.name)\" is deprecated. \(deprecationReason)",
79+
nodes: [node]
80+
)
81+
)
82+
}
83+
}
84+
return .continue
85+
}
86+
)
87+
}

0 commit comments

Comments
 (0)