-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update all validation rules: tests pass
- Loading branch information
Showing
14 changed files
with
666 additions
and
207 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
120 changes: 120 additions & 0 deletions
120
src/validation/__tests__/NoUnusedFragmentArgumentsRule-test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
import { describe, it } from 'mocha'; | ||
|
||
import { NoUnusedFragmentArgumentsRule } from '../rules/NoUnusedFragmentArgumentsRule.js'; | ||
|
||
import { expectValidationErrors } from './harness.js'; | ||
|
||
function expectErrors(queryStr: string) { | ||
return expectValidationErrors(NoUnusedFragmentArgumentsRule, queryStr); | ||
} | ||
|
||
function expectValid(queryStr: string) { | ||
expectErrors(queryStr).toDeepEqual([]); | ||
} | ||
|
||
describe('Validate: No unused fragment arguments', () => { | ||
it('uses all arguments', () => { | ||
expectValid(` | ||
fragment Foo($a: String, $b: String, $c: String) on Type { | ||
field(a: $a, b: $b, c: $c) | ||
} | ||
`); | ||
}); | ||
|
||
it('uses all arguments deeply', () => { | ||
expectValid(` | ||
fragment Foo($a: String, $b: String, $c: String) on Type { | ||
field(a: $a) { | ||
field(b: $b) { | ||
field(c: $c) | ||
} | ||
} | ||
} | ||
`); | ||
}); | ||
|
||
it('uses all arguments deeply in inline fragments', () => { | ||
expectValid(` | ||
fragment Foo($a: String, $b: String, $c: String) on Type { | ||
... on Type { | ||
field(a: $a) { | ||
field(b: $b) { | ||
... on Type { | ||
field(c: $c) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
`); | ||
}); | ||
|
||
it('argument not used', () => { | ||
expectErrors(` | ||
fragment Foo($a: String, $b: String, $c: String) on Type { | ||
field(a: $a, b: $b) | ||
} | ||
`).toDeepEqual([ | ||
{ | ||
message: 'Argument "$c" is never used in fragment "Foo".', | ||
locations: [{ line: 2, column: 44 }], | ||
}, | ||
]); | ||
}); | ||
|
||
it('query passes in unused argument', () => { | ||
expectErrors(` | ||
query Q($c: String) { | ||
type { | ||
...Foo(a: "", b: "", c: $c) | ||
} | ||
} | ||
fragment Foo($a: String, $b: String, $c: String) on Type { | ||
field(a: $a, b: $b) | ||
} | ||
`).toDeepEqual([ | ||
{ | ||
message: 'Argument "$c" is never used in fragment "Foo".', | ||
locations: [{ line: 7, column: 44 }], | ||
}, | ||
]); | ||
}); | ||
|
||
it('child fragment uses a variable of the same name', () => { | ||
expectErrors(` | ||
query Q($a: String) { | ||
type { | ||
...Foo | ||
} | ||
} | ||
fragment Foo($a: String) on Type { | ||
...Bar | ||
} | ||
fragment Bar on Type { | ||
field(a: $a) | ||
} | ||
`).toDeepEqual([ | ||
{ | ||
message: 'Argument "$a" is never used in fragment "Foo".', | ||
locations: [{ line: 7, column: 20 }], | ||
}, | ||
]); | ||
}); | ||
|
||
it('multiple arguments not used', () => { | ||
expectErrors(` | ||
fragment Foo($a: String, $b: String, $c: String) on Type { | ||
field(b: $b) | ||
} | ||
`).toDeepEqual([ | ||
{ | ||
message: 'Argument "$a" is never used in fragment "Foo".', | ||
locations: [{ line: 2, column: 20 }], | ||
}, | ||
{ | ||
message: 'Argument "$c" is never used in fragment "Foo".', | ||
locations: [{ line: 2, column: 44 }], | ||
}, | ||
]); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { GraphQLError } from '../../error/GraphQLError.js'; | ||
|
||
import type { ASTVisitor } from '../../language/visitor.js'; | ||
|
||
import type { ValidationContext } from '../ValidationContext.js'; | ||
|
||
/** | ||
* No unused variables | ||
* | ||
* A GraphQL fragment is only valid if all arguments defined by it | ||
* are used within the same fragment. | ||
* | ||
* See https://spec.graphql.org/draft/#sec-All-Variables-Used | ||
*/ | ||
export function NoUnusedFragmentArgumentsRule( | ||
context: ValidationContext, | ||
): ASTVisitor { | ||
return { | ||
FragmentDefinition(fragment) { | ||
const usages = context.getVariableUsages(fragment); | ||
const argumentNameUsed = new Set<string>( | ||
usages.map(({ node }) => node.name.value), | ||
); | ||
// FIXME: https://github.com/graphql/graphql-js/issues/2203 | ||
/* c8 ignore next */ | ||
const argumentDefinitions = fragment.arguments ?? []; | ||
for (const argDef of argumentDefinitions) { | ||
const argName = argDef.variable.name.value; | ||
if (!argumentNameUsed.has(argName)) { | ||
context.reportError( | ||
new GraphQLError( | ||
`Argument "$${argName}" is never used in fragment "${fragment.name.value}".`, | ||
{ nodes: argDef }, | ||
), | ||
); | ||
} | ||
} | ||
}, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.