Skip to content

skip/include directives do not avoidOptionals if field type is a list #6748

@brainkim

Description

@brainkim

Describe the bug

The @skip and @include directives seem to be generating optional types for GraphQLList fields, even when avoidOptionals is true.

To Reproduce
Steps to reproduce the behavior:
https://codesandbox.io/s/practical-swanson-b93wg

  1. My GraphQL schema:
type Query {
  me: User!
}

type User {
  messages: [Message!]!
}

type Message {
  content: String!
}
  1. My GraphQL operations:
query MyQuery($include: Boolean!) {
  me {
    messages @include(if: $include) {
      content
    }
  }
}
  1. My codegen.yml config file:
schema: schema.graphql
documents: document.graphql
generates:
  types.ts:
    plugins:
      - typescript
      - typescript-operations
config:
  avoidOptionals: true
  nonOptionalTypename: true

Expected behavior

I expect TypeScript optionals not to be included in the generated output.

Actual behavior

export type MyQueryQuery = (
  { __typename: 'Query' }
  & { me: (
    { __typename: 'User' }
    & { messages?: Maybe<(
      { __typename: 'Message' }
      & Pick<Message, 'content'>
    )> }
  ) }
);

messages is optional.

Environment:

  • OS: MacOS Big Sur (11.6)
  • "@graphql-codegen/add": "2.0.2",
  • "@graphql-codegen/cli": "1.20.1",
  • "@graphql-codegen/typescript": "1.21.0",
  • "@graphql-codegen/typescript-operations": "1.17.14",
  • "graphql": "15.5.0"

Additional context

Probably related to this PR:
#5104

Metadata

Metadata

Assignees

Labels

stage/5-alpha-release-testingThe pull request is merged, an alpha release is available, to be tested

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions