forked from graphql/graphql-js
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
94 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/** | ||
* Copyright (c) 2015, Facebook, Inc. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. An additional grant | ||
* of patent rights can be found in the PATENTS file in the same directory. | ||
*/ | ||
|
||
import { describe, it } from 'mocha'; | ||
import { expectPassesRule, expectFailsRule } from './harness'; | ||
import { | ||
UniqueVariableNames, | ||
duplicateVariableMessage, | ||
} from '../rules/UniqueVariableNames'; | ||
|
||
|
||
function duplicateVariable(name, l1, c1, l2, c2) { | ||
return { | ||
message: duplicateVariableMessage(name), | ||
locations: [ { line: l1, column: c1 }, { line: l2, column: c2 } ], | ||
}; | ||
} | ||
|
||
describe('Validate: Unique variable names', () => { | ||
|
||
it('unique variable names', () => { | ||
expectPassesRule(UniqueVariableNames, ` | ||
query A($x: Int, $y: String) { __typename } | ||
query B($x: String, $y: Int) { __typename } | ||
`); | ||
}); | ||
|
||
it('duplicate variable names', () => { | ||
expectFailsRule(UniqueVariableNames, ` | ||
query A($x: Int, $x: Int, $x: String) { __typename } | ||
query B($x: String, $x: Int) { __typename } | ||
query C($x: Int, $x: Int) { __typename } | ||
`, [ | ||
duplicateVariable('x', 2, 16, 2, 25), | ||
duplicateVariable('x', 2, 16, 2, 34), | ||
duplicateVariable('x', 3, 16, 3, 28), | ||
duplicateVariable('x', 4, 16, 4, 25) | ||
]); | ||
}); | ||
|
||
}); |
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 @@ | ||
/* @flow */ | ||
/** | ||
* Copyright (c) 2015, Facebook, Inc. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. An additional grant | ||
* of patent rights can be found in the PATENTS file in the same directory. | ||
*/ | ||
|
||
import type { ValidationContext } from '../index'; | ||
import type { VariableDefinition } from '../../language/ast'; | ||
import { GraphQLError } from '../../error'; | ||
|
||
|
||
export function duplicateVariableMessage(variableName: string): string { | ||
return `There can be only one variable named "${variableName}".`; | ||
} | ||
|
||
/** | ||
* Unique variable names | ||
* | ||
* A GraphQL operation is only valid if all its variables are uniquely named. | ||
*/ | ||
export function UniqueVariableNames(context: ValidationContext): any { | ||
let knownVariableNames = Object.create(null); | ||
return { | ||
OperationDefinition() { | ||
knownVariableNames = Object.create(null); | ||
}, | ||
VariableDefinition(node: VariableDefinition) { | ||
const variableName = node.variable.name.value; | ||
if (knownVariableNames[variableName]) { | ||
context.reportError(new GraphQLError( | ||
duplicateVariableMessage(variableName), | ||
[ knownVariableNames[variableName], node.variable.name ] | ||
)); | ||
} else { | ||
knownVariableNames[variableName] = node.variable.name; | ||
} | ||
} | ||
}; | ||
} |
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