Skip to content

Commit

Permalink
Added diagnostic for unsupported operations.
Browse files Browse the repository at this point in the history
  • Loading branch information
cutsoy committed Jan 15, 2023
1 parent 030cb55 commit 568ea5a
Show file tree
Hide file tree
Showing 11 changed files with 137 additions and 0 deletions.
4 changes: 4 additions & 0 deletions litho-diagnostics/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -542,6 +542,10 @@ diagnostics! {
"Fragment `{fragment}` is used here ..." @ span,
"... and it requires variable `${name}` to be type `{expected}` here ..." @ second,
"... but variable `${name}` is defined here as type `{ty}`." @ first
},
E0329 => UnsupportedOperation @ span + name {
"Operation must be defined in schema before it can be used.",
"Schema doesn't have a `{name}` type. You might be interested in using `@litho(url: \"...\")` to automatically import your existing schema. Learn more at https://litho.dev/docs/operations/import-schemas/." @ span
}
}

Expand Down
9 changes: 9 additions & 0 deletions litho-tests/snapshots/E0005
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,12 @@
· ─┬
· ╰── Missing name of this field here.
───╯


[E0329] Error: Operation must be defined in schema before it can be used.
╭─[graphql:1:1]
1 │ query Example {
· ──┬──
· ╰──── Schema doesn't have a `Query` type. You might be interested in using `@litho(url: "...")` to automatically import your existing schema. Learn more at https://litho.dev/docs/operations/import-schemas/.
───╯
9 changes: 9 additions & 0 deletions litho-tests/snapshots/E0011
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,12 @@
· ┬
· ╰── Fragment definition here does not have a type condition.
───╯


[E0329] Error: Operation must be defined in schema before it can be used.
╭─[graphql:5:1]
5 │ query {
· ──┬──
· ╰──── Schema doesn't have a `Query` type. You might be interested in using `@litho(url: "...")` to automatically import your existing schema. Learn more at https://litho.dev/docs/operations/import-schemas/.
───╯
9 changes: 9 additions & 0 deletions litho-tests/snapshots/E0013
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,12 @@
· ┬
· ╰── Type condition here does not have a named type.
───╯


[E0329] Error: Operation must be defined in schema before it can be used.
╭─[graphql:5:1]
5 │ query {
· ──┬──
· ╰──── Schema doesn't have a `Query` type. You might be interested in using `@litho(url: "...")` to automatically import your existing schema. Learn more at https://litho.dev/docs/operations/import-schemas/.
───╯
9 changes: 9 additions & 0 deletions litho-tests/snapshots/E0100
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,12 @@
· ─────┬─────
· ╰─────── Type `NonExistent` is referenced to here but never defined.
────╯


[E0329] Error: Operation must be defined in schema before it can be used.
╭─[graphql:9:1]
9 │ query {
· ──┬──
· ╰──── Schema doesn't have a `Query` type. You might be interested in using `@litho(url: "...")` to automatically import your existing schema. Learn more at https://litho.dev/docs/operations/import-schemas/.
───╯
9 changes: 9 additions & 0 deletions litho-tests/snapshots/E0316
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,12 @@
· ────────┬────────
· ╰────────── Fragment `undefinedFragment` is used here but never defined.
───╯


[E0329] Error: Operation must be defined in schema before it can be used.
╭─[graphql:1:1]
1 │ query {
· ──┬──
· ╰──── Schema doesn't have a `Query` type. You might be interested in using `@litho(url: "...")` to automatically import your existing schema. Learn more at https://litho.dev/docs/operations/import-schemas/.
───╯
25 changes: 25 additions & 0 deletions litho-tests/snapshots/E0329
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
[E0329] Error: Operation must be defined in schema before it can be used.
╭─[graphql:1:1]
1 │ query A {
· ──┬──
· ╰──── Schema doesn't have a `Query` type. You might be interested in using `@litho(url: "...")` to automatically import your existing schema. Learn more at https://litho.dev/docs/operations/import-schemas/.
───╯


[E0329] Error: Operation must be defined in schema before it can be used.
╭─[graphql:5:1]
5 │ mutation B {
· ────┬───
· ╰───── Schema doesn't have a `Mutation` type. You might be interested in using `@litho(url: "...")` to automatically import your existing schema. Learn more at https://litho.dev/docs/operations/import-schemas/.
───╯


[E0329] Error: Operation must be defined in schema before it can be used.
╭─[graphql:9:1]
9 │ subscription C {
· ──────┬─────
· ╰─────── Schema doesn't have a `Subscription` type. You might be interested in using `@litho(url: "...")` to automatically import your existing schema. Learn more at https://litho.dev/docs/operations/import-schemas/.
───╯
11 changes: 11 additions & 0 deletions litho-tests/tests/E0329.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
query A {
a
}

mutation B {
b
}

subscription C {
c
}
1 change: 1 addition & 0 deletions litho-validation/src/executable/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,6 @@ where
document.traverse(&fragments::FragmentSpreadIsPossible(database), &mut errors);
document.traverse(&operations::OperationNameUniqueness(database), &mut errors);
document.traverse(&operations::LoneAnonymousOperation(database), &mut errors);
document.traverse(&operations::SupportedOperation(database), &mut errors);
errors
}
2 changes: 2 additions & 0 deletions litho-validation/src/executable/operations/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
mod lone_anonymous_operation;
mod operation_name_uniqueness;
mod supported_operation;

pub use lone_anonymous_operation::LoneAnonymousOperation;
pub use operation_name_uniqueness::OperationNameUniqueness;
pub use supported_operation::SupportedOperation;
49 changes: 49 additions & 0 deletions litho-validation/src/executable/operations/supported_operation.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
use std::hash::Hash;
use std::sync::Arc;

use litho_diagnostics::Diagnostic;
use litho_language::ast::*;
use litho_types::Database;

pub struct SupportedOperation<'a, T>(pub &'a Database<T>)
where
T: Eq + Hash;

impl<'a, T> Visit<'a, T> for SupportedOperation<'a, T>
where
T: Eq + Hash + ToString,
{
type Accumulator = Vec<Diagnostic<Span>>;

fn visit_operation_definition(
&self,
node: &'a Arc<OperationDefinition<T>>,
accumulator: &mut Self::Accumulator,
) {
let Some(selection_set) = node.selection_set.ok() else {
return
};

let Some(ty) = self.0.inference.type_by_selection_set.get(selection_set) else {
return
};

if self.0.type_exists(ty) {
return;
}

let name = match node.ty.as_ref() {
Some(OperationType::Query(_)) | None => "Query",
Some(OperationType::Mutation(_)) => "Mutation",
Some(OperationType::Subscription(_)) => "Subscription",
};

accumulator.push(Diagnostic::unsupported_operation(
name.to_owned(),
node.ty
.as_ref()
.map(|ty| ty.span())
.unwrap_or(selection_set.span()),
))
}
}

0 comments on commit 568ea5a

Please sign in to comment.