Skip to content

Commit

Permalink
fix(visitor-plugin-common): guards possible runtime type error (#5724)
Browse files Browse the repository at this point in the history
* fix(visitor-plugin-common): guards possible runtime type error

* add changesets

* narrow down null-check responsibility

* remove unused var
  • Loading branch information
cometkim authored and ardatan committed Apr 12, 2021
1 parent 875cdae commit d9212aa
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 12 deletions.
37 changes: 37 additions & 0 deletions .changeset/chilly-donuts-breathe.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
---
'@graphql-cli/codegen': patch
'@graphql-codegen/cli': patch
'@graphql-codegen/c-sharp': patch
'@graphql-codegen/c-sharp-operations': patch
'@graphql-codegen/flow': patch
'@graphql-codegen/flow-operations': patch
'@graphql-codegen/java-apollo-android': patch
'@graphql-codegen/introspection': patch
'@graphql-codegen/schema-ast': patch
'@graphql-codegen/urql-introspection': patch
'@graphql-codegen/visitor-plugin-common': patch
'@graphql-codegen/typescript-apollo-angular': patch
'@graphql-codegen/typescript-apollo-client-helpers': patch
'@graphql-codegen/typescript-compatibility': patch
'@graphql-codegen/typescript-document-nodes': patch
'@graphql-codegen/typescript-generic-sdk': patch
'@graphql-codegen/typescript-graphql-request': patch
'@graphql-codegen/typescript-mongodb': patch
'@graphql-codegen/named-operations-object': patch
'@graphql-codegen/typescript-operations': patch
'@graphql-codegen/typescript-react-apollo': patch
'@graphql-codegen/typescript-react-offix': patch
'@graphql-codegen/typescript-react-query': patch
'@graphql-codegen/typescript-resolvers': patch
'@graphql-codegen/typescript-stencil-apollo': patch
'@graphql-codegen/typescript-type-graphql': patch
'@graphql-codegen/typed-document-node': patch
'@graphql-codegen/typescript': patch
'@graphql-codegen/typescript-urql': patch
'@graphql-codegen/typescript-vue-apollo': patch
'@graphql-codegen/graphql-modules-preset': patch
'@graphql-codegen/config-schema': patch
'@graphql-codegen/plugin-helpers': patch
---

fix(visitor-plugin-common): guard for a runtime type error
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ export class SelectionSetToObject<Config extends ParsedDocumentsConfig = ParsedD
for (const { field, selectedFieldType } of linkFieldSelectionSets.values()) {
const realSelectedFieldType = getBaseType(selectedFieldType as any);
const selectionSet = this.createNext(realSelectedFieldType, field.selectionSet);
const isConditional = hasConditionalDirectives(field.directives);
const isConditional = hasConditionalDirectives(field);

linkFields.push({
alias: field.alias ? this._processor.config.formatNamedField(field.alias.value, selectedFieldType) : undefined,
Expand All @@ -397,7 +397,7 @@ export class SelectionSetToObject<Config extends ParsedDocumentsConfig = ParsedD
...this._processor.transformPrimitiveFields(
parentSchemaType,
Array.from(primitiveFields.values()).map(field => ({
isConditional: hasConditionalDirectives(field.directives),
isConditional: hasConditionalDirectives(field),
fieldName: field.name.value,
}))
),
Expand Down
13 changes: 3 additions & 10 deletions packages/plugins/other/visitor-plugin-common/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import {
isListType,
isAbstractType,
GraphQLOutputType,
DirectiveNode,
} from 'graphql';
import { ScalarsMap, NormalizedScalarsMap, ParsedScalarsMap } from './types';
import { DEFAULT_SCALARS } from './scalars';
Expand Down Expand Up @@ -403,15 +402,9 @@ export function getPossibleTypes(schema: GraphQLSchema, type: GraphQLNamedType):
return [];
}

export function hasConditionalDirectives(directives: readonly DirectiveNode[]): boolean {
if (directives.length === 0) return false;

for (const directive of directives) {
if (['skip', 'include'].includes(directive.name.value)) {
return true;
}
}
return false;
export function hasConditionalDirectives(field: FieldNode): boolean {
const CONDITIONAL_DIRECTIVES = ['skip', 'include'];
return field.directives?.some(directive => CONDITIONAL_DIRECTIVES.includes(directive.name.value));
}

type WrapModifiersOptions = {
Expand Down

0 comments on commit d9212aa

Please sign in to comment.