Skip to content

Commit 7c52b9d

Browse files
committed
Split up symbol handling
1 parent f33ba9d commit 7c52b9d

File tree

5 files changed

+50
-18
lines changed

5 files changed

+50
-18
lines changed

src/jsutils/ObjMap.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,21 @@
11
export interface ObjMap<T> {
2-
[key: string | symbol]: T;
2+
[key: string]: T;
33
}
44

5-
export type ObjMapLike<T> = ObjMap<T> | { [key: string | symbol]: T };
5+
export type ObjMapLike<T> = ObjMap<T> | { [key: string]: T };
66

77
export interface ReadOnlyObjMap<T> {
8+
readonly [key: string]: T;
9+
}
10+
11+
export interface ReadOnlyObjMapWithSymbol<T> {
812
readonly [key: string | symbol]: T;
913
}
1014

1115
export type ReadOnlyObjMapLike<T> =
1216
| ReadOnlyObjMap<T>
17+
| { readonly [key: string]: T };
18+
19+
export type ReadOnlyObjMapSymbolLike<T> =
20+
| ReadOnlyObjMapWithSymbol<T>
1321
| { readonly [key: string | symbol]: T };

src/jsutils/toObjMap.ts

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
import type { Maybe } from './Maybe.js';
2-
import type { ReadOnlyObjMap, ReadOnlyObjMapLike } from './ObjMap.js';
2+
import type {
3+
ReadOnlyObjMap,
4+
ReadOnlyObjMapLike,
5+
ReadOnlyObjMapSymbolLike,
6+
ReadOnlyObjMapWithSymbol,
7+
} from './ObjMap.js';
38

