Skip to content

Commit d9212aa

Browse files
cometkimardatan
authored andcommittedApr 12, 2021
fix(visitor-plugin-common): guards possible runtime type error (#5724)
* fix(visitor-plugin-common): guards possible runtime type error * add changesets * narrow down null-check responsibility * remove unused var
1 parent 875cdae commit d9212aa

File tree

3 files changed

+42
-12
lines changed

3 files changed

+42
-12
lines changed
 

‎.changeset/chilly-donuts-breathe.md

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
---
2+
'@graphql-cli/codegen': patch
3+
'@graphql-codegen/cli': patch
4+
'@graphql-codegen/c-sharp': patch
5+
'@graphql-codegen/c-sharp-operations': patch
6+
'@graphql-codegen/flow': patch
7+
'@graphql-codegen/flow-operations': patch
8+
'@graphql-codegen/java-apollo-android': patch
9+
'@graphql-codegen/introspection': patch
10+
'@graphql-codegen/schema-ast': patch
11+
'@graphql-codegen/urql-introspection': patch
12+
'@graphql-codegen/visitor-plugin-common': patch
13+
'@graphql-codegen/typescript-apollo-angular': patch
14+
'@graphql-codegen/typescript-apollo-client-helpers': patch
15+
'@graphql-codegen/typescript-compatibility': patch
16+
'@graphql-codegen/typescript-document-nodes': patch
17+
'@graphql-codegen/typescript-generic-sdk': patch
18+
'@graphql-codegen/typescript-graphql-request': patch
19+
'@graphql-codegen/typescript-mongodb': patch
20+
'@graphql-codegen/named-operations-object': patch
21+
'@graphql-codegen/typescript-operations': patch
22+
'@graphql-codegen/typescript-react-apollo': patch
23+
'@graphql-codegen/typescript-react-offix': patch
24+
'@graphql-codegen/typescript-react-query': patch
25+
'@graphql-codegen/typescript-resolvers': patch
26+
'@graphql-codegen/typescript-stencil-apollo': patch
27+
'@graphql-codegen/typescript-type-graphql': patch
28+
'@graphql-codegen/typed-document-node': patch
29+
'@graphql-codegen/typescript': patch
30+
'@graphql-codegen/typescript-urql': patch
31+
'@graphql-codegen/typescript-vue-apollo': patch
32+
'@graphql-codegen/graphql-modules-preset': patch
33+
'@graphql-codegen/config-schema': patch
34+
'@graphql-codegen/plugin-helpers': patch
35+
---
36+
37+
fix(visitor-plugin-common): guard for a runtime type error

‎packages/plugins/other/visitor-plugin-common/src/selection-set-to-object.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,7 @@ export class SelectionSetToObject<Config extends ParsedDocumentsConfig = ParsedD
372372
for (const { field, selectedFieldType } of linkFieldSelectionSets.values()) {
373373
const realSelectedFieldType = getBaseType(selectedFieldType as any);
374374
const selectionSet = this.createNext(realSelectedFieldType, field.selectionSet);
375-
const isConditional = hasConditionalDirectives(field.directives);
375+
const isConditional = hasConditionalDirectives(field);
376376

377377
linkFields.push({
378378
alias: field.alias ? this._processor.config.formatNamedField(field.alias.value, selectedFieldType) : undefined,
@@ -397,7 +397,7 @@ export class SelectionSetToObject<Config extends ParsedDocumentsConfig = ParsedD
397397
...this._processor.transformPrimitiveFields(
398398
parentSchemaType,
399399
Array.from(primitiveFields.values()).map(field => ({
400-
isConditional: hasConditionalDirectives(field.directives),
400+
isConditional: hasConditionalDirectives(field),
401401
fieldName: field.name.value,
402402
}))
403403
),

‎packages/plugins/other/visitor-plugin-common/src/utils.ts

+3-10
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import {
2020
isListType,
2121
isAbstractType,
2222
GraphQLOutputType,
23-
DirectiveNode,
2423
} from 'graphql';
2524
import { ScalarsMap, NormalizedScalarsMap, ParsedScalarsMap } from './types';
2625
import { DEFAULT_SCALARS } from './scalars';
@@ -403,15 +402,9 @@ export function getPossibleTypes(schema: GraphQLSchema, type: GraphQLNamedType):
403402
return [];
404403
}
405404

406-
export function hasConditionalDirectives(directives: readonly DirectiveNode[]): boolean {
407-
if (directives.length === 0) return false;
408-
409-
for (const directive of directives) {
410-
if (['skip', 'include'].includes(directive.name.value)) {
411-
return true;
412-
}
413-
}
414-
return false;
405+
export function hasConditionalDirectives(field: FieldNode): boolean {
406+
const CONDITIONAL_DIRECTIVES = ['skip', 'include'];
407+
return field.directives?.some(directive => CONDITIONAL_DIRECTIVES.includes(directive.name.value));
415408
}
416409

417410
type WrapModifiersOptions = {

0 commit comments

Comments
 (0)
Please sign in to comment.