Skip to content

Commit

Permalink
isValidNameError: Remove node argument (#2424)
Browse files Browse the repository at this point in the history
If you want to bind error to node please use `locatedError`:
```
const error = isValidNameError(name);
if (error) {
  someFunction(locatedError(error, node));
}
```
  • Loading branch information
IvanGoncharov authored Feb 4, 2020
1 parent 4150d1f commit d72cd3d
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 18 deletions.
6 changes: 4 additions & 2 deletions src/error/locatedError.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import Maybe from '../tsutils/Maybe';

import { ASTNode } from '../language/ast';

import { GraphQLError } from './GraphQLError';
Expand All @@ -9,6 +11,6 @@ import { GraphQLError } from './GraphQLError';
*/
export function locatedError(
originalError: Error | GraphQLError,
nodes: ReadonlyArray<ASTNode>,
path: ReadonlyArray<string | number>,
nodes: ASTNode | ReadonlyArray<ASTNode> | undefined,
path?: Maybe<ReadonlyArray<string | number>>,
): GraphQLError;
4 changes: 2 additions & 2 deletions src/error/locatedError.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import { GraphQLError } from './GraphQLError';
*/
export function locatedError(
originalError: Error | GraphQLError,
nodes: $ReadOnlyArray<ASTNode>,
path: $ReadOnlyArray<string | number>,
nodes: ASTNode | $ReadOnlyArray<ASTNode> | void | null,
path?: ?$ReadOnlyArray<string | number>,
): GraphQLError {
// Note: this uses a brand-check to support GraphQL errors originating from
// other contexts.
Expand Down
5 changes: 3 additions & 2 deletions src/type/validate.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import objectValues from '../polyfills/objectValues';
import inspect from '../jsutils/inspect';

import { GraphQLError } from '../error/GraphQLError';
import { locatedError } from '../error/locatedError';

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

Expand Down Expand Up @@ -188,9 +189,9 @@ function validateName(
node: { +name: string, +astNode: ?ASTNode, ... },
): void {
// Ensure names are valid, however introspection types opt out.
const error = isValidNameError(node.name, node.astNode ?? undefined);
const error = isValidNameError(node.name);
if (error) {
context.addError(error);
context.addError(locatedError(error, node.astNode));
}
}

Expand Down
6 changes: 1 addition & 5 deletions src/utilities/assertValidName.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { GraphQLError } from '../error/GraphQLError';
import { ASTNode } from '../language/ast';

/**
* Upholds the spec rules about naming.
Expand All @@ -9,7 +8,4 @@ export function assertValidName(name: string): string;
/**
* Returns an Error if a name is invalid.
*/
export function isValidNameError(
name: string,
node?: ASTNode | undefined,
): GraphQLError | undefined;
export function isValidNameError(name: string): GraphQLError | undefined;
8 changes: 1 addition & 7 deletions src/utilities/assertValidName.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import devAssert from '../jsutils/devAssert';

import { GraphQLError } from '../error/GraphQLError';
import { type ASTNode } from '../language/ast';

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

Expand All @@ -21,21 +20,16 @@ export function assertValidName(name: string): string {
/**
* Returns an Error if a name is invalid.
*/
export function isValidNameError(
name: string,
node?: ASTNode | void,
): GraphQLError | void {
export function isValidNameError(name: string): GraphQLError | void {
devAssert(typeof name === 'string', 'Expected name to be a string.');
if (name.length > 1 && name[0] === '_' && name[1] === '_') {
return new GraphQLError(
`Name "${name}" must not begin with "__", which is reserved by GraphQL introspection.`,
node,
);
}
if (!NAME_RX.test(name)) {
return new GraphQLError(
`Names must match /^[_a-zA-Z][_a-zA-Z0-9]*$/ but "${name}" does not.`,
node,
);
}
}

0 comments on commit d72cd3d

Please sign in to comment.