dotansimha / dotansimha/graphql-code-generator

Avoid extra __typename when using inlineFragmentTypes: combine

Open
#8,320 0 comments 2 reactions 0 assignees View on GitHub
core
Dominant language
TypeScript
Stars
11.3k
Forks
1.4k
Avg merge
1d 1h
Merged PRs (30d)
23

Description

**Is your feature request related to a problem? Please describe.**

When using `inlineFragmentTypes: 'combine'`, query types gets a redundant `__typename` definition for each possible type. Since every fragment will get its own type, which also has the same `__typename` in it, adding another `__typename` by itself becomes redundant. By avoiding that, we could make the generated code more readable(_not really important for most people_), reduce the file sizes(_not crucial either_), and more importantly, have more readable IDE-provided type information.

Document:
```graphql
fragment UserSummaryFragment on User {
id
name
}

query getUsers {
users {
...UserSummaryFragment
}
}
```

Generated code:
```ts
export type UserSummaryFragment = {
__typename?: 'User';
id: string;
name: string;
email: string;
avatar: string;
};

export type GetUsersQueryVariables = Types.Exact<{ [key: string]: never }>;

export type GetUsersQuery = {
users: Array<{ __typename?: 'User' } & UserSummaryFragment>;
};
```

When hovering over the type:
![image](https://user-images.githubusercontent.com/6266078/188285203-761170e2-fb45-4d53-b0b0-6a8429f2063c.png)

This gets even worse when using it with `preResolveTypes: false`:
![image](https://user-images.githubusercontent.com/6266078/188285364-bbf6b8d5-7b4b-43c6-9783-fbc8aa01be04.png)

**Describe the solution you'd like**

Generated code:
```ts
# ...
export type GetUsersQuery = { users: Array };
```

When hovering over the type:
![image](https://user-images.githubusercontent.com/6266078/188285221-1a23dc4d-5a03-4652-933a-cb4317d4b7a9.png)
Has the same generated code(_query type part_) and IDE behavior when using `preResolveTypes: false`.

Of course, it would still need to generate `__typename` if there are no fragments.

**Describe alternatives you've considered**

To live with it 😭

**Additional context**
...

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.