Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use graphql-parser with document minifier #51

Merged
merged 1 commit into from
Nov 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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") {
dotansimha marked this conversation as resolved.
Show resolved Hide resolved
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
Loading