Skip to content

Commit 123c041

Browse files
committed
pass assumeValid to constructors
1 parent ef042cf commit 123c041

File tree

4 files changed

+212
-131
lines changed

4 files changed

+212
-131
lines changed

src/type/definition.ts

Lines changed: 60 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -668,8 +668,11 @@ export class GraphQLScalarType<TInternal = unknown, TExternal = TInternal>
668668
astNode: Maybe<ScalarTypeDefinitionNode>;
669669
extensionASTNodes: ReadonlyArray<ScalarTypeExtensionNode>;
670670

671-
constructor(config: Readonly<GraphQLScalarTypeConfig<TInternal, TExternal>>) {
672-
this.name = assertName(config.name);
671+
constructor(
672+
config: Readonly<GraphQLScalarTypeConfig<TInternal, TExternal>>,
673+
assumeValid?: boolean,
674+
) {
675+
this.name = assumeValid ? config.name : assertName(config.name);
673676
this.description = config.description;
674677
this.specifiedByURL = config.specifiedByURL;
675678
this.serialize =
@@ -879,7 +882,10 @@ export class GraphQLObjectType<TSource = any, TContext = any>
879882
private _fields: ThunkObjMap<GraphQLField<TSource, TContext>>;
880883
private _interfaces: ThunkReadonlyArray<GraphQLInterfaceType>;
881884

882-
constructor(config: Readonly<GraphQLObjectTypeConfig<TSource, TContext>>) {
885+
constructor(
886+
config: Readonly<GraphQLObjectTypeConfig<TSource, TContext>>,
887+
assumeValid?: boolean,
888+
) {
883889
this.name = assertName(config.name);
884890
this.description = config.description;
885891
this.isTypeOf = config.isTypeOf;
@@ -890,6 +896,7 @@ export class GraphQLObjectType<TSource = any, TContext = any>
890896
undefined,
891897
this,
892898
config.fields,
899+
assumeValid,
893900
);
894901
this._interfaces = defineInterfaces.bind(undefined, config.interfaces);
895902
}
@@ -945,13 +952,14 @@ function defineFieldMap<TSource, TContext>(
945952
| GraphQLObjectType<TSource, TContext>
946953
| GraphQLInterfaceType<TSource, TContext>,
947954
fields: ThunkObjMap<GraphQLFieldConfig<TSource, TContext>>,
955+
assumeValid: boolean | undefined,
948956
): GraphQLFieldMap<TSource, TContext> {
949957
const fieldMap = resolveObjMapThunk(fields);
950958

951959
return mapValue(
952960
fieldMap,
953961
(fieldConfig, fieldName) =>
954-
new GraphQLField(parentType, fieldName, fieldConfig),
962+
new GraphQLField(parentType, fieldName, fieldConfig, assumeValid),
955963
);
956964
}
957965

@@ -1114,17 +1122,18 @@ export class GraphQLField<TSource = any, TContext = any, TArgs = any>
11141122
| undefined,
11151123
name: string,
11161124
config: GraphQLFieldConfig<TSource, TContext, TArgs>,
1125+
assumeValid?: boolean,
11171126
) {
11181127
this.parentType = parentType;
1119-
this.name = assertName(name);
1128+
this.name = assumeValid ? name : assertName(name);
11201129
this.description = config.description;
11211130
this.type = config.type;
11221131

11231132
const argsConfig = config.args;
11241133
this.args = argsConfig
11251134
? Object.entries(argsConfig).map(
11261135
([argName, argConfig]) =>
1127-
new GraphQLArgument(this, argName, argConfig),
1136+
new GraphQLArgument(this, argName, argConfig, assumeValid),
11281137
)
11291138
: [];
11301139

@@ -1180,9 +1189,10 @@ export class GraphQLArgument implements GraphQLSchemaElement {
11801189
parent: GraphQLField | GraphQLDirective,
11811190
name: string,
11821191
config: GraphQLArgumentConfig,
1192+
assumeValid?: boolean,
11831193
) {
11841194
this.parent = parent;
1185-
this.name = assertName(name);
1195+
this.name = assumeValid ? name : assertName(name);
11861196
this.description = config.description;
11871197
this.type = config.type;
11881198
this.defaultValue = config.defaultValue;
@@ -1280,8 +1290,11 @@ export class GraphQLInterfaceType<TSource = any, TContext = any>
12801290
private _fields: ThunkObjMap<GraphQLField<TSource, TContext>>;
12811291
private _interfaces: ThunkReadonlyArray<GraphQLInterfaceType>;
12821292

1283-
constructor(config: Readonly<GraphQLInterfaceTypeConfig<TSource, TContext>>) {
1284-
this.name = assertName(config.name);
1293+
constructor(
1294+
config: Readonly<GraphQLInterfaceTypeConfig<TSource, TContext>>,
1295+
assumeValid?: boolean,
1296+
) {
1297+
this.name = assumeValid ? config.name : assertName(config.name);
12851298
this.description = config.description;
12861299
this.resolveType = config.resolveType;
12871300
this.extensions = toObjMapWithSymbols(config.extensions);
@@ -1291,6 +1304,7 @@ export class GraphQLInterfaceType<TSource = any, TContext = any>
12911304
undefined,
12921305
this,
12931306
config.fields,
1307+
assumeValid,
12941308
);
12951309
this._interfaces = defineInterfaces.bind(undefined, config.interfaces);
12961310
}
@@ -1406,8 +1420,11 @@ export class GraphQLUnionType implements GraphQLSchemaElement {
14061420

14071421
private _types: ThunkReadonlyArray<GraphQLObjectType>;
14081422

1409-
constructor(config: Readonly<GraphQLUnionTypeConfig<any, any>>) {
1410-
this.name = assertName(config.name);
1423+
constructor(
1424+
config: Readonly<GraphQLUnionTypeConfig<any, any>>,
1425+
assumeValid?: boolean,
1426+
) {
1427+
this.name = assumeValid ? config.name : assertName(config.name);
14111428
this.description = config.description;
14121429
this.resolveType = config.resolveType;
14131430
this.extensions = toObjMapWithSymbols(config.extensions);
@@ -1522,24 +1539,33 @@ export class GraphQLEnumType /* <T> */ implements GraphQLSchemaElement {
15221539

15231540
private _values:
15241541
| ReadonlyArray<GraphQLEnumValue /* <T> */>
1525-
| (() => GraphQLEnumValueConfigMap);
1542+
| (() => ReadonlyArray<GraphQLEnumValue /* <T> */>);
15261543

15271544
private _valueLookup: ReadonlyMap<any /* T */, GraphQLEnumValue> | null;
15281545
private _nameLookup: ObjMap<GraphQLEnumValue> | null;
15291546

1530-
constructor(config: Readonly<GraphQLEnumTypeConfig /* <T> */>) {
1531-
this.name = assertName(config.name);
1547+
constructor(
1548+
config: Readonly<GraphQLEnumTypeConfig /* <T> */>,
1549+
assumeValid?: boolean,
1550+
) {
1551+
this.name = assumeValid ? config.name : assertName(config.name);
15321552
this.description = config.description;
15331553
this.extensions = toObjMapWithSymbols(config.extensions);
15341554
this.astNode = config.astNode;
15351555
this.extensionASTNodes = config.extensionASTNodes ?? [];
15361556

15371557
this._values =
15381558
typeof config.values === 'function'
1539-
? config.values
1559+
? () =>
1560+
Object.entries(
1561+
(config.values as () => GraphQLEnumValueConfigMap)(),
1562+
).map(
1563+
([valueName, valueConfig]) =>
1564+
new GraphQLEnumValue(this, valueName, valueConfig, assumeValid),
1565+
)
15401566
: Object.entries(config.values).map(
15411567
([valueName, valueConfig]) =>
1542-
new GraphQLEnumValue(this, valueName, valueConfig),
1568+
new GraphQLEnumValue(this, valueName, valueConfig, assumeValid),
15431569
);
15441570
this._valueLookup = null;
15451571
this._nameLookup = null;
@@ -1551,10 +1577,7 @@ export class GraphQLEnumType /* <T> */ implements GraphQLSchemaElement {
15511577

15521578
getValues(): ReadonlyArray<GraphQLEnumValue /* <T> */> {
15531579
if (typeof this._values === 'function') {
1554-
this._values = Object.entries(this._values()).map(
1555-
([valueName, valueConfig]) =>
1556-
new GraphQLEnumValue(this, valueName, valueConfig),
1557-
);
1580+
this._values = this._values();
15581581
}
15591582
return this._values;
15601583
}
@@ -1754,9 +1777,10 @@ export class GraphQLEnumValue implements GraphQLSchemaElement {
17541777
parentEnum: GraphQLEnumType,
17551778
name: string,
17561779
config: GraphQLEnumValueConfig,
1780+
assumeValid?: boolean,
17571781
) {
17581782
this.parentEnum = parentEnum;
1759-
this.name = assertEnumValueName(name);
1783+
this.name = assumeValid ? name : assertEnumValueName(name);
17601784
this.description = config.description;
17611785
this.value = config.value !== undefined ? config.value : name;
17621786
this.deprecationReason = config.deprecationReason;
@@ -1831,15 +1855,23 @@ export class GraphQLInputObjectType implements GraphQLSchemaElement {
18311855

18321856
private _fields: ThunkObjMap<GraphQLInputField>;
18331857

1834-
constructor(config: Readonly<GraphQLInputObjectTypeConfig>) {
1835-
this.name = assertName(config.name);
1858+
constructor(
1859+
config: Readonly<GraphQLInputObjectTypeConfig>,
1860+
assumeValid?: boolean,
1861+
) {
1862+
this.name = assumeValid ? config.name : assertName(config.name);
18361863
this.description = config.description;
18371864
this.extensions = toObjMapWithSymbols(config.extensions);
18381865
this.astNode = config.astNode;
18391866
this.extensionASTNodes = config.extensionASTNodes ?? [];
18401867
this.isOneOf = config.isOneOf ?? false;
18411868

1842-
this._fields = defineInputFieldMap.bind(undefined, this, config.fields);
1869+
this._fields = defineInputFieldMap.bind(
1870+
undefined,
1871+
this,
1872+
config.fields,
1873+
assumeValid,
1874+
);
18431875
}
18441876

18451877
get [Symbol.toStringTag]() {
@@ -1877,12 +1909,13 @@ export class GraphQLInputObjectType implements GraphQLSchemaElement {
18771909
function defineInputFieldMap(
18781910
parentType: GraphQLInputObjectType,
18791911
fields: ThunkObjMap<GraphQLInputFieldConfig>,
1912+
assumeValid: boolean | undefined,
18801913
): GraphQLInputFieldMap {
18811914
const fieldMap = resolveObjMapThunk(fields);
18821915
return mapValue(
18831916
fieldMap,
18841917
(fieldConfig, fieldName) =>
1885-
new GraphQLInputField(parentType, fieldName, fieldConfig),
1918+
new GraphQLInputField(parentType, fieldName, fieldConfig, assumeValid),
18861919
);
18871920
}
18881921

@@ -1953,14 +1986,15 @@ export class GraphQLInputField implements GraphQLSchemaElement {
19531986
parentType: GraphQLInputObjectType,
19541987
name: string,
19551988
config: GraphQLInputFieldConfig,
1989+
assumeValid?: boolean,
19561990
) {
19571991
devAssert(
19581992
!('resolve' in config),
19591993
`${parentType}.${name} field has a resolve property, but Input Types cannot define resolvers.`,
19601994
);
19611995

19621996
this.parentType = parentType;
1963-
this.name = assertName(name);
1997+
this.name = assumeValid ? name : assertName(name);
19641998
this.description = config.description;
19651999
this.type = config.type;
19662000
this.defaultValue = config.defaultValue;

src/type/directives.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@ export class GraphQLDirective implements GraphQLSchemaElement {
6161
extensions: Readonly<GraphQLDirectiveExtensions>;
6262
astNode: Maybe<DirectiveDefinitionNode>;
6363

64-
constructor(config: Readonly<GraphQLDirectiveConfig>) {
65-
this.name = assertName(config.name);
64+
constructor(config: Readonly<GraphQLDirectiveConfig>, assumeValid?: boolean) {
65+
this.name = assumeValid ? config.name : assertName(config.name);
6666
this.description = config.description;
6767
this.locations = config.locations;
6868
this.isRepeatable = config.isRepeatable ?? false;
@@ -81,7 +81,8 @@ export class GraphQLDirective implements GraphQLSchemaElement {
8181
);
8282

8383
this.args = Object.entries(args).map(
84-
([argName, argConfig]) => new GraphQLArgument(this, argName, argConfig),
84+
([argName, argConfig]) =>
85+
new GraphQLArgument(this, argName, argConfig, assumeValid),
8586
);
8687
}
8788

0 commit comments

Comments
 (0)