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 #7515

Closed
whytspace opened this issue Feb 11, 2022 · 4 comments
Closed

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

whytspace opened this issue Feb 11, 2022 · 4 comments
Labels
kind/enhancement New feature or request presets Related to Codegen presets

Comments

@whytspace
Copy link

whytspace commented Feb 11, 2022

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…

@charlypoly charlypoly added kind/enhancement New feature or request presets Related to Codegen presets labels Mar 10, 2022
@emilio-martinez
Copy link

To be honest, I think this is a flat out bug – not an enhancement.

You can very easily run into scenarios where you run codegen and not even get an error for what should be an error. The above largely describes "useless" errors, but I didn't any any errors at all. Also as described above, this can easily be reproduced by removing preset: near-operation-file, and the errors will kick in.

@Negan1911
Copy link

This does not only happens on union cases, and it's pretty annoying not know whats the actual error and just get "Unable to validate GraphQL document"

@felipellrocha
Copy link

Any updates on how to get around this? I'm currently stuck on this issue

@eddeee888
Copy link
Collaborator

I've moved this issue to dotansimha/graphql-code-generator-community#934 because @graphql-codegen/near-operation-file-preset lives there.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/enhancement New feature or request presets Related to Codegen presets
Projects
None yet
Development

No branches or pull requests

6 participants