Skip to content

Commit 516dfb9

Browse files
committed
Only type Scalar config rather than Scalar type, improve schema builder types
1 parent f7c78eb commit 516dfb9

File tree

6 files changed

+85
-35
lines changed

6 files changed

+85
-35
lines changed

src/type/definition.js

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -215,13 +215,13 @@ function resolveThunk<T>(thunk: Thunk<T>): T {
215215
* });
216216
*
217217
*/
218-
export class GraphQLScalarType<TInternal, TExternal> {
218+
export class GraphQLScalarType {
219219
name: string;
220220
description: ?string;
221221

222-
_scalarConfig: GraphQLScalarTypeConfig<TInternal, TExternal>;
222+
_scalarConfig: GraphQLScalarTypeConfig<*, *>;
223223

224-
constructor(config: GraphQLScalarTypeConfig<TInternal, TExternal>) {
224+
constructor(config: GraphQLScalarTypeConfig<*, *>) {
225225
invariant(config.name, 'Type must be named.');
226226
assertValidName(config.name);
227227
this.name = config.name;
@@ -243,17 +243,20 @@ export class GraphQLScalarType<TInternal, TExternal> {
243243
this._scalarConfig = config;
244244
}
245245

246-
serialize(value: TInternal): ?TExternal {
246+
// Serializes an internal value to include an a response.
247+
serialize(value: mixed): mixed {
247248
const serializer = this._scalarConfig.serialize;
248249
return serializer(value);
249250
}
250251

251-
parseValue(value: TExternal): ?TInternal {
252+
// Parses an externally provided value to use as an input.
253+
parseValue(value: mixed): mixed {
252254
const parser = this._scalarConfig.parseValue;
253255
return parser ? parser(value) : null;
254256
}
255257

256-
parseLiteral(valueAST: Value): ?TInternal {
258+
// Parses an externally provided literal value to use as an input.
259+
parseLiteral(valueAST: Value): mixed {
257260
const parser = this._scalarConfig.parseLiteral;
258261
return parser ? parser(valueAST) : null;
259262
}
@@ -266,8 +269,8 @@ export class GraphQLScalarType<TInternal, TExternal> {
266269
export type GraphQLScalarTypeConfig<TInternal, TExternal> = {
267270
name: string;
268271
description?: ?string;
269-
serialize: (value: TInternal) => ?TExternal;
270-
parseValue?: (value: TExternal) => ?TInternal;
272+
serialize: (value: mixed) => ?TExternal;
273+
parseValue?: (value: mixed) => ?TInternal;
271274
parseLiteral?: (valueAST: Value) => ?TInternal;
272275
}
273276

src/type/scalars.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import { Kind } from '../language';
1919
const MAX_INT = 2147483647;
2020
const MIN_INT = -2147483648;
2121

22-
function coerceInt(value: number): ?number {
22+
function coerceInt(value: mixed): ?number {
2323
const num = Number(value);
2424
if (num === num && num <= MAX_INT && num >= MIN_INT) {
2525
return (num < 0 ? Math.ceil : Math.floor)(num);
@@ -45,7 +45,7 @@ export const GraphQLInt = new GraphQLScalarType({
4545
}
4646
});
4747

48-
function coerceFloat(value: number): ?number {
48+
function coerceFloat(value: mixed): ?number {
4949
const num = Number(value);
5050
return num === num ? num : null;
5151
}

src/utilities/buildASTSchema.js

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -47,22 +47,35 @@ import type {
4747
DirectiveDefinition,
4848
} from '../language/ast';
4949

50+
import { GraphQLSchema } from '../type/schema';
51+
52+
import {
53+
GraphQLString,
54+
GraphQLInt,
55+
GraphQLFloat,
56+
GraphQLBoolean,
57+
GraphQLID,
58+
} from '../type/scalars';
59+
5060
import {
51-
GraphQLSchema,
5261
GraphQLScalarType,
5362
GraphQLObjectType,
5463
GraphQLInterfaceType,
5564
GraphQLUnionType,
5665
GraphQLEnumType,
5766
GraphQLInputObjectType,
58-
GraphQLString,
59-
GraphQLInt,
60-
GraphQLFloat,
61-
GraphQLBoolean,
62-
GraphQLID,
6367
GraphQLList,
6468
GraphQLNonNull,
65-
} from '../type';
69+
isInputType,
70+
isOutputType,
71+
} from '../type/definition';
72+
73+
import type {
74+
GraphQLType,
75+
GraphQLNamedType,
76+
GraphQLInputType,
77+
GraphQLOutputType,
78+
} from '../type/definition';
6679

6780
import {
6881
GraphQLDirective,
@@ -86,11 +99,6 @@ import {
8699
__TypeKind,
87100
} from '../type/introspection';
88101

89-
import type {
90-
GraphQLType,
91-
GraphQLNamedType
92-
} from '../type/definition';
93-
94102

95103
function buildWrappedType(
96104
innerType: GraphQLType,
@@ -275,6 +283,18 @@ export function buildASTSchema(ast: Document): GraphQLSchema {
275283
return buildWrappedType(typeDef, typeAST);
276284
}
277285

286+
function produceInputType(typeAST: Type): GraphQLInputType {
287+
const type = produceType(typeAST);
288+
invariant(isInputType(type), 'Expected Input type.');
289+
return (type: any);
290+
}
291+
292+
function produceOutputType(typeAST: Type): GraphQLOutputType {
293+
const type = produceType(typeAST);
294+
invariant(isOutputType(type), 'Expected Output type.');
295+
return (type: any);
296+
}
297+
278298
function produceObjectType(typeAST: Type): GraphQLObjectType {
279299
const type = produceType(typeAST);
280300
invariant(type instanceof GraphQLObjectType, 'Expected Object type.');
@@ -343,7 +363,7 @@ export function buildASTSchema(ast: Document): GraphQLSchema {
343363
def.fields,
344364
field => field.name.value,
345365
field => ({
346-
type: produceType(field.type),
366+
type: produceOutputType(field.type),
347367
args: makeInputValues(field.arguments),
348368
deprecationReason: getDeprecationReason(field.directives)
349369
})
@@ -360,7 +380,7 @@ export function buildASTSchema(ast: Document): GraphQLSchema {
360380
values,
361381
value => value.name.value,
362382
value => {
363-
const type = produceType(value.type);
383+
const type = produceInputType(value.type);
364384
return { type, defaultValue: valueFromAST(value.defaultValue, type) };
365385
}
366386
);

src/utilities/buildClientSchema.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ export function buildClientSchema(
222222

223223
function buildScalarDef(
224224
scalarIntrospection: IntrospectionScalarType
225-
): GraphQLScalarType<boolean, void> {
225+
): GraphQLScalarType {
226226
return new GraphQLScalarType({
227227
name: scalarIntrospection.name,
228228
description: scalarIntrospection.description,

src/utilities/extendSchema.js

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ import {
2424
GraphQLScalarType,
2525
GraphQLEnumType,
2626
GraphQLInputObjectType,
27+
isInputType,
28+
isOutputType,
2729
} from '../type/definition';
2830

2931
import {
@@ -61,6 +63,8 @@ import {
6163
import type {
6264
GraphQLType,
6365
GraphQLNamedType,
66+
GraphQLInputType,
67+
GraphQLOutputType,
6468
} from '../type/definition';
6569

6670
import type {
@@ -200,8 +204,9 @@ export function extendSchema(
200204

201205
// Iterate through all types, getting the type definition for each, ensuring
202206
// that any type not directly referenced by a field will get created.
203-
const types = Object.keys(schema.getTypeMap()).map(typeName =>
204-
getTypeFromDef(schema.getType(typeName))
207+
const typeMap = schema.getTypeMap();
208+
const types = Object.keys(typeMap).map(typeName =>
209+
getTypeFromDef(typeMap[typeName])
205210
);
206211

207212
// Do the same with new types, appending to the list of defined types.
@@ -266,6 +271,18 @@ export function extendSchema(
266271
return type;
267272
}
268273

274+
function getInputTypeFromAST(astNode: NamedType): GraphQLInputType {
275+
const type = getTypeFromAST(astNode);
276+
invariant(isInputType(type), 'Must be Input type.');
277+
return (type: any);
278+
}
279+
280+
function getOutputTypeFromAST(astNode: NamedType): GraphQLOutputType {
281+
const type = getTypeFromAST(astNode);
282+
invariant(isOutputType(type), 'Must be Output type.');
283+
return (type: any);
284+
}
285+
269286
// Given a name, returns a type from either the existing schema or an
270287
// added type.
271288
function _getNamedType(typeName: string): ?GraphQLNamedType {
@@ -387,7 +404,7 @@ export function extendSchema(
387404
);
388405
}
389406
newFieldMap[fieldName] = {
390-
type: buildFieldType(field.type),
407+
type: buildOutputFieldType(field.type),
391408
args: buildInputValues(field.arguments),
392409
resolve: cannotExecuteClientSchema,
393410
};
@@ -480,7 +497,7 @@ export function extendSchema(
480497
typeAST.fields,
481498
field => field.name.value,
482499
field => ({
483-
type: buildFieldType(field.type),
500+
type: buildOutputFieldType(field.type),
484501
args: buildInputValues(field.arguments),
485502
resolve: cannotExecuteClientSchema,
486503
})
@@ -492,7 +509,7 @@ export function extendSchema(
492509
values,
493510
value => value.name.value,
494511
value => {
495-
const type = buildFieldType(value.type);
512+
const type = buildInputFieldType(value.type);
496513
return {
497514
type,
498515
defaultValue: valueFromAST(value.defaultValue, type)
@@ -501,14 +518,24 @@ export function extendSchema(
501518
);
502519
}
503520

504-
function buildFieldType(typeAST: Type): GraphQLType {
521+
function buildInputFieldType(typeAST: Type): GraphQLInputType {
522+
if (typeAST.kind === LIST_TYPE) {
523+
return new GraphQLList(buildInputFieldType(typeAST.type));
524+
}
525+
if (typeAST.kind === NON_NULL_TYPE) {
526+
return new GraphQLNonNull(buildInputFieldType(typeAST.type));
527+
}
528+
return getInputTypeFromAST(typeAST);
529+
}
530+
531+
function buildOutputFieldType(typeAST: Type): GraphQLOutputType {
505532
if (typeAST.kind === LIST_TYPE) {
506-
return new GraphQLList(buildFieldType(typeAST.type));
533+
return new GraphQLList(buildOutputFieldType(typeAST.type));
507534
}
508535
if (typeAST.kind === NON_NULL_TYPE) {
509-
return new GraphQLNonNull(buildFieldType(typeAST.type));
536+
return new GraphQLNonNull(buildOutputFieldType(typeAST.type));
510537
}
511-
return getTypeFromAST(typeAST);
538+
return getOutputTypeFromAST(typeAST);
512539
}
513540
}
514541

src/utilities/schemaPrinter.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ function printType(type: GraphQLType): string {
114114
return printInputObject(type);
115115
}
116116

117-
function printScalar(type: GraphQLScalarType<any, any>): string {
117+
function printScalar(type: GraphQLScalarType): string {
118118
return `scalar ${type.name}`;
119119
}
120120

0 commit comments

Comments
 (0)