dotansimha / dotansimha/graphql-code-generator

Nullability incorrect for fields on Interface/Union types

Open
#10,304 7 comments 0 reactions 1 assignee Claimed by @eddeee888 View on GitHub
Dominant language
TypeScript
Stars
11.3k
Forks
1.4k
Avg merge
1d 1h
Merged PRs (30d)
23

Description

### Which packages are impacted by your issue?

@graphql-codegen/core

### Describe the bug

Codegen is making the assumption that an Interface or Union wont change between codegen-time and execution time.

### Your Example Website or App

n/a

### Steps to Reproduce the Bug or Issue

Consider the following schema:
```
type Query { appConfiguration: AppConfiguration! }

interface AppConfiguration {
foo: Int # **nullable**
}

type iPhoneAppConfiguration implements AppConfiguration {
foo: Int! # **non-nullable**
}
```

And a query:
```
query AppConfiguration {
appConfiguration {
foo
}
}
```

The generated type looks like:
```
export type AppConfigurationQuery = {
__typename?: "Query"
appConfiguration: {
__typename?: "iPhoneAppConfiguration"
foo: number
}
}
```

Because `iPhoneAppConfiguration` is the only concrete implementation of AppConfiguration, codegen makes `foo` non-nullable. However, adding a new implementation of AppConfiguration is a non-breaking server change and could result in `foo` being null. It seems incorrect to assume an interface or union wont change between codegen time and when a query is executed.

### Expected behavior

I would expect the `foo` field to be nullable

### Screenshots or Videos

_No response_

### Platform

"@graphql-codegen/cli": "5.0.5",
"@graphql-codegen/schema-ast": "^4.1.0",
"@graphql-codegen/typescript": "4.1.5",

### Codegen Config File
```typescript
import type { CodegenConfig } from "@graphql-codegen/cli"

const config: CodegenConfig = {
schema: "http://localhost:5173/api/graphql",
documents: ["src/**/*.ts"],
ignoreNoDocuments: true,
generates: {
"./src/graphql/": {
preset: "client",
config: {
documentMode: "string",
},
},
"./schema.graphql": {
plugins: ["schema-ast"],
config: {
includeDirectives: 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.