dotansimha / dotansimha/graphql-code-generator

enumValues with exported type break type safety

Open
#5,122 1 comment 0 reactions 1 assignee Claimed by @charlypoly View on GitHub
core plugins stage/1-reproduction
Dominant language
TypeScript
Stars
11.3k
Forks
1.4k
Avg merge
1d 1h
Merged PRs (30d)
23

Description

**Describe the bug**

When specifying `enumValues` and pointing to an exported type directly, the exported type could not match the enum at all.

This means the schema isn't enforced on the types imported and it breaks type safety.

**To Reproduce**
Steps to reproduce the behavior:

1. My GraphQL schema:

```graphql
enum Foobar {
FOO
BAR
}

type Something {
someArg: String!
someFoobar: Foobar!
}

input SomeInput {
foobar: Foobar!
}

type Query {
show(input: SomeInput!): String
}

```

2. My `codegen.yml` config file:

```yml
schema: ./**/*.graphql
generates:
./schema-types.ts:
plugins:
- typescript
config:
namingConvention:
enumValues: keep
typesPrefix: "Graphql"
enumPrefix: false
nonOptionalTypename: false
avoidOptionals: false
skipTypename: true
scalars:
Date: "string"
Cursor: "string"
JSON: "{ [key: string]: any }"
enumValues:
Foobar: ./index#Foobar

```

3. Sandbox link

-> https://codesandbox.io/s/exciting-haibt-xpoln?file=/codegen.yml

**Expected behavior**

As you can see in the generated types, `Foobar` is properly used from the exported existing type, itself imported from `index#Foobar`.

Because we delegated the enum to a re-export of a pre-defined enum, the schema is no longer the source of truth here, which means that I can have an enum that partially (or not at all) implements an enum defined in the schema.

Could the generator make an extra check on this to see if all schema-defined keys are present in the code-defined enum?

That would give a better level of type safety.

**Environment:**

See Sandbox: https://codesandbox.io/s/exciting-haibt-xpoln?file=/index.ts

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.