dotansimha / dotansimha/graphql-code-generator-community
Useless error description when using near-operation-file and unions
- Dominant language
- TypeScript
- Stars
- 137
- Forks
- 195
- Avg merge
- 6h 20m
- Merged PRs (30d)
- 16
Description
### 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:
```graphql
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
}
```
2. My GraphQL operations:
```graphql
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 }
}
}
```
3. My `codegen.yml` config file:
```yml
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…
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.