Skip to content

Commit d72cd3d

Browse files
isValidNameError: Remove node argument (#2424)
If you want to bind error to node please use `locatedError`: ``` const error = isValidNameError(name); if (error) { someFunction(locatedError(error, node)); } ```
1 parent 4150d1f commit d72cd3d

File tree

5 files changed

+11
-18
lines changed

5 files changed

+11
-18
lines changed

src/error/locatedError.d.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import Maybe from '../tsutils/Maybe';
2+
13
import { ASTNode } from '../language/ast';
24

35
import { GraphQLError } from './GraphQLError';
@@ -9,6 +11,6 @@ import { GraphQLError } from './GraphQLError';
911
*/
1012
export function locatedError(
1113
originalError: Error | GraphQLError,
12-
nodes: ReadonlyArray<ASTNode>,
13-
path: ReadonlyArray<string | number>,
14+
nodes: ASTNode | ReadonlyArray<ASTNode> | undefined,
15+
path?: Maybe<ReadonlyArray<string | number>>,
1416
): GraphQLError;

src/error/locatedError.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ import { GraphQLError } from './GraphQLError';
1111
*/
1212
export function locatedError(
1313
originalError: Error | GraphQLError,
14-
nodes: $ReadOnlyArray<ASTNode>,
15-
path: $ReadOnlyArray<string | number>,
14+
nodes: ASTNode | $ReadOnlyArray<ASTNode> | void | null,
15+
path?: ?$ReadOnlyArray<string | number>,
1616
): GraphQLError {
1717
// Note: this uses a brand-check to support GraphQL errors originating from
1818
// other contexts.

src/type/validate.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import objectValues from '../polyfills/objectValues';
77
import inspect from '../jsutils/inspect';
88

99
import { GraphQLError } from '../error/GraphQLError';
10+
import { locatedError } from '../error/locatedError';
1011

1112
import { type ASTNode, type NamedTypeNode } from '../language/ast';
1213

@@ -188,9 +189,9 @@ function validateName(
188189
node: { +name: string, +astNode: ?ASTNode, ... },
189190
): void {
190191
// Ensure names are valid, however introspection types opt out.
191-
const error = isValidNameError(node.name, node.astNode ?? undefined);
192+
const error = isValidNameError(node.name);
192193
if (error) {
193-
context.addError(error);
194+
context.addError(locatedError(error, node.astNode));
194195
}
195196
}
196197

src/utilities/assertValidName.d.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { GraphQLError } from '../error/GraphQLError';
2-
import { ASTNode } from '../language/ast';
32

43
/**
54
* Upholds the spec rules about naming.
@@ -9,7 +8,4 @@ export function assertValidName(name: string): string;
98
/**
109
* Returns an Error if a name is invalid.
1110
*/
12-
export function isValidNameError(
13-
name: string,
14-
node?: ASTNode | undefined,
15-
): GraphQLError | undefined;
11+
export function isValidNameError(name: string): GraphQLError | undefined;

src/utilities/assertValidName.js

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import devAssert from '../jsutils/devAssert';
44

55
import { GraphQLError } from '../error/GraphQLError';
6-
import { type ASTNode } from '../language/ast';
76

87
const NAME_RX = /^[_a-zA-Z][_a-zA-Z0-9]*$/;
98

@@ -21,21 +20,16 @@ export function assertValidName(name: string): string {
2120
/**
2221
* Returns an Error if a name is invalid.
2322
*/
24-
export function isValidNameError(
25-
name: string,
26-
node?: ASTNode | void,
27-
): GraphQLError | void {
23+
export function isValidNameError(name: string): GraphQLError | void {
2824
devAssert(typeof name === 'string', 'Expected name to be a string.');
2925
if (name.length > 1 && name[0] === '_' && name[1] === '_') {
3026
return new GraphQLError(
3127
`Name "${name}" must not begin with "__", which is reserved by GraphQL introspection.`,
32-
node,
3328
);
3429
}
3530
if (!NAME_RX.test(name)) {
3631
return new GraphQLError(
3732
`Names must match /^[_a-zA-Z][_a-zA-Z0-9]*$/ but "${name}" does not.`,
38-
node,
3933
);
4034
}
4135
}

0 commit comments

Comments
 (0)