Skip to content

Commit f9a2877

Browse files
committed
Move name assertion to definition time
1 parent 4ff563e commit f9a2877

File tree

2 files changed

+20
-11
lines changed

2 files changed

+20
-11
lines changed

src/type/definition.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,7 @@ export class GraphQLScalarType/* <T> */ {
200200

201201
constructor(config: GraphQLScalarTypeConfig/* <T> */) {
202202
invariant(config.name, 'Type must be named.');
203+
assertValidName(config.name);
203204
this.name = config.name;
204205
this.description = config.description;
205206
invariant(
@@ -296,6 +297,7 @@ export class GraphQLObjectType {
296297

297298
constructor(config: GraphQLObjectTypeConfig) {
298299
invariant(config.name, 'Type must be named.');
300+
assertValidName(config.name);
299301
this.name = config.name;
300302
this.description = config.description;
301303
if (config.isTypeOf) {
@@ -381,6 +383,7 @@ function defineFieldMap(
381383

382384
var resultFieldMap = {};
383385
fieldNames.forEach(fieldName => {
386+
assertValidName(fieldName);
384387
var field = {
385388
...fieldMap[fieldName],
386389
name: fieldName
@@ -399,6 +402,7 @@ function defineFieldMap(
399402
`as keys.`
400403
);
401404
field.args = Object.keys(field.args).map(argName => {
405+
assertValidName(argName);
402406
var arg = field.args[argName];
403407
invariant(
404408
isInputType(arg.type),
@@ -537,6 +541,7 @@ export class GraphQLInterfaceType {
537541

538542
constructor(config: GraphQLInterfaceTypeConfig) {
539543
invariant(config.name, 'Type must be named.');
544+
assertValidName(config.name);
540545
this.name = config.name;
541546
this.description = config.description;
542547
if (config.resolveType) {
@@ -638,6 +643,7 @@ export class GraphQLUnionType {
638643

639644
constructor(config: GraphQLUnionTypeConfig) {
640645
invariant(config.name, 'Type must be named.');
646+
assertValidName(config.name);
641647
this.name = config.name;
642648
this.description = config.description;
643649
if (config.resolveType) {
@@ -742,6 +748,7 @@ export class GraphQLEnumType/* <T> */ {
742748

743749
constructor(config: GraphQLEnumTypeConfig/* <T> */) {
744750
this.name = config.name;
751+
assertValidName(config.name);
745752
this.description = config.description;
746753
this._values = defineEnumValues(this, config.values);
747754
this._enumConfig = config;
@@ -813,6 +820,7 @@ function defineEnumValues(
813820
`${type} values must be an object with value names as keys.`
814821
);
815822
return valueNames.map(valueName => {
823+
assertValidName(valueName);
816824
var value = valueMap[valueName];
817825
invariant(
818826
isPlainObj(value),
@@ -881,6 +889,7 @@ export class GraphQLInputObjectType {
881889

882890
constructor(config: InputObjectConfig) {
883891
invariant(config.name, 'Type must be named.');
892+
assertValidName(config.name);
884893
this.name = config.name;
885894
this.description = config.description;
886895
this._typeConfig = config;
@@ -905,6 +914,7 @@ export class GraphQLInputObjectType {
905914
);
906915
var resultFieldMap = {};
907916
fieldNames.forEach(fieldName => {
917+
assertValidName(fieldName);
908918
var field = {
909919
...fieldMap[fieldName],
910920
name: fieldName
@@ -1025,3 +1035,13 @@ export class GraphQLNonNull {
10251035
return this.ofType.toString() + '!';
10261036
}
10271037
}
1038+
1039+
var NAME_RX = /^[_a-zA-Z][_a-zA-Z0-9]*$/;
1040+
1041+
// Helper to assert that provided names are valid.
1042+
function assertValidName(name: string): void {
1043+
invariant(
1044+
NAME_RX.test(name),
1045+
`Names must match /^[_a-zA-Z][_a-zA-Z0-9]*$/ but "${name}" does not.`
1046+
);
1047+
}

src/type/schema.js

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -147,13 +147,9 @@ function typeMapReducer(map: TypeMap, type: ?GraphQLType): TypeMap {
147147
var fieldMap = type.getFields();
148148
Object.keys(fieldMap).forEach(fieldName => {
149149
var field = fieldMap[fieldName];
150-
assertValidName(fieldName);
151150

152151
if (field.args) {
153152
var fieldArgTypes = field.args.map(arg => arg.type);
154-
155-
field.args.forEach(arg => assertValidName(arg.name));
156-
157153
reducedMap = fieldArgTypes.reduce(typeMapReducer, reducedMap);
158154
}
159155
reducedMap = typeMapReducer(reducedMap, field.type);
@@ -163,13 +159,6 @@ function typeMapReducer(map: TypeMap, type: ?GraphQLType): TypeMap {
163159
return reducedMap;
164160
}
165161

166-
function assertValidName(name: string): void {
167-
invariant(
168-
/^[_a-zA-Z][_a-zA-Z0-9]*$/.test(name),
169-
`Names must match /^[_a-zA-Z][_a-zA-Z0-9]*$/ ` +
170-
`but "${name}" does not.`);
171-
}
172-
173162
function assertObjectImplementsInterface(
174163
object: GraphQLObjectType,
175164
iface: GraphQLInterfaceType

0 commit comments

Comments
 (0)