49
export function toObjMap<T>(
510
obj: Maybe<ReadOnlyObjMapLike<T>>,
@@ -17,6 +22,25 @@ export function toObjMap<T>(
1722
map[key] = value;
1823
}
1924

25+
return map;
26+
}
27+
28+
export function toObjMapWithSymbols<T>(
29+
obj: Maybe<ReadOnlyObjMapSymbolLike<T>>,
30+
): ReadOnlyObjMapWithSymbol<T> {
31+
if (obj == null) {
32+
return Object.create(null);
33+
}
34+
35+
if (Object.getPrototypeOf(obj) === null) {
36+
return obj;
37+
}
38+
39+
const map = Object.create(null);
40+
for (const [key, value] of Object.entries(obj)) {
41+
map[key] = value;
42+
}
43+
2044
for (const key of Object.getOwnPropertySymbols(obj)) {
2145
map[key] = obj[key];
2246
}

src/type/definition.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import type { ObjMap } from '../jsutils/ObjMap.js';
1111
import type { Path } from '../jsutils/Path.js';
1212
import type { PromiseOrValue } from '../jsutils/PromiseOrValue.js';
1313
import { suggestionList } from '../jsutils/suggestionList.js';
14-
import { toObjMap } from '../jsutils/toObjMap.js';
14+
import { toObjMapWithSymbols } from '../jsutils/toObjMap.js';
1515

1616
import { GraphQLError } from '../error/GraphQLError.js';
1717

@@ -610,7 +610,7 @@ export class GraphQLScalarType<TInternal = unknown, TExternal = TInternal> {
610610
((node, variables) => parseValue(valueFromASTUntyped(node, variables)));
611611
this.coerceInputLiteral = config.coerceInputLiteral;
612612
this.valueToLiteral = config.valueToLiteral;
613-
this.extensions = toObjMap(config.extensions);
613+
this.extensions = toObjMapWithSymbols(config.extensions);
614614
this.astNode = config.astNode;
615615
this.extensionASTNodes = config.extensionASTNodes ?? [];
616616

@@ -783,7 +783,7 @@ export class GraphQLObjectType<TSource = any, TContext = any> {
783783
this.name = assertName(config.name);
784784
this.description = config.description;
785785
this.isTypeOf = config.isTypeOf;
786-
this.extensions = toObjMap(config.extensions);
786+
this.extensions = toObjMapWithSymbols(config.extensions);
787787
this.astNode = config.astNode;
788788
this.extensionASTNodes = config.extensionASTNodes ?? [];
789789
this._fields = (defineFieldMap<TSource, TContext>).bind(
@@ -854,7 +854,7 @@ function defineFieldMap<TSource, TContext>(
854854
resolve: fieldConfig.resolve,
855855
subscribe: fieldConfig.subscribe,
856856
deprecationReason: fieldConfig.deprecationReason,
857-
extensions: toObjMap(fieldConfig.extensions),
857+
extensions: toObjMapWithSymbols(fieldConfig.extensions),
858858
astNode: fieldConfig.astNode,
859859
};
860860
});
@@ -869,7 +869,7 @@ export function defineArguments(
869869
type: argConfig.type,
870870
defaultValue: defineDefaultValue(argName, argConfig),
871871
deprecationReason: argConfig.deprecationReason,
872-
extensions: toObjMap(argConfig.extensions),
872+
extensions: toObjMapWithSymbols(argConfig.extensions),
873873
astNode: argConfig.astNode,
874874
}));
875875
}
@@ -1122,7 +1122,7 @@ export class GraphQLInterfaceType<TSource = any, TContext = any> {
11221122
this.name = assertName(config.name);
11231123
this.description = config.description;
11241124
this.resolveType = config.resolveType;
1125-
this.extensions = toObjMap(config.extensions);
1125+
this.extensions = toObjMapWithSymbols(config.extensions);
11261126
this.astNode = config.astNode;
11271127
this.extensionASTNodes = config.extensionASTNodes ?? [];
11281128
this._fields = (defineFieldMap<TSource, TContext>).bind(
@@ -1247,7 +1247,7 @@ export class GraphQLUnionType {
12471247
this.name = assertName(config.name);
12481248
this.description = config.description;
12491249
this.resolveType = config.resolveType;
1250-
this.extensions = toObjMap(config.extensions);
1250+
this.extensions = toObjMapWithSymbols(config.extensions);
12511251
this.astNode = config.astNode;
12521252
this.extensionASTNodes = config.extensionASTNodes ?? [];
12531253

@@ -1333,7 +1333,7 @@ function enumValuesFromConfig(values: GraphQLEnumValueConfigMap) {
13331333
description: valueConfig.description,
13341334
value: valueConfig.value !== undefined ? valueConfig.value : valueName,
13351335
deprecationReason: valueConfig.deprecationReason,
1336-
extensions: toObjMap(valueConfig.extensions),
1336+
extensions: toObjMapWithSymbols(valueConfig.extensions),
13371337
astNode: valueConfig.astNode,
13381338
}));
13391339
}
@@ -1378,7 +1378,7 @@ export class GraphQLEnumType /* <T> */ {
13781378
constructor(config: Readonly<GraphQLEnumTypeConfig /* <T> */>) {
13791379
this.name = assertName(config.name);
13801380
this.description = config.description;
1381-
this.extensions = toObjMap(config.extensions);
1381+
this.extensions = toObjMapWithSymbols(config.extensions);
13821382
this.astNode = config.astNode;
13831383
this.extensionASTNodes = config.extensionASTNodes ?? [];
13841384

@@ -1626,7 +1626,7 @@ export class GraphQLInputObjectType {
16261626
constructor(config: Readonly<GraphQLInputObjectTypeConfig>) {
16271627
this.name = assertName(config.name);
16281628
this.description = config.description;
1629-
this.extensions = toObjMap(config.extensions);
1629+
this.extensions = toObjMapWithSymbols(config.extensions);
16301630
this.astNode = config.astNode;
16311631
this.extensionASTNodes = config.extensionASTNodes ?? [];
16321632
this.isOneOf = config.isOneOf ?? false;
@@ -1686,7 +1686,7 @@ function defineInputFieldMap(
16861686
type: fieldConfig.type,
16871687
defaultValue: defineDefaultValue(fieldName, fieldConfig),
16881688
deprecationReason: fieldConfig.deprecationReason,
1689-
extensions: toObjMap(fieldConfig.extensions),
1689+
extensions: toObjMapWithSymbols(fieldConfig.extensions),
16901690
astNode: fieldConfig.astNode,
16911691
}));
16921692
}

src/type/directives.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { inspect } from '../jsutils/inspect.js';
22
import { instanceOf } from '../jsutils/instanceOf.js';
33
import type { Maybe } from '../jsutils/Maybe.js';
4-
import { toObjMap } from '../jsutils/toObjMap.js';
4+
import { toObjMapWithSymbols } from '../jsutils/toObjMap.js';
55

66
import type { DirectiveDefinitionNode } from '../language/ast.js';
77
import { DirectiveLocation } from '../language/directiveLocation.js';
@@ -65,7 +65,7 @@ export class GraphQLDirective {
6565
this.description = config.description;
6666
this.locations = config.locations;
6767
this.isRepeatable = config.isRepeatable ?? false;
68-
this.extensions = toObjMap(config.extensions);
68+
this.extensions = toObjMapWithSymbols(config.extensions);
6969
this.astNode = config.astNode;
7070

7171
const args = config.args ?? {};

src/type/schema.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { inspect } from '../jsutils/inspect.js';
22
import { instanceOf } from '../jsutils/instanceOf.js';
33
import type { Maybe } from '../jsutils/Maybe.js';
44
import type { ObjMap } from '../jsutils/ObjMap.js';
5-
import { toObjMap } from '../jsutils/toObjMap.js';
5+
import { toObjMapWithSymbols } from '../jsutils/toObjMap.js';
66

77
import type { GraphQLError } from '../error/GraphQLError.js';
88

@@ -162,7 +162,7 @@ export class GraphQLSchema {
162162
this.__validationErrors = config.assumeValid === true ? [] : undefined;
163163

164164
this.description = config.description;
165-
this.extensions = toObjMap(config.extensions);
165+
this.extensions = toObjMapWithSymbols(config.extensions);
166166
this.astNode = config.astNode;
167167
this.extensionASTNodes = config.extensionASTNodes ?? [];
168168

0 commit comments

Comments
 (0)