dotansimha / dotansimha/graphql-code-generator-community

[Flow] wrong type of nullable operation using inline fragments (union)

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

Description

**Describe the bug**
There is a query operation with nullable return type (graphql interface). It uses graphql inline fragments (union) based on two types implementing the interface. In result of graphql code generation to flow types first branch of the union is marked as [maybe type](https://flow.org/en/docs/types/maybe/), but second one is not. See below repository and instructions for more details.

**To Reproduce**
Steps to reproduce the behavior:
There is a [repository](https://github.com/olyverDev/graphql-codegen-flow-example) prepared for reproduction. You can just review the code or re-run codegen script on your own.

1. My GraphQL schema:

```graphql
type Query {
getEntity: Entity # the key moment return type is nullable
}

interface Entity {
field: String!
}

type SubEntityA implements Entity {
field: String!
subEntityFieldA: String!
}

type SubEntityB implements Entity {
field: String!
subEntityFieldB: String!
}
```

2. My GraphQL operations:

```graphql
fragment SubEntityFragment on Entity {
... on SubEntityA {
subEntityFieldA
}
... on SubEntityB {
subEntityFieldB
}
}

query getEntity {
data: getEntity {
...SubEntityFragment
field
}
}
```

3. My `codegen.yml` config file:

```yml
schema: ./schema.graphql
documents:
- ./query.js
generates:
./graphql.types.js:
plugins:
- flow
- flow-operations
config:
preResolveTypes: true
```

**Expected behavior**

It is expected that all branches of generated query type would be marked as maybe types.

**Environment:**

- OS: MacOS Big Sur version 11.12.3, Windows 10
- `@graphql-codegen/add`: 2.0.2
- `@graphql-codegen/cli`: 1.21.3
- `@graphql-codegen/flow`: 1.18.5
- `@graphql-codegen/flow-operations`: 1.18.7
- NodeJS: v12.13.0

Contributor guide

No contributing guide indexed for this repository

Research direction

Start by rerunning the codegen script in the linked reproduction repository using schema.graphql, query.js, and the shown codegen.yml, then inspect graphql.types.js. Trace how the flow and flow-operations plugins represent nullable interface results with inline fragments; done means every generated union branch is marked as a maybe type.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.