Skip to content

Commit 7fe9985

Browse files
Remove usage of Flow placeholder argument + cleanup (#1669)
1 parent da61380 commit 7fe9985

File tree

1 file changed

+106
-86
lines changed

1 file changed

+106
-86
lines changed

src/utilities/buildASTSchema.js

Lines changed: 106 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import { Kind } from '../language/kinds';
2424

2525
import type {
2626
DocumentNode,
27+
NameNode,
2728
TypeNode,
2829
NamedTypeNode,
2930
SchemaDefinitionNode,
@@ -49,8 +50,9 @@ import type {
4950
GraphQLType,
5051
GraphQLNamedType,
5152
GraphQLFieldConfig,
53+
GraphQLArgumentConfig,
5254
GraphQLEnumValueConfig,
53-
GraphQLInputField,
55+
GraphQLInputFieldConfig,
5456
} from '../type/definition';
5557

5658
import {
@@ -256,17 +258,17 @@ export class ASTDefinitionBuilder {
256258
return this.buildType(typeNode);
257259
}
258260

259-
buildDirective(directiveNode: DirectiveDefinitionNode): GraphQLDirective {
261+
buildDirective(directive: DirectiveDefinitionNode): GraphQLDirective {
262+
const locations = directive.locations.map(
263+
({ value }) => ((value: any): DirectiveLocationEnum),
264+
);
265+
260266
return new GraphQLDirective({
261-
name: directiveNode.name.value,
262-
description: getDescription(directiveNode, this._options),
263-
locations: directiveNode.locations.map(
264-
node => ((node.value: any): DirectiveLocationEnum),
265-
),
266-
args:
267-
directiveNode.arguments &&
268-
this._makeInputValues(directiveNode.arguments),
269-
astNode: directiveNode,
267+
name: directive.name.value,
268+
description: getDescription(directive, this._options),
269+
locations,
270+
args: keyByNameNode(directive.arguments || [], arg => this.buildArg(arg)),
271+
astNode: directive,
270272
});
271273
}
272274

@@ -277,19 +279,31 @@ export class ASTDefinitionBuilder {
277279
// with validateSchema() will produce more actionable results.
278280
type: (this._buildWrappedType(field.type): any),
279281
description: getDescription(field, this._options),
280-
args: field.arguments && this._makeInputValues(field.arguments),
282+
args: keyByNameNode(field.arguments || [], arg => this.buildArg(arg)),
281283
deprecationReason: getDeprecationReason(field),
282284
astNode: field,
283285
};
284286
}
285287

286-
buildInputField(value: InputValueDefinitionNode): GraphQLInputField {
288+
buildArg(value: InputValueDefinitionNode): GraphQLArgumentConfig {
289+
// Note: While this could make assertions to get the correctly typed
290+
// value, that would throw immediately while type system validation
291+
// with validateSchema() will produce more actionable results.
292+
const type: any = this._buildWrappedType(value.type);
293+
return {
294+
type,
295+
description: getDescription(value, this._options),
296+
defaultValue: valueFromAST(value.defaultValue, type),
297+
astNode: value,
298+
};
299+
}
300+
301+
buildInputField(value: InputValueDefinitionNode): GraphQLInputFieldConfig {
287302
// Note: While this could make assertions to get the correctly typed
288303
// value, that would throw immediately while type system validation
289304
// with validateSchema() will produce more actionable results.
290305
const type: any = this._buildWrappedType(value.type);
291306
return {
292-
name: value.name.value,
293307
type,
294308
description: getDescription(value, this._options),
295309
defaultValue: valueFromAST(value.defaultValue, type),
@@ -305,121 +319,127 @@ export class ASTDefinitionBuilder {
305319
};
306320
}
307321

308-
_makeSchemaDef(def: TypeDefinitionNode): GraphQLNamedType {
309-
switch (def.kind) {
322+
_makeSchemaDef(astNode: TypeDefinitionNode): GraphQLNamedType {
323+
switch (astNode.kind) {
310324
case Kind.OBJECT_TYPE_DEFINITION:
311-
return this._makeTypeDef(def);
325+
return this._makeTypeDef(astNode);
312326
case Kind.INTERFACE_TYPE_DEFINITION:
313-
return this._makeInterfaceDef(def);
327+
return this._makeInterfaceDef(astNode);
314328
case Kind.ENUM_TYPE_DEFINITION:
315-
return this._makeEnumDef(def);
329+
return this._makeEnumDef(astNode);
316330
case Kind.UNION_TYPE_DEFINITION:
317-
return this._makeUnionDef(def);
331+
return this._makeUnionDef(astNode);
318332
case Kind.SCALAR_TYPE_DEFINITION:
319-
return this._makeScalarDef(def);
333+
return this._makeScalarDef(astNode);
320334
case Kind.INPUT_OBJECT_TYPE_DEFINITION:
321-
return this._makeInputObjectDef(def);
335+
return this._makeInputObjectDef(astNode);
322336
default:
323-
throw new Error(`Type kind "${def.kind}" not supported.`);
337+
throw new Error(`Type kind "${astNode.kind}" not supported.`);
324338
}
325339
}
326340

327-
_makeTypeDef(def: ObjectTypeDefinitionNode) {
328-
const interfaces: ?$ReadOnlyArray<NamedTypeNode> = def.interfaces;
341+
_makeTypeDef(astNode: ObjectTypeDefinitionNode) {
342+
const interfaceNodes = astNode.interfaces;
343+
const fieldNodes = astNode.fields;
344+
345+
// Note: While this could make assertions to get the correctly typed
346+
// values below, that would throw immediately while type system
347+
// validation with validateSchema() will produce more actionable results.
348+
const interfaces =
349+
interfaceNodes && interfaceNodes.length > 0
350+
? () => interfaceNodes.map(ref => (this.buildType(ref): any))
351+
: [];
352+
353+
const fields =
354+
fieldNodes && fieldNodes.length > 0
355+
? () => keyByNameNode(fieldNodes, field => this.buildField(field))
356+
: Object.create(null);
357+
329358
return new GraphQLObjectType({
330-
name: def.name.value,
331-
description: getDescription(def, this._options),
332-
fields: () => this._makeFieldDefMap(def),
333-
// Note: While this could make early assertions to get the correctly
334-
// typed values, that would throw immediately while type system
335-
// validation with validateSchema() will produce more actionable results.
336-
interfaces: interfaces
337-
? () => interfaces.map(ref => (this.buildType(ref): any))
338-
: [],
339-
astNode: def,
359+
name: astNode.name.value,
360+
description: getDescription(astNode, this._options),
361+
interfaces,
362+
fields,
363+
astNode,
340364
});
341365
}
342366

343-
_makeFieldDefMap(
344-
def: ObjectTypeDefinitionNode | InterfaceTypeDefinitionNode,
345-
) {
346-
return def.fields
347-
? keyValMap<_, GraphQLFieldConfig<mixed, mixed>>(
348-
def.fields,
349-
field => field.name.value,
350-
field => this.buildField(field),
351-
)
352-
: {};
353-
}
367+
_makeInterfaceDef(astNode: InterfaceTypeDefinitionNode) {
368+
const fieldNodes = astNode.fields;
354369

355-
_makeInputValues(values: $ReadOnlyArray<InputValueDefinitionNode>) {
356-
return keyValMap<_, GraphQLInputField>(
357-
values,
358-
value => value.name.value,
359-
value => this.buildInputField(value),
360-
);
361-
}
370+
const fields =
371+
fieldNodes && fieldNodes.length > 0
372+
? () => keyByNameNode(fieldNodes, field => this.buildField(field))
373+
: Object.create(null);
362374

363-
_makeInterfaceDef(def: InterfaceTypeDefinitionNode) {
364375
return new GraphQLInterfaceType({
365-
name: def.name.value,
366-
description: getDescription(def, this._options),
367-
fields: () => this._makeFieldDefMap(def),
368-
astNode: def,
376+
name: astNode.name.value,
377+
description: getDescription(astNode, this._options),
378+
fields,
379+
astNode,
369380
});
370381
}
371382

372-
_makeEnumDef(def: EnumTypeDefinitionNode) {
383+
_makeEnumDef(astNode: EnumTypeDefinitionNode) {
384+
const valueNodes = astNode.values || [];
385+
373386
return new GraphQLEnumType({
374-
name: def.name.value,
375-
description: getDescription(def, this._options),
376-
values: this._makeValueDefMap(def),
377-
astNode: def,
387+
name: astNode.name.value,
388+
description: getDescription(astNode, this._options),
389+
values: keyByNameNode(valueNodes, value => this.buildEnumValue(value)),
390+
astNode,
378391
});
379392
}
380393

381-
_makeValueDefMap(def: EnumTypeDefinitionNode) {
382-
return def.values
383-
? keyValMap<_, GraphQLEnumValueConfig>(
384-
def.values,
385-
enumValue => enumValue.name.value,
386-
enumValue => this.buildEnumValue(enumValue),
387-
)
388-
: {};
389-
}
394+
_makeUnionDef(astNode: UnionTypeDefinitionNode) {
395+
const typeNodes = astNode.types;
396+
397+
// Note: While this could make assertions to get the correctly typed
398+
// values below, that would throw immediately while type system
399+
// validation with validateSchema() will produce more actionable results.
400+
const types =
401+
typeNodes && typeNodes.length > 0
402+
? () => typeNodes.map(ref => (this.buildType(ref): any))
403+
: [];
390404

391-
_makeUnionDef(def: UnionTypeDefinitionNode) {
392-
const types: ?$ReadOnlyArray<NamedTypeNode> = def.types;
393405
return new GraphQLUnionType({
394-
name: def.name.value,
395-
description: getDescription(def, this._options),
396-
// Note: While this could make assertions to get the correctly typed
397-
// values below, that would throw immediately while type system
398-
// validation with validateSchema() will produce more actionable results.
399-
types: types ? () => types.map(ref => (this.buildType(ref): any)) : [],
400-
astNode: def,
406+
name: astNode.name.value,
407+
description: getDescription(astNode, this._options),
408+
types,
409+
astNode,
401410
});
402411
}
403412

404-
_makeScalarDef(def: ScalarTypeDefinitionNode) {
413+
_makeScalarDef(astNode: ScalarTypeDefinitionNode) {
405414
return new GraphQLScalarType({
406-
name: def.name.value,
407-
description: getDescription(def, this._options),
408-
astNode: def,
415+
name: astNode.name.value,
416+
description: getDescription(astNode, this._options),
417+
astNode,
409418
serialize: value => value,
410419
});
411420
}
412421

413422
_makeInputObjectDef(def: InputObjectTypeDefinitionNode) {
423+
const { fields } = def;
424+
414425
return new GraphQLInputObjectType({
415426
name: def.name.value,
416427
description: getDescription(def, this._options),
417-
fields: () => (def.fields ? this._makeInputValues(def.fields) : {}),
428+
fields: fields
429+
? () => keyByNameNode(fields, field => this.buildInputField(field))
430+
: Object.create(null),
418431
astNode: def,
419432
});
420433
}
421434
}
422435

436+
function keyByNameNode<T: { +name: NameNode }, V>(
437+
list: $ReadOnlyArray<T>,
438+
valFn: (item: T) => V,
439+
): ObjMap<V> {
440+
return keyValMap(list, ({ name }) => name.value, valFn);
441+
}
442+
423443
/**
424444
* Given a field or enum value node, returns the string value for the
425445
* deprecation reason.

0 commit comments

Comments
 (0)