-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move NoUnusedVariablesRule to RelayTransforms
Reviewed By: josephsavona Differential Revision: D17796064 fbshipit-source-id: 40844ed7b8883616cc1214a9036ea950cd9dcb30
- Loading branch information
1 parent
6946e85
commit 3dd79e8
Showing
28 changed files
with
523 additions
and
89 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
2 changes: 1 addition & 1 deletion
2
...-compiler/codegen/__tests__/fixtures/compileRelayArtifacts/client-fields-on-roots.graphql
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
query FooQuery($id: ID!, $arg: String) { | ||
query FooQuery($id: ID!) { | ||
client_root_field | ||
|
||
node(id: $id) { | ||
|
2 changes: 1 addition & 1 deletion
2
..._/fixtures/compileRelayArtifacts/connection-resolver-field-stream-if-default-true.graphql
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
2 changes: 1 addition & 1 deletion
2
.../fixtures/compileRelayArtifacts/connection-resolver-field-stream-if-explicit-true.graphql
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
2 changes: 1 addition & 1 deletion
2
..._tests__/fixtures/compileRelayArtifacts/unknown-root-variable-in-fragment.invalid.graphql
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
91 changes: 91 additions & 0 deletions
91
packages/relay-compiler/transforms/ValidateUnusedVariablesTransform.js
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,91 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
* @flow strict-local | ||
* @format | ||
*/ | ||
|
||
'use strict'; | ||
|
||
const inferRootArgumentDefinitions = require('../core/inferRootArgumentDefinitions'); | ||
|
||
const { | ||
createCombinedError, | ||
createUserError, | ||
eachWithErrors, | ||
} = require('../core/RelayCompilerError'); | ||
|
||
import type GraphQLCompilerContext from '../core/GraphQLCompilerContext'; | ||
import type {ArgumentDefinition} from '../core/GraphQLIR'; | ||
|
||
const SCHEMA_EXTENSION = | ||
'directive @DEPRECATED__relay_ignore_unused_variables_error on QUERY | MUTATION | SUBSCRIPTION'; | ||
|
||
/** | ||
* Validates that there are no unused variables in the operation. | ||
* former `graphql-js`` NoUnusedVariablesRule | ||
*/ | ||
function validateUnusedVariablesTransform( | ||
context: GraphQLCompilerContext, | ||
): GraphQLCompilerContext { | ||
const contextWithUsedArguments = inferRootArgumentDefinitions(context); | ||
const errors = eachWithErrors(context.documents(), node => { | ||
if (node.kind !== 'Root') { | ||
return; | ||
} | ||
const rootArgumentLocations = new Map( | ||
node.argumentDefinitions.map(arg => [arg.name, arg.loc]), | ||
); | ||
const nodeWithUsedArguments = contextWithUsedArguments.getRoot(node.name); | ||
const usedArguments = argumentDefinitionsToMap( | ||
nodeWithUsedArguments.argumentDefinitions, | ||
); | ||
for (const usedArgumentName of usedArguments.keys()) { | ||
rootArgumentLocations.delete(usedArgumentName); | ||
} | ||
|
||
const ignoreErrorDirective = node.directives.find( | ||
({name}) => name === 'DEPRECATED__relay_ignore_unused_variables_error', | ||
); | ||
if (rootArgumentLocations.size > 0 && !ignoreErrorDirective) { | ||
const isPlural = rootArgumentLocations.size > 1; | ||
throw createUserError( | ||
`Variable${isPlural ? 's' : ''} '$${Array.from( | ||
rootArgumentLocations.keys(), | ||
).join("', '$")}' ${isPlural ? 'are' : 'is'} never used in operation '${ | ||
node.name | ||
}'.`, | ||
Array.from(rootArgumentLocations.values()), | ||
); | ||
} | ||
if (rootArgumentLocations.size === 0 && ignoreErrorDirective) { | ||
throw createUserError( | ||
"Invalid usage of '@DEPRECATED__relay_ignore_unused_variables_error.'" + | ||
`No unused variables found in the query '${node.name}'`, | ||
[ignoreErrorDirective.loc], | ||
); | ||
} | ||
}); | ||
if (errors != null && errors.length !== 0) { | ||
throw createCombinedError(errors); | ||
} | ||
return context; | ||
} | ||
|
||
function argumentDefinitionsToMap<T: ArgumentDefinition>( | ||
argDefs: $ReadOnlyArray<T>, | ||
): Map<string, T> { | ||
const map = new Map(); | ||
for (const argDef of argDefs) { | ||
map.set(argDef.name, argDef); | ||
} | ||
return map; | ||
} | ||
|
||
module.exports = { | ||
transform: validateUnusedVariablesTransform, | ||
SCHEMA_EXTENSION, | ||
}; |
43 changes: 43 additions & 0 deletions
43
packages/relay-compiler/transforms/__tests__/ValidateUnusedVariablesTransform-test.js
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,43 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
* @flow strict-local | ||
* @format | ||
* @emails oncall+relay | ||
*/ | ||
|
||
'use strict'; | ||
|
||
const GraphQLCompilerContext = require('../../core/GraphQLCompilerContext'); | ||
const Schema = require('../../core/Schema'); | ||
const ValidateUnusedVariablesTransform = require('../ValidateUnusedVariablesTransform'); | ||
|
||
const {transformASTSchema} = require('../../core/ASTConvert'); | ||
const { | ||
TestSchema, | ||
generateTestsFromFixtures, | ||
parseGraphQLText, | ||
} = require('relay-test-utils-internal'); | ||
|
||
generateTestsFromFixtures( | ||
`${__dirname}/fixtures/ValidateUnusedVariablesTransform`, | ||
text => { | ||
const extendedSchema = transformASTSchema(TestSchema, [ | ||
ValidateUnusedVariablesTransform.SCHEMA_EXTENSION, | ||
]); | ||
const {definitions} = parseGraphQLText(extendedSchema, text); | ||
const compilerSchema = Schema.DEPRECATED__create( | ||
TestSchema, | ||
extendedSchema, | ||
); | ||
return new GraphQLCompilerContext(compilerSchema) | ||
.addAll(definitions) | ||
.applyTransforms([ValidateUnusedVariablesTransform.transform]) | ||
.documents() | ||
.map(doc => `${doc.name}: NO ERRORS.`) | ||
.join('\n'); | ||
}, | ||
); |
Oops, something went wrong.