Skip to content

Commit 1d85e45

Browse files
Define 'toStringTag' directly inside class definitions (#2367)
1 parent f9349e8 commit 1d85e45

File tree

6 files changed

+67
-53
lines changed

6 files changed

+67
-53
lines changed

src/jsutils/defineToStringTag.js

Lines changed: 0 additions & 25 deletions
This file was deleted.

src/language/source.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
// @flow strict
22

3+
import { SYMBOL_TO_STRING_TAG } from '../polyfills/symbols';
4+
35
import devAssert from '../jsutils/devAssert';
4-
import defineToStringTag from '../jsutils/defineToStringTag';
56

67
type Location = {|
78
line: number,
@@ -34,7 +35,9 @@ export class Source {
3435
'column in locationOffset is 1-indexed and must be positive.',
3536
);
3637
}
37-
}
3838

39-
// Conditionally apply `[Symbol.toStringTag]` if `Symbol`s are supported
40-
defineToStringTag(Source);
39+
// $FlowFixMe Flow doesn't support computed properties yet
40+
get [SYMBOL_TO_STRING_TAG]() {
41+
return 'Source';
42+
}
43+
}

src/polyfills/symbols.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,8 @@ export const SYMBOL_ITERATOR: string =
1010
export const SYMBOL_ASYNC_ITERATOR: string =
1111
// $FlowFixMe Flow doesn't define `Symbol.asyncIterator` yet
1212
typeof Symbol === 'function' ? Symbol.asyncIterator : '@@asyncIterator';
13+
14+
/* istanbul ignore next (See: https://github.com/graphql/graphql-js/issues/2317) */
15+
export const SYMBOL_TO_STRING_TAG: string =
16+
// $FlowFixMe Flow doesn't define `Symbol.toStringTag` yet
17+
typeof Symbol === 'function' ? Symbol.toStringTag : '@@toStringTag';

src/type/definition.js

Lines changed: 43 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// @flow strict
22

33
import objectEntries from '../polyfills/objectEntries';
4+
import { SYMBOL_TO_STRING_TAG } from '../polyfills/symbols';
45

