Skip to content

Commit

Permalink
@Skip @include Arrays fix (#6204)
Browse files Browse the repository at this point in the history
* fix array issue

* changeset
  • Loading branch information
gilgardosh committed Jun 29, 2021
1 parent df19f91 commit 6762aff
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 4 deletions.
6 changes: 6 additions & 0 deletions .changeset/wet-panthers-kiss.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@graphql-codegen/visitor-plugin-common': patch
'@graphql-codegen/typescript-operations': patch
---

Fix for array types with @skip @include directives
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
PrimitiveField,
} from './base';
import { GraphQLObjectType, GraphQLInterfaceType, isEnumType, isNonNullType } from 'graphql';
import { getBaseType } from '@graphql-codegen/plugin-helpers';
import { getBaseType, removeNonNullWrapper } from '@graphql-codegen/plugin-helpers';

export class PreResolveTypesProcessor extends BaseSelectionSetProcessor<SelectionSetProcessorConfig> {
transformTypenameField(type: string, name: string): ProcessResult {
Expand All @@ -34,6 +34,7 @@ export class PreResolveTypesProcessor extends BaseSelectionSetProcessor<Selectio
let typeToUse = baseType.name;

const useInnerType = field.isConditional && isNonNullType(fieldObj.type);
const innerType = useInnerType ? removeNonNullWrapper(fieldObj.type) : undefined;

if (isEnumType(baseType)) {
typeToUse =
Expand All @@ -43,8 +44,8 @@ export class PreResolveTypesProcessor extends BaseSelectionSetProcessor<Selectio
typeToUse = this.config.scalars[baseType.name];
}

const name = this.config.formatNamedField(field.fieldName, useInnerType ? baseType : fieldObj.type);
const wrappedType = this.config.wrapTypeWithModifiers(typeToUse, useInnerType ? baseType : fieldObj.type);
const name = this.config.formatNamedField(field.fieldName, useInnerType ? innerType : fieldObj.type);
const wrappedType = this.config.wrapTypeWithModifiers(typeToUse, useInnerType ? innerType : fieldObj.type);

return {
name,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4939,6 +4939,8 @@ function test(q: GetEntityBrandDataQuery): void {
type User {
name: String!
address: String!
nicknames: [String!]
parents: [User!]!
}
`);

Expand All @@ -4947,6 +4949,8 @@ function test(q: GetEntityBrandDataQuery): void {
user {
name
address @include(if: $showAddress)
nicknames @include(if: $showNicknames)
parents @include(if: $showParents)
}
}
`);
Expand All @@ -4968,7 +4972,7 @@ function test(q: GetEntityBrandDataQuery): void {
}>;
export type UserQuery = { __typename?: 'Query', user: { __typename?: 'User', name: string, address?: Maybe<string> } };`);
export type UserQuery = { __typename?: 'Query', user: { __typename?: 'User', name: string, address?: Maybe<string>, nicknames?: Maybe<Array<string>>, parents?: Maybe<Array<User>> } };`);
});

it('objects with @skip, @include should pre resolve into optional', async () => {
Expand Down

0 comments on commit 6762aff

Please sign in to comment.