Skip to content

Commit 1c6b53e

Browse files
committed
Unify arguments naming for parseValue/serialize methods
1 parent 4fcef75 commit 1c6b53e

File tree

2 files changed

+112
-102
lines changed

2 files changed

+112
-102
lines changed

src/type/definition.js

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -609,8 +609,14 @@ export class GraphQLScalarType {
609609
defineToStringTag(GraphQLScalarType);
610610
defineToJSON(GraphQLScalarType);
611611
612-
export type GraphQLScalarSerializer<TExternal> = (value: mixed) => ?TExternal;
613-
export type GraphQLScalarValueParser<TInternal> = (value: mixed) => ?TInternal;
612+
export type GraphQLScalarSerializer<TExternal> = (
613+
outputValue: mixed,
614+
) => ?TExternal;
615+
616+
export type GraphQLScalarValueParser<TInternal> = (
617+
inputValue: mixed,
618+
) => ?TInternal;
619+
614620
export type GraphQLScalarLiteralParser<TInternal> = (
615621
valueNode: ValueNode,
616622
variables: ?ObjMap<mixed>,
@@ -1226,16 +1232,16 @@ export class GraphQLEnumType /* <T> */ {
12261232
return this._nameLookup[name];
12271233
}
12281234

1229-
serialize(value: mixed /* T */): ?string {
1230-
const enumValue = this._valueLookup.get(value);
1235+
serialize(outputValue: mixed /* T */): ?string {
1236+
const enumValue = this._valueLookup.get(outputValue);
12311237
if (enumValue) {
12321238
return enumValue.name;
12331239
}
12341240
}
12351241

1236-
parseValue(value: mixed): ?any /* T */ {
1237-
if (typeof value === 'string') {
1238-
const enumValue = this.getValue(value);
1242+
parseValue(inputValue: mixed): ?any /* T */ {
1243+
if (typeof inputValue === 'string') {
1244+
const enumValue = this.getValue(inputValue);
12391245
if (enumValue) {
12401246
return enumValue.value;
12411247
}

src/type/scalars.js

Lines changed: 99 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -21,41 +21,42 @@ import { type GraphQLNamedType, GraphQLScalarType } from './definition';
2121
const MAX_INT = 2147483647;
2222
const MIN_INT = -2147483648;
2323

24-
function serializeInt(value: mixed): number {
25-
if (typeof value === 'boolean') {
26-
return value ? 1 : 0;
24+
function serializeInt(outputValue: mixed): number {
25+
if (typeof outputValue === 'boolean') {
26+
return outputValue ? 1 : 0;
2727
}
2828

29-
let num = value;
30-
if (typeof value === 'string' && value !== '') {
31-
num = Number(value);
29+
let num = outputValue;
30+
if (typeof outputValue === 'string' && outputValue !== '') {
31+
num = Number(outputValue);
3232
}
3333

3434
if (!isInteger(num)) {
3535
throw new GraphQLError(
36-
`Int cannot represent non-integer value: ${inspect(value)}`,
36+
`Int cannot represent non-integer value: ${inspect(outputValue)}`,
3737
);
3838
}
3939
if (num > MAX_INT || num < MIN_INT) {
4040
throw new GraphQLError(
41-
`Int cannot represent non 32-bit signed integer value: ${inspect(value)}`,
41+
'Int cannot represent non 32-bit signed integer value: ' +
42+
inspect(outputValue),
4243
);
4344
}
4445
return num;
4546
}
4647

47-
function coerceInt(value: mixed): number {
48-
if (!isInteger(value)) {
48+
function coerceInt(inputValue: mixed): number {
49+
if (!isInteger(inputValue)) {
4950
throw new GraphQLError(
50-
`Int cannot represent non-integer value: ${inspect(value)}`,
51+
`Int cannot represent non-integer value: ${inspect(inputValue)}`,
5152
);
5253
}
53-
if (value > MAX_INT || value < MIN_INT) {
54+
if (inputValue > MAX_INT || inputValue < MIN_INT) {
5455
throw new GraphQLError(
55-
`Int cannot represent non 32-bit signed integer value: ${inspect(value)}`,
56+
`Int cannot represent non 32-bit signed integer value: ${inputValue}`,
5657
);
5758
}
58-
return value;
59+
return inputValue;
5960
}
6061

6162
export const GraphQLInt = new GraphQLScalarType({
@@ -64,48 +65,48 @@ export const GraphQLInt = new GraphQLScalarType({
6465
'The `Int` scalar type represents non-fractional signed whole numeric values. Int can represent values between -(2^31) and 2^31 - 1.',
6566
serialize: serializeInt,
6667
parseValue: coerceInt,
67-
parseLiteral(ast) {
68-
if (ast.kind !== Kind.INT) {
68+
parseLiteral(valueNode) {
69+
if (valueNode.kind !== Kind.INT) {
6970
throw new GraphQLError(
70-
'Int cannot represent non-integer value: ' + print(ast),
71-
ast,
71+
`Int cannot represent non-integer value: ${print(valueNode)}`,
72+
valueNode,
7273
);
7374
}
74-
const num = parseInt(ast.value, 10);
75+
const num = parseInt(valueNode.value, 10);
7576
if (num > MAX_INT || num < MIN_INT) {
7677
throw new GraphQLError(
77-
'Int cannot represent non 32-bit signed integer value: ' + ast.value,
78-
ast,
78+
`Int cannot represent non 32-bit signed integer value: ${valueNode.value}`,
79+
valueNode,
7980
);
8081
}
8182
return num;
8283
},
8384
});
8485

85-
function serializeFloat(value: mixed): number {
86-
if (typeof value === 'boolean') {
87-
return value ? 1 : 0;
86+
function serializeFloat(outputValue: mixed): number {
87+
if (typeof outputValue === 'boolean') {
88+
return outputValue ? 1 : 0;
8889
}
8990

90-
let num = value;
91-
if (typeof value === 'string' && value !== '') {
92-
num = Number(value);
91+
let num = outputValue;
92+
if (typeof outputValue === 'string' && outputValue !== '') {
93+
num = Number(outputValue);
9394
}
9495
if (!isFinite(num)) {
9596
throw new GraphQLError(
96-
`Float cannot represent non numeric value: ${inspect(value)}`,
97+
`Float cannot represent non numeric value: ${inspect(outputValue)}`,
9798
);
9899
}
99100
return num;
100101
}
101102

102-
function coerceFloat(value: mixed): number {
103-
if (!isFinite(value)) {
103+
function coerceFloat(inputValue: mixed): number {
104+
if (!isFinite(inputValue)) {
104105
throw new GraphQLError(
105-
`Float cannot represent non numeric value: ${inspect(value)}`,
106+
`Float cannot represent non numeric value: ${inspect(inputValue)}`,
106107
);
107108
}
108-
return value;
109+
return inputValue;
109110
}
110111

111112
export const GraphQLFloat = new GraphQLScalarType({
@@ -114,60 +115,62 @@ export const GraphQLFloat = new GraphQLScalarType({
114115
'The `Float` scalar type represents signed double-precision fractional values as specified by [IEEE 754](https://en.wikipedia.org/wiki/IEEE_floating_point).',
115116
serialize: serializeFloat,
116117
parseValue: coerceFloat,
117-
parseLiteral(ast) {
118-
if (ast.kind !== Kind.FLOAT && ast.kind !== Kind.INT) {
118+
parseLiteral(valueNode) {
119+
if (valueNode.kind !== Kind.FLOAT && valueNode.kind !== Kind.INT) {
119120
throw new GraphQLError(
120-
'Float cannot represent non numeric value: ' + print(ast),
121-
ast,
121+
`Float cannot represent non numeric value: ${print(valueNode)}`,
122+
valueNode,
122123
);
123124
}
124-
return parseFloat(ast.value);
125+
return parseFloat(valueNode.value);
125126
},
126127
});
127128

128129
// Support serializing objects with custom valueOf() or toJSON() functions -
129130
// a common way to represent a complex value which can be represented as
130131
// a string (ex: MongoDB id objects).
131-
function serializeObject(value: mixed): mixed {
132-
if (isObjectLike(value)) {
133-
if (typeof value.valueOf === 'function') {
134-
const valueOfResult = value.valueOf();
132+
function serializeObject(outputValue: mixed): mixed {
133+
if (isObjectLike(outputValue)) {
134+
if (typeof outputValue.valueOf === 'function') {
135+
const valueOfResult = outputValue.valueOf();
135136
if (!isObjectLike(valueOfResult)) {
136137
return valueOfResult;
137138
}
138139
}
139-
if (typeof value.toJSON === 'function') {
140+
if (typeof outputValue.toJSON === 'function') {
140141
// $FlowFixMe(>=0.90.0)
141-
return value.toJSON();
142+
return outputValue.toJSON();
142143
}
143144
}
144-
return value;
145+
return outputValue;
145146
}
146147

147-
function serializeString(rawValue: mixed): string {
148-
const value = serializeObject(rawValue);
148+
function serializeString(outputValue: mixed): string {
149+
const coercedValue = serializeObject(outputValue);
149150

150151
// Serialize string, boolean and number values to a string, but do not
151152
// attempt to coerce object, function, symbol, or other types as strings.
152-
if (typeof value === 'string') {
153-
return value;
153+
if (typeof coercedValue === 'string') {
154+
return coercedValue;
154155
}
155-
if (typeof value === 'boolean') {
156-
return value ? 'true' : 'false';
156+
if (typeof coercedValue === 'boolean') {
157+
return coercedValue ? 'true' : 'false';
157158
}
158-
if (isFinite(value)) {
159-
return value.toString();
159+
if (isFinite(coercedValue)) {
160+
return coercedValue.toString();
160161
}
161-
throw new GraphQLError(`String cannot represent value: ${inspect(rawValue)}`);
162+
throw new GraphQLError(
163+
`String cannot represent value: ${inspect(outputValue)}`,
164+
);
162165
}
163166

164-
function coerceString(value: mixed): string {
165-
if (typeof value !== 'string') {
167+
function coerceString(inputValue: mixed): string {
168+
if (typeof inputValue !== 'string') {
166169
throw new GraphQLError(
167-
`String cannot represent a non string value: ${inspect(value)}`,
170+
`String cannot represent a non string value: ${inspect(inputValue)}`,
168171
);
169172
}
170-
return value;
173+
return inputValue;
171174
}
172175

173176
export const GraphQLString = new GraphQLScalarType({
@@ -176,74 +179,74 @@ export const GraphQLString = new GraphQLScalarType({
176179
'The `String` scalar type represents textual data, represented as UTF-8 character sequences. The String type is most often used by GraphQL to represent free-form human-readable text.',
177180
serialize: serializeString,
178181
parseValue: coerceString,
179-
parseLiteral(ast) {
180-
if (ast.kind !== Kind.STRING) {
182+
parseLiteral(valueNode) {
183+
if (valueNode.kind !== Kind.STRING) {
181184
throw new GraphQLError(
182-
'String cannot represent a non string value: ' + print(ast),
183-
ast,
185+
`String cannot represent a non string value: ${print(valueNode)}`,
186+
valueNode,
184187
);
185188
}
186-
return ast.value;
189+
return valueNode.value;
187190
},
188191
});
189192

190-
function serializeBoolean(value: mixed): boolean {
191-
if (typeof value === 'boolean') {
192-
return value;
193+
function serializeBoolean(outputValue: mixed): boolean {
194+
if (typeof outputValue === 'boolean') {
195+
return outputValue;
193196
}
194-
if (isFinite(value)) {
195-
return value !== 0;
197+
if (isFinite(outputValue)) {
198+
return outputValue !== 0;
196199
}
197200
throw new GraphQLError(
198-
`Boolean cannot represent a non boolean value: ${inspect(value)}`,
201+
`Boolean cannot represent a non boolean value: ${inspect(outputValue)}`,
199202
);
200203
}
201204

202-
function coerceBoolean(value: mixed): boolean {
203-
if (typeof value !== 'boolean') {
205+
function coerceBoolean(inputValue: mixed): boolean {
206+
if (typeof inputValue !== 'boolean') {
204207
throw new GraphQLError(
205-
`Boolean cannot represent a non boolean value: ${inspect(value)}`,
208+
`Boolean cannot represent a non boolean value: ${inspect(inputValue)}`,
206209
);
207210
}
208-
return value;
211+
return inputValue;
209212
}
210213

211214
export const GraphQLBoolean = new GraphQLScalarType({
212215
name: 'Boolean',
213216
description: 'The `Boolean` scalar type represents `true` or `false`.',
214217
serialize: serializeBoolean,
215218
parseValue: coerceBoolean,
216-
parseLiteral(ast) {
217-
if (ast.kind !== Kind.BOOLEAN) {
219+
parseLiteral(valueNode) {
220+
if (valueNode.kind !== Kind.BOOLEAN) {
218221
throw new GraphQLError(
219-
'Boolean cannot represent a non boolean value: ' + print(ast),
220-
ast,
222+
`Boolean cannot represent a non boolean value: ${print(valueNode)}`,
223+
valueNode,
221224
);
222225
}
223-
return ast.value;
226+
return valueNode.value;
224227
},
225228
});
226229

227-
function serializeID(rawValue: mixed): string {
228-
const value = serializeObject(rawValue);
230+
function serializeID(outputValue: mixed): string {
231+
const coercedValue = serializeObject(outputValue);
229232

230-
if (typeof value === 'string') {
231-
return value;
233+
if (typeof coercedValue === 'string') {
234+
return coercedValue;
232235
}
233-
if (isInteger(value)) {
234-
return String(value);
236+
if (isInteger(coercedValue)) {
237+
return String(coercedValue);
235238
}
236-
throw new GraphQLError(`ID cannot represent value: ${inspect(rawValue)}`);
239+
throw new GraphQLError(`ID cannot represent value: ${inspect(outputValue)}`);
237240
}
238241

239-
function coerceID(value: mixed): string {
240-
if (typeof value === 'string') {
241-
return value;
242+
function coerceID(inputValue: mixed): string {
243+
if (typeof inputValue === 'string') {
244+
return inputValue;
242245
}
243-
if (isInteger(value)) {
244-
return value.toString();
246+
if (isInteger(inputValue)) {
247+
return inputValue.toString();
245248
}
246-
throw new GraphQLError(`ID cannot represent value: ${inspect(value)}`);
249+
throw new GraphQLError(`ID cannot represent value: ${inspect(inputValue)}`);
247250
}
248251

249252
export const GraphQLID = new GraphQLScalarType({
@@ -252,14 +255,15 @@ export const GraphQLID = new GraphQLScalarType({
252255
'The `ID` scalar type represents a unique identifier, often used to refetch an object or as key for a cache. The ID type appears in a JSON response as a String; however, it is not intended to be human-readable. When expected as an input type, any string (such as `"4"`) or integer (such as `4`) input value will be accepted as an ID.',
253256
serialize: serializeID,
254257
parseValue: coerceID,
255-
parseLiteral(ast) {
256-
if (ast.kind !== Kind.STRING && ast.kind !== Kind.INT) {
258+
parseLiteral(valueNode) {
259+
if (valueNode.kind !== Kind.STRING && valueNode.kind !== Kind.INT) {
257260
throw new GraphQLError(
258-
'ID cannot represent a non-string and non-integer value: ' + print(ast),
259-
ast,
261+
'ID cannot represent a non-string and non-integer value: ' +
262+
print(valueNode),
263+
valueNode,
260264
);
261265
}
262-
return ast.value;
266+
return valueNode.value;
263267
},
264268
});
265269

0 commit comments

Comments
 (0)