-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Expand file tree
/
Copy pathdeprecated_fields.rs
More file actions
203 lines (184 loc) · 8.4 KB
/
Copy pathdeprecated_fields.rs
File metadata and controls
203 lines (184 loc) · 8.4 KB
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
use std::sync::Arc;
use common::Diagnostic;
use common::DiagnosticTag;
use common::DiagnosticsResult;
use common::WithLocation;
use graphql_ir::Argument;
use graphql_ir::ConstantValue;
use graphql_ir::Directive;
use graphql_ir::ExecutableDefinition;
use graphql_ir::LinkedField;
use graphql_ir::Program;
use graphql_ir::ScalarField;
use graphql_ir::ValidationMessage;
use graphql_ir::ValidationMessageWithData;
use graphql_ir::Validator;
use graphql_ir::Value;
use schema::FieldID;
use schema::SDLSchema;
use schema::Schema;
use schema::Type;
use crate::fragment_alias_directive::FRAGMENT_DANGEROUSLY_UNALIAS_DIRECTIVE_NAME;
pub fn deprecated_fields(
schema: &Arc<SDLSchema>,
program: &Program,
) -> DiagnosticsResult<Vec<Diagnostic>> {
let mut validator = DeprecatedFields::new(schema);
validator.validate_program(program)?;
Ok(validator.warnings)
}
pub fn deprecated_fields_for_executable_definition(
schema: &Arc<SDLSchema>,
definition: &ExecutableDefinition,
) -> DiagnosticsResult<Vec<Diagnostic>> {
let mut validator = DeprecatedFields::new(schema);
match definition {
ExecutableDefinition::Fragment(fragment) => validator.validate_fragment(fragment),
ExecutableDefinition::Operation(operation) => validator.validate_operation(operation),
}?;
Ok(validator.warnings)
}
struct DeprecatedFields<'a> {
schema: &'a Arc<SDLSchema>,
warnings: Vec<Diagnostic>,
}
impl<'a> DeprecatedFields<'a> {
fn new(schema: &'a Arc<SDLSchema>) -> Self {
Self {
schema,
warnings: vec![],
}
}
fn validate_field(&mut self, field_id: &WithLocation<FieldID>, arguments: &[Argument]) {
let schema = &self.schema;
let field_definition = schema.field(field_id.item);
if let Some(deprecation) = field_definition.deprecated() {
let parent_type = field_definition.parent_type.unwrap();
let parent_name = schema.get_type_name(parent_type);
self.warnings.push(Diagnostic::hint(
ValidationMessage::DeprecatedField {
field_name: field_definition.name.item,
parent_name,
deprecation_reason: deprecation.reason,
},
field_id.location,
vec![DiagnosticTag::DEPRECATED],
));
}
for arg in arguments {
if let Some(arg_definition) = field_definition.arguments.named(arg.name.item) {
if let Some(directive) = arg_definition.deprecated() {
let parent_type = field_definition.parent_type.unwrap();
let parent_name = schema.get_type_name(parent_type);
self.warnings.push(Diagnostic::hint(
ValidationMessage::DeprecatedFieldArgument {
argument_name: arg.name.item,
field_name: field_definition.name.item,
parent_name,
deprecation_reason: directive.reason,
},
arg.name.location,
vec![DiagnosticTag::DEPRECATED],
));
}
if let Type::Enum(enum_id) = arg_definition.type_.inner() {
let enum_def = schema.enum_(enum_id);
if let Value::Constant(ConstantValue::Enum(value_name)) = &arg.value.item {
if let Some(enum_value) =
enum_def.values.iter().find(|v| v.value == *value_name)
&& let Some(deprecation) = enum_value.deprecated()
{
self.warnings.push(Diagnostic::hint(
ValidationMessage::DeprecatedEnumValue {
enum_value: *value_name,
enum_name: enum_def.name.item.0,
deprecation_reason: deprecation.reason,
},
arg.value.location,
vec![DiagnosticTag::DEPRECATED],
));
}
}
}
}
}
}
}
// While the individual methods return a diagnostic, since using deprecated fields are not errors per-se, we reserve
// returning an `Err` for cases where we are unable to correctly check.
// Deprecation warnings are collected in `self.warnings`.
impl Validator for DeprecatedFields<'_> {
const NAME: &'static str = "DeprecatedFields";
const VALIDATE_ARGUMENTS: bool = false;
const VALIDATE_DIRECTIVES: bool = true;
fn validate_linked_field(&mut self, field: &LinkedField) -> DiagnosticsResult<()> {
self.validate_field(&field.definition, &field.arguments);
self.default_validate_linked_field(field)
}
fn validate_scalar_field(&mut self, field: &ScalarField) -> DiagnosticsResult<()> {
self.validate_field(&field.definition, &field.arguments);
self.default_validate_scalar_field(field)
}
fn validate_value(&mut self, value: &Value) -> DiagnosticsResult<()> {
// TODO: Deprecated enum values inside deeply nested constant objects/arrays
// are not yet validated. Top-level enum values in field and directive arguments
// are handled in validate_field and validate_directive respectively.
self.default_validate_value(value)
}
fn validate_directive(&mut self, directive: &Directive) -> DiagnosticsResult<()> {
if let Some(directive_definition) = self.schema.get_directive(directive.name.item) {
// GraphQL does not support @deprecated on directive definitions,
// but there are some directives that Relay exposes as escape
// hatches which we would like to render as struckthrough in order
// to indicate that they should be avoided or migrated to some other pattern.
if directive_definition.name.item == *FRAGMENT_DANGEROUSLY_UNALIAS_DIRECTIVE_NAME {
self.warnings.push(Diagnostic::hint_with_data(
ValidationMessageWithData::DeprecatedDangerouslyUnaliasedDirective,
directive.location,
vec![DiagnosticTag::DEPRECATED],
));
}
for arg in &directive.arguments {
if let Some(arg_definition) = directive_definition.arguments.named(arg.name.item) {
if let Some(deprecation) = arg_definition.deprecated() {
self.warnings.push(Diagnostic::hint(
ValidationMessage::DeprecatedDirectiveArgument {
argument_name: arg.name.item,
directive_name: directive.name.item,
deprecation_reason: deprecation.reason,
},
arg.name.location,
vec![DiagnosticTag::DEPRECATED],
));
}
if let Type::Enum(enum_id) = arg_definition.type_.inner() {
let enum_def = self.schema.enum_(enum_id);
if let Value::Constant(ConstantValue::Enum(value_name)) = &arg.value.item {
if let Some(enum_value) =
enum_def.values.iter().find(|v| v.value == *value_name)
&& let Some(deprecation) = enum_value.deprecated()
{
self.warnings.push(Diagnostic::hint(
ValidationMessage::DeprecatedEnumValue {
enum_value: *value_name,
enum_name: enum_def.name.item.0,
deprecation_reason: deprecation.reason,
},
arg.value.location,
vec![DiagnosticTag::DEPRECATED],
));
}
}
}
}
}
}
Ok(())
}
}