Skip to content

Commit aceb38e

Browse files
authored
Use graphql-parser with document minifier (#51)
1 parent 18d181e commit aceb38e

File tree

3 files changed

+106
-3
lines changed

3 files changed

+106
-3
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ graphql-tools = { version = "...", features = "graphql_parser_fork", default-fea
3232

3333
#### Validation Rules
3434

35-
> This comparison is based on `graphql-js` refernece implementation.
35+
> This comparison is based on `graphql-js` reference implementation.
3636
3737
- [x] ExecutableDefinitions (not actually needed)
3838
- [x] UniqueOperationNames

src/ast/operation_visitor.rs

+36-2
Original file line numberDiff line numberDiff line change
@@ -167,9 +167,43 @@ fn visit_definitions<'a, Visitor, UserContext>(
167167
Definition::Operation(operation) => match operation {
168168
OperationDefinition::Query(_) => Some(&context.schema.query_type().name),
169169
OperationDefinition::SelectionSet(_) => Some(&context.schema.query_type().name),
170-
OperationDefinition::Mutation(_) => context.schema.mutation_type().map(|t| &t.name),
170+
OperationDefinition::Mutation(_) => {
171+
context.schema.mutation_type().map(|t| &t.name).or_else(|| {
172+
// Awkward hack but enables me to move forward
173+
// Somehow the `mutation_type()` gives None, even though `Mutation` type is defined in the schema.
174+
if let Some(type_definition) = context.schema.type_by_name("Mutation") {
175+
return match type_definition {
176+
crate::parser::schema::TypeDefinition::Object(object_type) => {
177+
Some(&object_type.name)
178+
}
179+
_ => None,
180+
};
181+
}
182+
183+
None
184+
})
185+
}
171186
OperationDefinition::Subscription(_) => {
172-
context.schema.subscription_type().map(|t| &t.name)
187+
context
188+
.schema
189+
.subscription_type()
190+
.map(|t| &t.name)
191+
.or_else(|| {
192+
// Awkward hack but enables me to move forward
193+
// Somehow the `subscription_type()` gives None, even though `Subscription` type is defined in the schema.
194+
if let Some(type_definition) =
195+
context.schema.type_by_name("Subscription")
196+
{
197+
return match type_definition {
198+
crate::parser::schema::TypeDefinition::Object(object_type) => {
199+
Some(&object_type.name)
200+
}
201+
_ => None,
202+
};
203+
}
204+
205+
None
206+
})
173207
}
174208
},
175209
};

src/validation/rules/fields_on_correct_type.rs

+69
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,12 @@ pub static FIELDS_ON_CORRECT_TYPE_TEST_SCHEMA: &str = "
118118
type Query {
119119
human: Human
120120
}
121+
type Mutation {
122+
deletePetByName(name: String): Pet
123+
}
124+
type Subscription {
125+
onNewPet: Pet
126+
}
121127
";
122128

123129
#[test]
@@ -219,6 +225,69 @@ fn ignores_fields_on_unknown_type() {
219225
assert_eq!(get_messages(&errors).len(), 0);
220226
}
221227

228+
#[test]
229+
fn unknown_query_field() {
230+
use crate::validation::test_utils::*;
231+
232+
let mut plan = create_plan_from_rule(Box::new(FieldsOnCorrectType {}));
233+
let errors = test_operation_with_schema(
234+
"query test {
235+
unknownField
236+
}",
237+
&FIELDS_ON_CORRECT_TYPE_TEST_SCHEMA,
238+
&mut plan,
239+
);
240+
241+
let messages = get_messages(&errors);
242+
assert_eq!(messages.len(), 1);
243+
assert_eq!(
244+
messages,
245+
vec!["Cannot query field \"unknownField\" on type \"Query\"."]
246+
);
247+
}
248+
249+
#[test]
250+
fn unknown_mutation_field() {
251+
use crate::validation::test_utils::*;
252+
253+
let mut plan = create_plan_from_rule(Box::new(FieldsOnCorrectType {}));
254+
let errors = test_operation_with_schema(
255+
"mutation test {
256+
unknownField
257+
}",
258+
&FIELDS_ON_CORRECT_TYPE_TEST_SCHEMA,
259+
&mut plan,
260+
);
261+
262+
let messages = get_messages(&errors);
263+
assert_eq!(messages.len(), 1);
264+
assert_eq!(
265+
messages,
266+
vec!["Cannot query field \"unknownField\" on type \"Mutation\"."]
267+
);
268+
}
269+
270+
#[test]
271+
fn unknown_subscription_field() {
272+
use crate::validation::test_utils::*;
273+
274+
let mut plan = create_plan_from_rule(Box::new(FieldsOnCorrectType {}));
275+
let errors = test_operation_with_schema(
276+
"subscription test {
277+
unknownField
278+
}",
279+
&FIELDS_ON_CORRECT_TYPE_TEST_SCHEMA,
280+
&mut plan,
281+
);
282+
283+
let messages = get_messages(&errors);
284+
assert_eq!(messages.len(), 1);
285+
assert_eq!(
286+
messages,
287+
vec!["Cannot query field \"unknownField\" on type \"Subscription\"."]
288+
);
289+
}
290+
222291
#[test]
223292
fn reports_errors_when_type_is_known_again() {
224293
use crate::validation::test_utils::*;

0 commit comments

Comments
 (0)