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

fix(compiler): identify variables in operation directives #888

Merged
merged 4 commits into from
Jul 30, 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
6 changes: 6 additions & 0 deletions crates/apollo-compiler/src/validation/variable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,12 @@ pub(crate) fn validate_unused_variables(
)
})
.collect();

// You're allowed to do `query($var: Int!) @dir(arg: $var) {}`
for used in variables_in_directives(&operation.directives) {
unused_vars.remove(used);
}

let walked = walk_selections(
document,
&operation.selection_set,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
query ExampleQuery($variable: Int) {
directive @track(label: String) on FRAGMENT_DEFINITION
directive @report(label: String) on QUERY | MUTATION | SUBSCRIPTION

query ExampleQuery($variable: Int) @report(label: $queryLabel) {
topProducts(first: $variable) {
name
}
... subFrag
}

fragment subFrag on Query {
fragment subFrag on Query @track(label: $productsLabel) {
topProducts(first: $variable) {
price(setPrice: $value)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,21 @@
Error: variable `$queryLabel` is not defined
╭─[0009_operation_with_undefined_variables_in_fragment.graphql:4:51]
4 │ query ExampleQuery($variable: Int) @report(label: $queryLabel) {
│ ─────┬─────
│ ╰─────── not found in this scope
───╯
Error: variable `$productsLabel` is not defined
╭─[0009_operation_with_undefined_variables_in_fragment.graphql:11:41]
11 │ fragment subFrag on Query @track(label: $productsLabel) {
│ ───────┬──────
│ ╰──────── not found in this scope
────╯
Error: variable `$value` is not defined
╭─[0009_operation_with_undefined_variables_in_fragment.graphql:10:21]
╭─[0009_operation_with_undefined_variables_in_fragment.graphql:13:21]
10 │ price(setPrice: $value)
13 │ price(setPrice: $value)
│ ───┬──
│ ╰──── not found in this scope
────╯
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
directive @x(y: Int!) on QUERY | MUTATION | SUBSCRIPTION
directive @z(f: Boolean) on FRAGMENT_DEFINITION

type Query { field: Boolean }
type Mutation { field: Boolean }
type Subscription { field: Boolean }

query Q ($a: Int!, $f: Boolean) @x(y: $a) { field ...f }
mutation M ($b: Int!) @x(y: $b) { field }
subscription S ($c: Int!) @x(y: $c) { field }

fragment f on Query @z(f: $f) { field }
Loading