graphql-code-generator-community icon indicating copy to clipboard operation
graphql-code-generator-community copied to clipboard

Useless error description when using near-operation-file and unions

Open eddeee888 opened this issue 11 months ago • 3 comments

Describe the bug

Original Issue: https://github.com/dotansimha/graphql-code-generator/issues/7515

Describe the bug When using the @graphql-codegen/near-operation-file-preset to generate type definitions via the typescript plugin, the error messages when the query is invalid misses all relevent information to actually fix the problem.

Instead there are two different types of errors thrown:

when querying for fields that do not exist on all types of the union

TypeError: Cannot read properties of undefined (reading 'type')
        at node_modules/@graphql-codegen/visitor-plugin-common/index.js:3038:65

when querying for an object that does not exist

lastType.getFields is not a function
    Error: Unable to validate GraphQL document!
        at node_modules/@graphql-codegen/near-operation-file-preset/index.js:206:19

To Reproduce Steps to reproduce the behavior:

https://codesandbox.io/s/graphql-codegen-near-operation-file-error-5n915?file=/document.graphql

  1. My GraphQL schema:
type A {
  id: Int!
}

type B {
  id: Int!
  fieldOnlyOnB: String
}

union AorB = B | A

type Query {
  getAList: [A!]!
  getBList: [B!]!
  getABList: [AorB!]!
}

schema {
  query: Query
}

  1. My GraphQL operations:
query {
  getABList {
    id
    # this works as expected, since it is perfectly valid
    ... on B {
      fieldOnlyOnB
    }

    # throws "Cannot read properties of undefined (reading 'type')"
    fieldOnlyOnB

    # throws "lastType.getFields is not a function"
    invalidField { invalidSubField }
  }
}
  1. My codegen.yml config file:
schema: schema.graphql
documents: document.graphql
generates:
  types.ts:
    # comment out the nexxt line and you will see proper error messages
    preset: near-operation-file
    presetConfig: 
      extension: .types.ts
      baseTypesPath: "types.ts"
    plugins:
      - typescript

Expected behavior Proper, useful error mesages about errors in my query. The same as if I was not using the near-operation-file preset.

In the sandbox, just comment out the line preset: near-operation-file in codegen.yml and you will see the error messages that I expected.

Environment:

  • OS: MacOS 12.2
  • @graphql-codegen/add: ^3.1.1
  • @graphql-codegen/cli: ^2.4.0
  • @graphql-codegen/near-operation-file-preset: 2.2.4
  • @graphql-codegen/typescript: 2.4.3
  • @graphql-codegen/typescript-operations: 2.2.4
  • graphql: ^16.2.0
  • NodeJS: v16.10.0

Additional context Obviously this is a very simplifyed reproduction case. Originally the problem occurred when we made a few changes to our API to use union types in certain situations. When trying to run codegen afterwards, not having proper error messages made it very hard to update the client queries…

eddeee888 avatar Dec 29 '24 13:12 eddeee888

Seems like an issue in this block

eddeee888 avatar Dec 29 '24 13:12 eddeee888

@eddeee888 thanks! seem like a simple fix?

dotansimha avatar Dec 30 '24 09:12 dotansimha

Potentially! I just haven't had a chance to look deep enough

eddeee888 avatar Dec 30 '24 12:12 eddeee888