dotansimha / dotansimha/graphql-code-generator

[typescript-operations] Conflicting types for operation and exported types

Open
#7,165 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

**Describe the bug**
I am unable to use React Hooks because the generated exported type is not compatible with the query operation type.

```typescript
export type V1Deployment = {
__typename?: 'V1Deployment';
/** APIVersion defines the versioned schema of this representation of an object. Servers should convert recognized schemas to the latest internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources */
apiVersion?: Maybe;
/** Kind is a string value representing the REST resource this object represents. Servers may infer this from the endpoint the client submits requests to. Cannot be updated. In CamelCase. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds */
kind?: Maybe;
/** ObjectMeta is metadata that all persisted resources must have, which includes all objects users must create. */
metadata?: Maybe;
/** DeploymentSpec is the specification of the desired behavior of the Deployment. */
spec?: Maybe;
/** DeploymentStatus is the most recently observed status of the Deployment. */
status?: Maybe;
};
```
Does not end up being compatible at compile time with

```typescript
export type GetDeploymentQuery = (
{ __typename?: 'Query' }
& { getDeployment?: Maybe<(
{ __typename?: 'V1Deployment' }
& Pick
& { metadata?: Maybe<(
{ __typename?: 'V1ObjectMeta' }
& Pick
)>, spec?: Maybe<(
{ __typename?: 'V1DeploymentSpec' }
& { template: (
{ __typename?: 'V1PodTemplateSpec' }
& { spec?: Maybe<(
{ __typename?: 'V1PodSpec' }
& { containers: Array
)>> }
)> }
) }
)>, status?: Maybe<(
{ __typename?: 'V1DeploymentStatus' }
& Pick
)> }
)> }
);
```
To fix this I changed the generated GetDeploymentQuery code to the following ...

```typescript
export type GetDeploymentQuery = (
{ __typename?: 'Query' }
& { getDeployment?: V1Deployment}
);
```

**To Reproduce**
1. https://github.com/joshughes/graphql-code-generator-issue-sandbox-template
2. npm install
3. open main.ts to see compile-time typescript error

https://github.com/joshughes/graphql-code-generator-issue-sandbox-template/pull/2/files

The PR above makes typescript happy

**Expected behavior**
I would expect the exported type to be used in the operation type, so these sort of conflicts do not occur. I could not find an option to ensure those were reused.

**Environment:**

- OS: Ununto 20.04
- "@apollo/client": "^3.5.6",
- "@graphql-codegen/add": "2.0.2",
- "@graphql-codegen/cli": "1.20.1",
- "@graphql-codegen/typescript": "1.21.0",
- "@graphql-codegen/typescript-operations": "1.17.14",
- "@graphql-codegen/typescript-react-apollo": "^3.2.2",
- NodeJS: 12.17.0

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.