Skip to content

Commit 635a13c

Browse files
committed
coverage: Clearly mark not reachable places in the code
1 parent 642eeaf commit 635a13c

11 files changed

+223
-126
lines changed

src/__tests__/starWarsSchema.js

+5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
// @flow strict
22

3+
import invariant from '../jsutils/invariant';
4+
35
import { GraphQLSchema } from '../type/schema';
46
import { GraphQLString } from '../type/scalars';
57
import {
@@ -131,6 +133,9 @@ const characterInterface = new GraphQLInterfaceType({
131133
if (character.type === 'Droid') {
132134
return droidType;
133135
}
136+
137+
// Not reachable. All possible types have been considered.
138+
invariant(false);
134139
},
135140
});
136141

src/execution/__tests__/abstract-promise-test.js

+35-21
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import { expect } from 'chai';
44
import { describe, it } from 'mocha';
55

6+
import invariant from '../../jsutils/invariant';
7+
68
import { GraphQLSchema } from '../../type/schema';
79
import { GraphQLString, GraphQLBoolean } from '../../type/scalars';
810
import {
@@ -268,15 +270,18 @@ describe('Execute: Handles execution of abstract types with promises', () => {
268270
const PetType = new GraphQLInterfaceType({
269271
name: 'Pet',
270272
resolveType(obj) {
271-
return Promise.resolve(
272-
obj instanceof Dog
273-
? DogType
274-
: obj instanceof Cat
275-
? CatType
276-
: obj instanceof Human
277-
? HumanType
278-
: null,
279-
);
273+
if (obj instanceof Dog) {
274+
return Promise.resolve(DogType);
275+
}
276+
if (obj instanceof Cat) {
277+
return Promise.resolve(CatType);
278+
}
279+
if (obj instanceof Human) {
280+
return Promise.resolve(HumanType);
281+
}
282+
283+
// Not reachable. All possible types have been considered.
284+
invariant(false);
280285
},
281286
fields: {
282287
name: { type: GraphQLString },
@@ -394,15 +399,18 @@ describe('Execute: Handles execution of abstract types with promises', () => {
394399
const PetType = new GraphQLUnionType({
395400
name: 'Pet',
396401
resolveType(obj) {
397-
return Promise.resolve(
398-
obj instanceof Dog
399-
? DogType
400-
: obj instanceof Cat
401-
? CatType
402-
: obj instanceof Human
403-
? HumanType
404-
: null,
405-
);
402+
if (obj instanceof Dog) {
403+
return Promise.resolve(DogType);
404+
}
405+
if (obj instanceof Cat) {
406+
return Promise.resolve(CatType);
407+
}
408+
if (obj instanceof Human) {
409+
return Promise.resolve(HumanType);
410+
}
411+
412+
// Not reachable. All possible types have been considered.
413+
invariant(false);
406414
},
407415
types: [DogType, CatType],
408416
});
@@ -470,9 +478,15 @@ describe('Execute: Handles execution of abstract types with promises', () => {
470478
const PetType = new GraphQLInterfaceType({
471479
name: 'Pet',
472480
resolveType(obj) {
473-
return Promise.resolve(
474-
obj instanceof Dog ? 'Dog' : obj instanceof Cat ? 'Cat' : null,
475-
);
481+
if (obj instanceof Dog) {
482+
return Promise.resolve('Dog');
483+
}
484+
if (obj instanceof Cat) {
485+
return Promise.resolve('Cat');
486+
}
487+
488+
// Not reachable. All possible types have been considered.
489+
invariant(false);
476490
},
477491
fields: {
478492
name: { type: GraphQLString },

src/execution/__tests__/abstract-test.js

+35-15
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import { expect } from 'chai';
44
import { describe, it } from 'mocha';
55

6+
import invariant from '../../jsutils/invariant';
7+
68
import { GraphQLSchema } from '../../type/schema';
79
import { GraphQLString, GraphQLBoolean } from '../../type/scalars';
810
import {
@@ -191,13 +193,18 @@ describe('Execute: Handles execution of abstract types', () => {
191193
const PetType = new GraphQLInterfaceType({
192194
name: 'Pet',
193195
resolveType(obj) {
194-
return obj instanceof Dog
195-
? DogType
196-
: obj instanceof Cat
197-
? CatType
198-
: obj instanceof Human
199-
? HumanType
200-
: null;
196+
if (obj instanceof Dog) {
197+
return DogType;
198+
}
199+
if (obj instanceof Cat) {
200+
return CatType;
201+
}
202+
if (obj instanceof Human) {
203+
return HumanType;
204+
}
205+
206+
// Not reachable. All possible types have been considered.
207+
invariant(false);
201208
},
202209
fields: {
203210
name: { type: GraphQLString },
@@ -316,13 +323,18 @@ describe('Execute: Handles execution of abstract types', () => {
316323
const PetType = new GraphQLUnionType({
317324
name: 'Pet',
318325
resolveType(obj) {
319-
return obj instanceof Dog
320-
? DogType
321-
: obj instanceof Cat
322-
? CatType
323-
: obj instanceof Human
324-
? HumanType
325-
: null;
326+
if (obj instanceof Dog) {
327+
return DogType;
328+
}
329+
if (obj instanceof Cat) {
330+
return CatType;
331+
}
332+
if (obj instanceof Human) {
333+
return HumanType;
334+
}
335+
336+
// Not reachable. All possible types have been considered.
337+
invariant(false);
326338
},
327339
types: [DogType, CatType],
328340
});
@@ -435,7 +447,15 @@ describe('Execute: Handles execution of abstract types', () => {
435447
const PetType = new GraphQLInterfaceType({
436448
name: 'Pet',
437449
resolveType(obj) {
438-
return obj instanceof Dog ? 'Dog' : obj instanceof Cat ? 'Cat' : null;
450+
if (obj instanceof Dog) {
451+
return 'Dog';
452+
}
453+
if (obj instanceof Cat) {
454+
return 'Cat';
455+
}
456+
457+
// Not reachable. All possible types have been considered.
458+
invariant(false);
439459
},
440460
fields: {
441461
name: { type: GraphQLString },

src/execution/__tests__/union-interface-test.js

+5
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import { expect } from 'chai';
44
import { describe, it } from 'mocha';
55

6+
import invariant from '../../jsutils/invariant';
7+
68
import { parse } from '../../language/parser';
79

810
import { GraphQLSchema } from '../../type/schema';
@@ -120,6 +122,9 @@ const PetType = new GraphQLUnionType({
120122
if (value instanceof Cat) {
121123
return CatType;
122124
}
125+
126+
// Not reachable. All possible types have been considered.
127+
invariant(false);
123128
},
124129
});
125130

src/type/introspection.js

+14-7
Original file line numberDiff line numberDiff line change
@@ -192,19 +192,26 @@ export const __Type = new GraphQLObjectType({
192192
resolve(type) {
193193
if (isScalarType(type)) {
194194
return TypeKind.SCALAR;
195-
} else if (isObjectType(type)) {
195+
}
196+
if (isObjectType(type)) {
196197
return TypeKind.OBJECT;
197-
} else if (isInterfaceType(type)) {
198+
}
199+
if (isInterfaceType(type)) {
198200
return TypeKind.INTERFACE;
199-
} else if (isUnionType(type)) {
201+
}
202+
if (isUnionType(type)) {
200203
return TypeKind.UNION;
201-
} else if (isEnumType(type)) {
204+
}
205+
if (isEnumType(type)) {
202206
return TypeKind.ENUM;
203-
} else if (isInputObjectType(type)) {
207+
}
208+
if (isInputObjectType(type)) {
204209
return TypeKind.INPUT_OBJECT;
205-
} else if (isListType(type)) {
210+
}
211+
if (isListType(type)) {
206212
return TypeKind.LIST;
207-
} else if (isNonNullType(type)) {
213+
}
214+
if (isNonNullType(type)) {
208215
return TypeKind.NON_NULL;
209216
}
210217

src/utilities/TypeInfo.js

+10-6
Original file line numberDiff line numberDiff line change
@@ -172,12 +172,16 @@ export class TypeInfo {
172172
break;
173173
case Kind.OPERATION_DEFINITION: {
174174
let type: mixed;
175-
if (node.operation === 'query') {
176-
type = schema.getQueryType();
177-
} else if (node.operation === 'mutation') {
178-
type = schema.getMutationType();
179-
} else if (node.operation === 'subscription') {
180-
type = schema.getSubscriptionType();
175+
switch (node.operation) {
176+
case 'query':
177+
type = schema.getQueryType();
178+
break;
179+
case 'mutation':
180+
type = schema.getMutationType();
181+
break;
182+
case 'subscription':
183+
type = schema.getSubscriptionType();
184+
break;
181185
}
182186
this._typeStack.push(isObjectType(type) ? type : undefined);
183187
break;

src/utilities/extendSchema.js

+12-6
Original file line numberDiff line numberDiff line change
@@ -265,17 +265,23 @@ export function extendSchemaImpl(
265265
if (isIntrospectionType(type) || isSpecifiedScalarType(type)) {
266266
// Builtin types are not extended.
267267
return type;
268-
} else if (isScalarType(type)) {
268+
}
269+
if (isScalarType(type)) {
269270
return extendScalarType(type);
270-
} else if (isObjectType(type)) {
271+
}
272+
if (isObjectType(type)) {
271273
return extendObjectType(type);
272-
} else if (isInterfaceType(type)) {
274+
}
275+
if (isInterfaceType(type)) {
273276
return extendInterfaceType(type);
274-
} else if (isUnionType(type)) {
277+
}
278+
if (isUnionType(type)) {
275279
return extendUnionType(type);
276-
} else if (isEnumType(type)) {
280+
}
281+
if (isEnumType(type)) {
277282
return extendEnumType(type);
278-
} else if (isInputObjectType(type)) {
283+
}
284+
if (isInputObjectType(type)) {
279285
return extendInputObjectType(type);
280286
}
281287

src/utilities/lexicographicSortSchema.js

+10-5
Original file line numberDiff line numberDiff line change
@@ -104,33 +104,38 @@ export function lexicographicSortSchema(schema: GraphQLSchema): GraphQLSchema {
104104
function sortNamedType(type) {
105105
if (isScalarType(type) || isIntrospectionType(type)) {
106106
return type;
107-
} else if (isObjectType(type)) {
107+
}
108+
if (isObjectType(type)) {
108109
const config = type.toConfig();
109110
return new GraphQLObjectType({
110111
...config,
111112
interfaces: () => sortTypes(config.interfaces),
112113
fields: () => sortFields(config.fields),
113114
});
114-
} else if (isInterfaceType(type)) {
115+
}
116+
if (isInterfaceType(type)) {
115117
const config = type.toConfig();
116118
return new GraphQLInterfaceType({
117119
...config,
118120
interfaces: () => sortTypes(config.interfaces),
119121
fields: () => sortFields(config.fields),
120122
});
121-
} else if (isUnionType(type)) {
123+
}
124+
if (isUnionType(type)) {
122125
const config = type.toConfig();
123126
return new GraphQLUnionType({
124127
...config,
125128
types: () => sortTypes(config.types),
126129
});
127-
} else if (isEnumType(type)) {
130+
}
131+
if (isEnumType(type)) {
128132
const config = type.toConfig();
129133
return new GraphQLEnumType({
130134
...config,
131135
values: sortObjMap(config.values),
132136
});
133-
} else if (isInputObjectType(type)) {
137+
}
138+
if (isInputObjectType(type)) {
134139
const config = type.toConfig();
135140
return new GraphQLInputObjectType({
136141
...config,

src/utilities/schemaPrinter.js

+10-5
Original file line numberDiff line numberDiff line change
@@ -160,15 +160,20 @@ function isSchemaOfCommonNames(schema: GraphQLSchema): boolean {
160160
export function printType(type: GraphQLNamedType, options?: Options): string {
161161
if (isScalarType(type)) {
162162
return printScalar(type, options);
163-
} else if (isObjectType(type)) {
163+
}
164+
if (isObjectType(type)) {
164165
return printObject(type, options);
165-
} else if (isInterfaceType(type)) {
166+
}
167+
if (isInterfaceType(type)) {
166168
return printInterface(type, options);
167-
} else if (isUnionType(type)) {
169+
}
170+
if (isUnionType(type)) {
168171
return printUnion(type, options);
169-
} else if (isEnumType(type)) {
172+
}
173+
if (isEnumType(type)) {
170174
return printEnum(type, options);
171-
} else if (isInputObjectType(type)) {
175+
}
176+
if (isInputObjectType(type)) {
172177
return printInputObject(type, options);
173178
}
174179

0 commit comments

Comments
 (0)