Skip to content

Commit 4150d1f

Browse files
Use nullish coalescing operator in all appropriate places (#2423)
1 parent 468dfb5 commit 4150d1f

29 files changed

+93
-78
lines changed

src/__tests__/starWarsData.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ export type Droid = {|
117117
*/
118118
function getCharacter(id) {
119119
// Returning a promise just to illustrate that GraphQL.js supports it.
120-
return Promise.resolve(humanData[id] || droidData[id]);
120+
return Promise.resolve(humanData[id] ?? droidData[id]);
121121
}
122122

123123
/**

src/error/GraphQLError.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ export class GraphQLError extends Error {
148148
locations: {
149149
// Coercing falsy values to undefined ensures they will not be included
150150
// in JSON.stringify() when not provided.
151-
value: _locations || undefined,
151+
value: _locations ?? undefined,
152152
// By being enumerable, JSON.stringify will include `locations` in the
153153
// resulting output. This ensures that the simplest possible GraphQL
154154
// service adheres to the spec.
@@ -157,28 +157,28 @@ export class GraphQLError extends Error {
157157
path: {
158158
// Coercing falsy values to undefined ensures they will not be included
159159
// in JSON.stringify() when not provided.
160-
value: path || undefined,
160+
value: path ?? undefined,
161161
// By being enumerable, JSON.stringify will include `path` in the
162162
// resulting output. This ensures that the simplest possible GraphQL
163163
// service adheres to the spec.
164164
enumerable: path != null,
165165
},
166166
nodes: {
167-
value: _nodes || undefined,
167+
value: _nodes ?? undefined,
168168
},
169169
source: {
170-
value: _source || undefined,
170+
value: _source ?? undefined,
171171
},
172172
positions: {
173-
value: _positions || undefined,
173+
value: _positions ?? undefined,
174174
},
175175
originalError: {
176176
value: originalError,
177177
},
178178
extensions: {
179179
// Coercing falsy values to undefined ensures they will not be included
180180
// in JSON.stringify() when not provided.
181-
value: _extensions || undefined,
181+
value: _extensions ?? undefined,
182182
// By being enumerable, JSON.stringify will include `path` in the
183183
// resulting output. This ensures that the simplest possible GraphQL
184184
// service adheres to the spec.

src/error/formatError.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { type GraphQLError } from './GraphQLError';
1212
*/
1313
export function formatError(error: GraphQLError): GraphQLFormattedError {
1414
devAssert(error, 'Received null or undefined error.');
15-
const message = error.message || 'An unknown error occurred.';
15+
const message = error.message ?? 'An unknown error occurred.';
1616
const locations = error.locations;
1717
const path = error.path;
1818
const extensions = error.extensions;

src/error/locatedError.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export function locatedError(
2222

2323
return new GraphQLError(
2424
originalError.message,
25-
(originalError: any).nodes || nodes,
25+
(originalError: any).nodes ?? nodes,
2626
(originalError: any).source,
2727
(originalError: any).positions,
2828
path,

src/execution/execute.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -314,12 +314,12 @@ export function buildExecutionContext(
314314
}
315315

316316
/* istanbul ignore next (See https://github.com/graphql/graphql-js/issues/2203) */
317-
const variableDefinitions = operation.variableDefinitions || [];
317+
const variableDefinitions = operation.variableDefinitions ?? [];
318318

319319
const coercedVariableValues = getVariableValues(
320320
schema,
321321
variableDefinitions,
322-
rawVariableValues || {},
322+
rawVariableValues ?? {},
323323
{ maxErrors: 50 },
324324
);
325325

@@ -334,8 +334,8 @@ export function buildExecutionContext(
334334
contextValue,
335335
operation,
336336
variableValues: coercedVariableValues.coerced,
337-
fieldResolver: fieldResolver || defaultFieldResolver,
338-
typeResolver: typeResolver || defaultTypeResolver,
337+
fieldResolver: fieldResolver ?? defaultFieldResolver,
338+
typeResolver: typeResolver ?? defaultTypeResolver,
339339
errors: [],
340340
};
341341
}
@@ -619,7 +619,7 @@ function resolveField(
619619
return;
620620
}
621621

622-
const resolveFn = fieldDef.resolve || exeContext.fieldResolver;
622+
const resolveFn = fieldDef.resolve ?? exeContext.fieldResolver;
623623

624624
const info = buildResolveInfo(
625625
exeContext,
@@ -961,7 +961,7 @@ function completeAbstractValue(
961961
path: Path,
962962
result: mixed,
963963
): PromiseOrValue<ObjMap<mixed>> {
964-
const resolveTypeFn = returnType.resolveType || exeContext.typeResolver;
964+
const resolveTypeFn = returnType.resolveType ?? exeContext.typeResolver;
965965
const contextValue = exeContext.contextValue;
966966
const runtimeType = resolveTypeFn(result, contextValue, info, returnType);
967967

src/execution/values.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ export function getArgumentValues(
166166
const coercedValues = {};
167167

168168
/* istanbul ignore next (See https://github.com/graphql/graphql-js/issues/2203) */
169-
const argumentNodes = node.arguments || [];
169+
const argumentNodes = node.arguments ?? [];
170170
const argNodeMap = keyMap(argumentNodes, arg => arg.name.value);
171171

172172
for (const argDef of def.args) {

src/language/lexer.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ export class Lexer {
6666
if (token.kind !== TokenKind.EOF) {
6767
do {
6868
// Note: next is only mutable during parsing, so we cast to allow this.
69-
token = token.next || ((token: any).next = readToken(this, token));
69+
token = token.next ?? ((token: any).next = readToken(this, token));
7070
} while (token.kind === TokenKind.COMMENT);
7171
}
7272
return token;

src/language/parser.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1482,7 +1482,7 @@ class Parser {
14821482
* is encountered.
14831483
*/
14841484
unexpected(atToken?: ?Token): GraphQLError {
1485-
const token = atToken || this._lexer.token;
1485+
const token = atToken ?? this._lexer.token;
14861486
return syntaxError(
14871487
this._lexer.source,
14881488
token.start,

src/language/printer.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ function addDescription(cb) {
256256
* print all items together separated by separator if provided
257257
*/
258258
function join(maybeArray: ?Array<string>, separator = '') {
259-
return maybeArray?.filter(x => x).join(separator) || '';
259+
return maybeArray?.filter(x => x).join(separator) ?? '';
260260
}
261261

262262
/**

src/language/visitor.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,7 @@ export function visit(
332332
} else {
333333
stack = { inArray, index, keys, edits, prev: stack };
334334
inArray = Array.isArray(node);
335-
keys = inArray ? node : visitorKeys[node.kind] || [];
335+
keys = inArray ? node : visitorKeys[node.kind] ?? [];
336336
index = -1;
337337
edits = [];
338338
if (parent) {

0 commit comments

Comments
 (0)