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

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

Open
eddeee888 opened this issue Dec 29, 2024 · 3 comments
Open

Comments

@eddeee888
Copy link

eddeee888 commented Dec 29, 2024

Describe the bug

Original Issue: dotansimha/graphql-code-generator#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
Copy link
Author

Seems like an issue in this block

@dotansimha
Copy link
Owner

@eddeee888 thanks! seem like a simple fix?

@eddeee888
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants