dotansimha / dotansimha/graphql-code-generator

[typescript-operations] operation type not restricted to inline fragment

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

Description

**Describe the bug**
typescript-operations does not appear to take into account the specific inline fragments used, instead generating a type that could include anything from the returned interface.

**To Reproduce**
Have two or more types that share an interface.

Define a query that returns the interface, but only include one of the types in an inline fragment.

Codesandbox: https://codesandbox.io/s/long-http-1wibc

1. My GraphQL schema:

```graphql
type Query {
entries(type: [String]): EntryInterface
}

interface EntryInterface {
title: String
}

type first_Entry implements EntryInterface {
title: String
first: String
}

type second_Entry implements EntryInterface {
title: String
second: String
}
```

2. My GraphQL operations:

```graphql
query first {
entries(type: "first") {
...on first_Entry {
__typename
title
first
}
}
}

```

3. My `codegen.yml` config file:

```yml
schema: schema.graphql
documents: document.graphql
generates:
types.ts:
plugins:
- typescript
- typescript-operations
```

**Expected behavior**

`FirstQuery` is generated as follows:

```ts
export type FirstQuery = {
__typename ? : 'Query',
entries ? : {
__typename: 'first_Entry',
title ? : string | null | undefined,
first ? : string | null | undefined
} | {
__typename ? : 'second_Entry'
} | null | undefined
};
```

The expected type would be:

```ts
export type FirstQuery = {
__typename ? : 'Query',
entries ? : {
__typename: 'first_Entry',
title ? : string | null | undefined,
first ? : string | null | undefined
} | null | undefined
};
```

(i.e. no `{ __typename?: 'second_Entry' }` in the union).

**Environment:**

- OS: MacOS
- `@graphql-codegen/typescript`: 2.2.4
- `@graphql-codegen/typescript-operations`: 2.1.8
- NodeJS: 14

**Additional context**
In typescript-operations v1, when using `skipTypename: true` objects only containing `__typename?` would be removed, so this could be avoided. In v2 the union will include `{}` instead.

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.