dotansimha / dotansimha/graphql-code-generator-community

Data should be null when an error occurs

Open
#670 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
TypeScript
Stars
137
Forks
195
Avg merge
6h 20m
Merged PRs (30d)
16

Description

### Which packages are impacted by your issue?

@graphql-codegen/typescript-react-apollo

### Describe the bug

If `errorPolicy` is set to `all` (for @apollo/client) and an error occurs GQL returns `data` = `null`, but the type of `data` variable doesn't contain `null`, just `undefined`.

GQL returns:
```json
{
"data": null,
"errors": [
{
"message": "Example error",
"locations": [
{
"line": 3,
"column": 9
}
],
"path": [
"foo",
"name"
]
}
]
}
```

But in the type `null` is missing:
```typescript
const { data } = useFooQuery();

// data is: FooQuery | undefined
// but should be: FooQuery | null
```

### Your Example Website or App

https://github.com/josef-ct/graphql-codegen-missing-null-issue

### Steps to Reproduce the Bug or Issue

1. generate the code (types & hooks)
2. use the hook ```const { data, loading, error } = useFooQuery();```
3. the type of `data` is `FooQuery | undefined`

### Expected behavior

I would expect the type should reflect the real value, which is `null` in this case.

### Screenshots or Videos

_No response_

### Platform

- OS: macOS, Windows
- NodeJS: 20.11.0
- `graphql` version: 16.8.1
- `@graphql-codegen/cli`, `@graphql-codegen/typescript-react-apollo` version(s): 5.0.2, 4.3.0

### Codegen Config File

import type { CodegenConfig } from '@graphql-codegen/cli';

const config: CodegenConfig = {
overwrite: true,
schema: 'http://localhost:3333',
documents: 'src/**/!(graphql).ts(x)?',
generates: {
'src/generated/graphql.ts': {
plugins: [
{
add: {
// nemohu ignorovat přes .eslintignore, protože to ignoruje i typy a eslint si myslí
// že věci z graphql-js jsou any
content: '/* eslint-disable */',
},
},
'typescript',
'typescript-react-apollo',
'typescript-operations',
],
},
'./graphql.schema.json': {
plugins: ['introspection'],
},
},
config: {
withHooks: true,
useExplicitTyping: true,
},
};

export default config;

### Additional context

_No response_

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.