Skip to content

Commit 3ec488e

Browse files
committed
feat: replace instanceOf with unique Symbol checks
1 parent cfbc023 commit 3ec488e

12 files changed

+124
-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,11 +1,12 @@
11
import { devAssert } from '../jsutils/devAssert';
2-
import { instanceOf } from '../jsutils/instanceOf';
32

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

8+
const isSourceSymbol = Symbol.for('Source');
9+
910
/**
1011
* A representation of source input to GraphQL. The `name` and `locationOffset` parameters are
1112
* optional, but they are useful for clients who store GraphQL documents in source files.
@@ -14,6 +15,7 @@ interface Location {
1415
* The `line` and `column` properties in `locationOffset` are 1-indexed.
1516
*/
1617
export class Source {
18+
[isSourceSymbol]: true = true;
1719
body: string;
1820
name: string;
1921
locationOffset: Location;
@@ -47,5 +49,7 @@ export class Source {
4749
* @internal
4850
*/
4951
export function isSource(source: unknown): source is Source {
50-
return instanceOf(source, Source);
52+
return (
53+
typeof source === 'object' && source != null && isSourceSymbol in source
54+
);
5155
}

src/type/__tests__/definition-test.ts

+21
Original file line numberDiff line numberDiff line change
@@ -643,6 +643,27 @@ describe('Type System: Non-Null', () => {
643643
expectNonNull(ListOfScalarsType).to.not.throw();
644644
expectNonNull(ListOfNonNullScalarsType).to.not.throw();
645645
});
646+
647+
it('rejects a non-type as nullable type of non-null', () => {
648+
// @ts-expect-error
649+
expectNonNull(NonNullScalarType).to.throw(
650+
'Expected Scalar! to be a GraphQL nullable type.',
651+
);
652+
// @ts-expect-error
653+
expectNonNull({}).to.throw('Expected {} to be a GraphQL nullable type.');
654+
// @ts-expect-error
655+
expectNonNull(String).to.throw(
656+
'Expected [function String] to be a GraphQL nullable type.',
657+
);
658+
// @ts-expect-error (must provide type)
659+
expectNonNull(null).to.throw(
660+
'Expected null to be a GraphQL nullable type.',
661+
);
662+
// @ts-expect-error (must provide type)
663+
expectNonNull(undefined).to.throw(
664+
'Expected undefined to be a GraphQL nullable type.',
665+
);
666+
});
646667
});
647668

648669
describe('Type System: test utility methods', () => {

0 commit comments

Comments
 (0)