Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeError when spreading a fragment on a field of a different type #7364

Closed
cecchi opened this issue Jan 13, 2022 · 1 comment
Closed

TypeError when spreading a fragment on a field of a different type #7364

cecchi opened this issue Jan 13, 2022 · 1 comment
Labels
core Related to codegen core/cli stage/6-released The issue has been solved on a released version of the library

Comments

@cecchi
Copy link

cecchi commented Jan 13, 2022

Describe the bug

This seems likely to be related to #6874.

We have many union types in our schema, especially to define different groups of identity types (e.g. users, admins, or system users). It is technically valid GraphQL to define a fragment on one of these union types and spread it on a field that is a different union type, as long as the individual members of the union are compatible.

This worked without issue when using core@1.17.10 with visitor-plugin-common@1.21.1. After upgrading to core@2.4.0 and visitor-plugin-common@2.5.2 a TypeError is thrown (we are using the near-operation-file-preset, if relevant).

To Reproduce

Here's a code sandbox demonstrating the issue. I did my best to reduce the issue to the smallest reproduction possible.

https://codesandbox.io/s/silly-pateu-6dzgf?file=/src/document.gql

  1. My GraphQL schema:
scalar AnyId

### IDENTITIES

type ConsumerUser {
  id: AnyId!
  firstName: String!
  lastName: String!
}

type AdminUser {
  id: AnyId!
  firstName: String!
  lastName: String!
}

type SystemUser {
  id: AnyId!
}

### TICKETS

type Ticket {
  id: AnyId!
  createdBy: TicketAuthor
}

union TicketAuthor = AdminUser | SystemUser
union TicketRequester = ConsumerUser | AdminUser | SystemUser 

### QUERIES

type Query {
  ticket: Ticket!
}
  1. My GraphQL operations:
# This fragment is defined on type TicketRequester: a union of ConsumerUser | AdminUser | SystemUser
fragment TicketRequester on TicketRequester {
  ... on ConsumerUser {
    id
    userFirstName: firstName
    userLastName: lastName
  }
  ... on AdminUser {
    id
    operatorFirstName: firstName
    operatorLastName: lastName
  }
  ... on SystemUser {
    id
  }
}

query Ticket {
  ticket {
    id
    # This field is of type TicketAuthor, NOT TicketRequester
    # TicketAuthor is a union of AdminUser | SystemUser
    createdBy {
      ...TicketRequester
    }
  }
}
  1. My codegen.yml config file:
schema: schema.graphql
documents: src/**/*.gql
generates:
  src/generated/types.ts:
    - typescript
  src/:
    preset: near-operation-file
    presetConfig:
      extension: .generated.tsx
      baseTypesPath: generated/types.ts
    plugins:
      - typescript-operations
      - typescript-react-apollo
    config: 
      skipTypename: false
      nonOptionalTypename: true
      avoidOptionals: false
      enumsAsConst: true
      withHOC: false
      withComponent: true
      withHooks: true
      withMutationFn: true
      withResultType: true
      withMutationOptionsType: true
      documentMode: DocumentMode.documentNodeImportFragments
      dedupeFragments: true
      optimizeDocumentNode: true
      skipDocumentsValidation: true
      useTypeImports: true

Expected behavior
The code generation should succeed, or at a minimum should fail to validate if it's determined that this is invalid GraphQL (our server and previous version of graphql-codegen have been fine with this syntax historically).

Actual behavior

TypeError: Cannot read property 'type' of undefined
        at /sandbox/node_modules/@graphql-codegen/visitor-plugin-common/index.js:3068:69
        at Array.map (<anonymous>)
        at PreResolveTypesProcessor.transformAliasesPrimitiveFields (/sandbox/node_modules/@graphql-codegen/visitor-plugin-common/index.
js:3058:23)
        at SelectionSetToObject.buildSelectionSetString (/sandbox/node_modules/@graphql-codegen/visitor-plugin-common/index.js:2880:32)
        at /sandbox/node_modules/@graphql-codegen/visitor-plugin-common/index.js:2762:41
        at Array.reduce (<anonymous>)
        at SelectionSetToObject._buildGroupedSelections (/sandbox/node_modules/@graphql-codegen/visitor-plugin-common/index.js:2752:80)
        at SelectionSetToObject.transformSelectionSet (/sandbox/node_modules/@graphql-codegen/visitor-plugin-common/index.js:2921:54)
        at SelectionSetToObject.buildSelectionSetString (/sandbox/node_modules/@graphql-codegen/visitor-plugin-common/index.js:2870:89)
        at /sandbox/node_modules/@graphql-codegen/visitor-plugin-common/index.js:2762:41
@saihaj
Copy link
Collaborator

saihaj commented Dec 1, 2022

@saihaj saihaj closed this as completed Dec 1, 2022
@saihaj saihaj added the stage/6-released The issue has been solved on a released version of the library label Dec 1, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
core Related to codegen core/cli stage/6-released The issue has been solved on a released version of the library
Projects
None yet
Development

No branches or pull requests

3 participants