56
import inspect from '../jsutils/inspect';
67
import keyMap from '../jsutils/keyMap';
@@ -15,7 +16,6 @@ import isObjectLike from '../jsutils/isObjectLike';
1516
import identityFunc from '../jsutils/identityFunc';
1617
import defineToJSON from '../jsutils/defineToJSON';
1718
import suggestionList from '../jsutils/suggestionList';
18-
import defineToStringTag from '../jsutils/defineToStringTag';
1919
import { type PromiseOrValue } from '../jsutils/PromiseOrValue';
2020
import {
2121
type ObjMap,
@@ -367,8 +367,12 @@ export function GraphQLList(ofType) {
367367
return '[' + String(this.ofType) + ']';
368368
};
369369

370-
// Conditionally apply `[Symbol.toStringTag]` if `Symbol`s are supported
371-
defineToStringTag(GraphQLList);
370+
Object.defineProperty(GraphQLList.prototype, SYMBOL_TO_STRING_TAG, {
371+
get() {
372+
return 'GraphQLList';
373+
},
374+
});
375+
372376
defineToJSON(GraphQLList);
373377

374378
/**
@@ -411,8 +415,12 @@ export function GraphQLNonNull(ofType) {
411415
return String(this.ofType) + '!';
412416
};
413417

414-
// Conditionally apply `[Symbol.toStringTag]` if `Symbol`s are supported
415-
defineToStringTag(GraphQLNonNull);
418+
Object.defineProperty(GraphQLNonNull.prototype, SYMBOL_TO_STRING_TAG, {
419+
get() {
420+
return 'GraphQLNonNull';
421+
},
422+
});
423+
416424
defineToJSON(GraphQLNonNull);
417425

418426
/**
@@ -608,10 +616,13 @@ export class GraphQLScalarType {
608616
toString(): string {
609617
return this.name;
610618
}
619+
620+
// $FlowFixMe Flow doesn't support computed properties yet
621+
get [SYMBOL_TO_STRING_TAG]() {
622+
return 'GraphQLScalarType';
623+
}
611624
}
612625
613-
// Conditionally apply `[Symbol.toStringTag]` if `Symbol`s are supported
614-
defineToStringTag(GraphQLScalarType);
615626
defineToJSON(GraphQLScalarType);
616627
617628
export type GraphQLScalarSerializer<TExternal> = (
@@ -743,10 +754,13 @@ export class GraphQLObjectType {
743754
toString(): string {
744755
return this.name;
745756
}
757+
758+
// $FlowFixMe Flow doesn't support computed properties yet
759+
get [SYMBOL_TO_STRING_TAG]() {
760+
return 'GraphQLObjectType';
761+
}
746762
}
747763

748-
// Conditionally apply `[Symbol.toStringTag]` if `Symbol`s are supported
749-
defineToStringTag(GraphQLObjectType);
750764
defineToJSON(GraphQLObjectType);
751765

752766
function defineInterfaces(
@@ -1050,10 +1064,13 @@ export class GraphQLInterfaceType {
10501064
toString(): string {
10511065
return this.name;
10521066
}
1067+
1068+
// $FlowFixMe Flow doesn't support computed properties yet
1069+
get [SYMBOL_TO_STRING_TAG]() {
1070+
return 'GraphQLInterfaceType';
1071+
}
10531072
}
10541073

1055-
// Conditionally apply `[Symbol.toStringTag]` if `Symbol`s are supported
1056-
defineToStringTag(GraphQLInterfaceType);
10571074
defineToJSON(GraphQLInterfaceType);
10581075

10591076
export type GraphQLInterfaceTypeConfig<TSource, TContext> = {|
@@ -1149,10 +1166,13 @@ export class GraphQLUnionType {
11491166
toString(): string {
11501167
return this.name;
11511168
}
1169+
1170+
// $FlowFixMe Flow doesn't support computed properties yet
1171+
get [SYMBOL_TO_STRING_TAG]() {
1172+
return 'GraphQLUnionType';
1173+
}
11521174
}
11531175

1154-
// Conditionally apply `[Symbol.toStringTag]` if `Symbol`s are supported
1155-
defineToStringTag(GraphQLUnionType);
11561176
defineToJSON(GraphQLUnionType);
11571177

11581178
function defineTypes(
@@ -1319,10 +1339,13 @@ export class GraphQLEnumType /* <T> */ {
13191339
toString(): string {
13201340
return this.name;
13211341
}
1342+
1343+
// $FlowFixMe Flow doesn't support computed properties yet
1344+
get [SYMBOL_TO_STRING_TAG]() {
1345+
return 'GraphQLEnumType';
1346+
}
13221347
}
13231348

1324-
// Conditionally apply `[Symbol.toStringTag]` if `Symbol`s are supported
1325-
defineToStringTag(GraphQLEnumType);
13261349
defineToJSON(GraphQLEnumType);
13271350

13281351
function didYouMeanEnumValue(
@@ -1468,10 +1491,13 @@ export class GraphQLInputObjectType {
14681491
toString(): string {
14691492
return this.name;
14701493
}
1494+
1495+
// $FlowFixMe Flow doesn't support computed properties yet
1496+
get [SYMBOL_TO_STRING_TAG]() {
1497+
return 'GraphQLInputObjectType';
1498+
}
14711499
}
14721500

1473-
// Conditionally apply `[Symbol.toStringTag]` if `Symbol`s are supported
1474-
defineToStringTag(GraphQLInputObjectType);
14751501
defineToJSON(GraphQLInputObjectType);
14761502

14771503
function defineInputFieldMap(

src/type/directives.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
// @flow strict
22

33
import objectEntries from '../polyfills/objectEntries';
4+
import { SYMBOL_TO_STRING_TAG } from '../polyfills/symbols';
45

56
import inspect from '../jsutils/inspect';
67
import toObjMap from '../jsutils/toObjMap';
78
import devAssert from '../jsutils/devAssert';
89
import instanceOf from '../jsutils/instanceOf';
910
import defineToJSON from '../jsutils/defineToJSON';
1011
import isObjectLike from '../jsutils/isObjectLike';
11-
import defineToStringTag from '../jsutils/defineToStringTag';
1212
import {
1313
type ReadOnlyObjMap,
1414
type ReadOnlyObjMapLike,
@@ -111,10 +111,13 @@ export class GraphQLDirective {
111111
toString(): string {
112112
return '@' + this.name;
113113
}
114+
115+
// $FlowFixMe Flow doesn't support computed properties yet
116+
get [SYMBOL_TO_STRING_TAG]() {
117+
return 'GraphQLDirective';
118+
}
114119
}
115120

116-
// Conditionally apply `[Symbol.toStringTag]` if `Symbol`s are supported
117-
defineToStringTag(GraphQLDirective);
118121
defineToJSON(GraphQLDirective);
119122

120123
export type GraphQLDirectiveConfig = {|

src/type/schema.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22

33
import find from '../polyfills/find';
44
import objectValues from '../polyfills/objectValues';
5+
import { SYMBOL_TO_STRING_TAG } from '../polyfills/symbols';
56

67
import inspect from '../jsutils/inspect';
78
import toObjMap from '../jsutils/toObjMap';
89
import devAssert from '../jsutils/devAssert';
910
import instanceOf from '../jsutils/instanceOf';
1011
import isObjectLike from '../jsutils/isObjectLike';
11-
import defineToStringTag from '../jsutils/defineToStringTag';
1212
import {
1313
type ObjMap,
1414
type ReadOnlyObjMap,
@@ -283,10 +283,12 @@ export class GraphQLSchema {
283283
assumeValid: this.__validationErrors !== undefined,
284284
};
285285
}
286-
}
287286

288-
// Conditionally apply `[Symbol.toStringTag]` if `Symbol`s are supported
289-
defineToStringTag(GraphQLSchema);
287+
// $FlowFixMe Flow doesn't support computed properties yet
288+
get [SYMBOL_TO_STRING_TAG]() {
289+
return 'GraphQLSchema';
290+
}
291+
}
290292

291293
type TypeMap = ObjMap<GraphQLNamedType>;
292294

0 commit comments

Comments
 (0)