Skip to content

Commit

Permalink
Use graphql-parser with document minifier (#51)
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilkisiela authored Nov 13, 2024
1 parent 18d181e commit aceb38e
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 3 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ graphql-tools = { version = "...", features = "graphql_parser_fork", default-fea

#### Validation Rules

> This comparison is based on `graphql-js` refernece implementation.
> This comparison is based on `graphql-js` reference implementation.
- [x] ExecutableDefinitions (not actually needed)
- [x] UniqueOperationNames
Expand Down
38 changes: 36 additions & 2 deletions src/ast/operation_visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,9 +167,43 @@ fn visit_definitions<'a, Visitor, UserContext>(
Definition::Operation(operation) => match operation {
OperationDefinition::Query(_) => Some(&context.schema.query_type().name),
OperationDefinition::SelectionSet(_) => Some(&context.schema.query_type().name),
OperationDefinition::Mutation(_) => context.schema.mutation_type().map(|t| &t.name),
OperationDefinition::Mutation(_) => {
context.schema.mutation_type().map(|t| &t.name).or_else(|| {
// Awkward hack but enables me to move forward
// Somehow the `mutation_type()` gives None, even though `Mutation` type is defined in the schema.
if let Some(type_definition) = context.schema.type_by_name("Mutation") {
return match type_definition {
crate::parser::schema::TypeDefinition::Object(object_type) => {
Some(&object_type.name)
}
_ => None,
};
}

None
})
}
OperationDefinition::Subscription(_) => {
context.schema.subscription_type().map(|t| &t.name)
context
.schema
.subscription_type()
.map(|t| &t.name)
.or_else(|| {
// Awkward hack but enables me to move forward
// Somehow the `subscription_type()` gives None, even though `Subscription` type is defined in the schema.
if let Some(type_definition) =
context.schema.type_by_name("Subscription")
{
return match type_definition {
crate::parser::schema::TypeDefinition::Object(object_type) => {
Some(&object_type.name)
}
_ => None,
};
}

None
})
}
},
};
Expand Down
69 changes: 69 additions & 0 deletions src/validation/rules/fields_on_correct_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,12 @@ pub static FIELDS_ON_CORRECT_TYPE_TEST_SCHEMA: &str = "
type Query {
human: Human
}
type Mutation {
deletePetByName(name: String): Pet
}
type Subscription {
onNewPet: Pet
}
";

#[test]
Expand Down Expand Up @@ -219,6 +225,69 @@ fn ignores_fields_on_unknown_type() {
assert_eq!(get_messages(&errors).len(), 0);
}

#[test]
fn unknown_query_field() {
use crate::validation::test_utils::*;

let mut plan = create_plan_from_rule(Box::new(FieldsOnCorrectType {}));
let errors = test_operation_with_schema(
"query test {
unknownField
}",
&FIELDS_ON_CORRECT_TYPE_TEST_SCHEMA,
&mut plan,
);

let messages = get_messages(&errors);
assert_eq!(messages.len(), 1);
assert_eq!(
messages,
vec!["Cannot query field \"unknownField\" on type \"Query\"."]
);
}

#[test]
fn unknown_mutation_field() {
use crate::validation::test_utils::*;

let mut plan = create_plan_from_rule(Box::new(FieldsOnCorrectType {}));
let errors = test_operation_with_schema(
"mutation test {
unknownField
}",
&FIELDS_ON_CORRECT_TYPE_TEST_SCHEMA,
&mut plan,
);

let messages = get_messages(&errors);
assert_eq!(messages.len(), 1);
assert_eq!(
messages,
vec!["Cannot query field \"unknownField\" on type \"Mutation\"."]
);
}

#[test]
fn unknown_subscription_field() {
use crate::validation::test_utils::*;

let mut plan = create_plan_from_rule(Box::new(FieldsOnCorrectType {}));
let errors = test_operation_with_schema(
"subscription test {
unknownField
}",
&FIELDS_ON_CORRECT_TYPE_TEST_SCHEMA,
&mut plan,
);

let messages = get_messages(&errors);
assert_eq!(messages.len(), 1);
assert_eq!(
messages,
vec!["Cannot query field \"unknownField\" on type \"Subscription\"."]
);
}

#[test]
fn reports_errors_when_type_is_known_again() {
use crate::validation::test_utils::*;
Expand Down

0 comments on commit aceb38e

Please sign in to comment.