Skip to content

Commit 8bd61d7

Browse files
committed
feat: replace instanceOf with unique Symbol checks
1 parent e414279 commit 8bd61d7

File tree

12 files changed

+104
-155
lines changed

12 files changed

+104
-155
lines changed

src/error/GraphQLError.ts

+9
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,22 @@ export interface GraphQLErrorOptions {
2929
extensions?: Maybe<GraphQLErrorExtensions>;
3030
}
3131

32+
const isGraphQLErrorSymbol = Symbol.for('GraphQLError');
33+
34+
export function isGraphQLError(error: unknown): error is GraphQLError {
35+
return (
36+
typeof error === 'object' && error != null && isGraphQLErrorSymbol in error
37+
);
38+
}
39+
3240
/**
3341
* A GraphQLError describes an Error found during the parse, validate, or
3442
* execute phases of performing a GraphQL operation. In addition to a message
3543
* and stack trace, it also includes information about the locations in a
3644
* GraphQL document and/or execution result that correspond to the Error.
3745
*/
3846
export class GraphQLError extends Error {
47+
[isGraphQLErrorSymbol]: true = true;
3948
/**
4049
* An array of `{ line, column }` locations within the source GraphQL document
4150
* which correspond to this error.

src/jsutils/__tests__/instanceOf-test.ts

-79
This file was deleted.

src/jsutils/instanceOf.ts

-54
This file was deleted.

src/language/source.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import { devAssert } from '../jsutils/devAssert';
22
import { inspect } from '../jsutils/inspect';
3-
import { instanceOf } from '../jsutils/instanceOf';
43

54
interface Location {
65
line: number;
76
column: number;
87
}
98

9+
const isSourceSymbol = Symbol.for('Source');
10+
1011
/**
1112
* A representation of source input to GraphQL. The `name` and `locationOffset` parameters are
1213
* optional, but they are useful for clients who store GraphQL documents in source files.
@@ -15,6 +16,7 @@ interface Location {
1516
* The `line` and `column` properties in `locationOffset` are 1-indexed.
1617
*/
1718
export class Source {
19+
[isSourceSymbol]: true = true;
1820
body: string;
1921
name: string;
2022
locationOffset: Location;
@@ -53,5 +55,7 @@ export class Source {
5355
* @internal
5456
*/
5557
export function isSource(source: unknown): source is Source {
56-
return instanceOf(source, Source);
58+
return (
59+
typeof source === 'object' && source != null && isSourceSymbol in source
60+
);
5761
}

src/type/__tests__/definition-test.ts

+1
Original file line numberDiff line numberDiff line change
@@ -937,6 +937,7 @@ describe('Type System: Non-Null', () => {
937937
});
938938

939939
it('rejects a non-type as nullable type of non-null', () => {
940+
// @ts-expect-error
940941
expectNonNull(NonNullScalarType).to.throw(
941942
'Expected Scalar! to be a GraphQL nullable type.',
942943
);

0 commit comments

Comments
 (0)