Graphql-code-generator: external enumValues with typesPrefix won't work together

Created on 1 Jul 2019  路  11Comments  路  Source: dotansimha/graphql-code-generator

Describe the bug

If we have typesPrefix set to some value and enumValues point to a file then import value in the generated output won't have required type prefix and hence type validation fails.

To Reproduce
Steps to reproduce the behavior:

  1. My GraphQL schema:

  1. My GraphQL operations:
# Put your operations here
  1. My codegen.yml config file:
# Put your schema here
.....
    config:
        enumValues:
          MyEnum: ./files#MyEnum
        typesPrefix: I
...

Generated Output

import { MyEnum } from "./files";
export type IResolversTypes = {
....
MyEnum: IMyEnum
}

Expected behavior

Environment:

  • OS:
  • @graphql-codegen/...:
  • NodeJS:

Additional context

bug plugins waiting-for-release

Most helpful comment

@dotansimha Thanks that fixed it!.

The reason we have the react-apollo in the generated file for two reasons.

  1. We need common queries to be packaged so we can ship one package such that each microservice can use to call the gateway server.
  2. It is hard to have two separate files with types & gql queries in one and react generated code in another as mentioned in https://github.com/dotansimha/graphql-code-generator/issues/1975

All 11 comments

Fixed in: https://github.com/dotansimha/graphql-code-generator/pull/2121
@veeramarni can you please test it with the latest alpha? 1.3.1-alpha-77551f3e.79

Actually needed to do a small fix for typescript-resolvers to make it work there as well. Available as 1.3.1-alpha-70a40ede.80.

@dotansimha I have tried after updating all libraries to 1.3.1-alpha-70a40ede.80 but still have same issue.

import { MyEnum } from "./files";
export type IResolversTypes = {
....
MyEnum: IMyEnum
}

@veeramarni can you please share a reproduction in a repo? (or codesandbox)
I added a test that seems to pass: https://github.com/dotansimha/graphql-code-generator/pull/2121/files#diff-f7bb64df03f464913f48980ead990c1aR224

I also added some more changes and refactored enumsValues a bit. Can you please try it? 1.3.1-alpha-789526ab.83

For some reason, we cannot replicate in the codesandbox not sure why we still have the issue.

As I see now, enums will won't have the prefix: I, but in the code generated in our project has I prefix.

https://codesandbox.io/embed/graphql-codegen-issue-template-rdsel

I'm able to reproduce the issue after adding - typescript-react-apollo plugin.

https://codesandbox.io/embed/graphql-codegen-issue-template-rdsel

@veeramarni

Make sure to remove all modifiers from the version on package.json, it shouldn't have any ^ or ~, because otherwise you'll get the wrong alpha version.

I tested it now, and it seems to work fine, am I missing something? (try to clear types.ts in the sandbox editor and save it, before running yarn codegen again, and then re-open type.ts).

I got this output:

import { MyEnum } from "./files";


// ...

export type IUser = {
  id: Scalars["ID"];
  username: Scalars["String"];
  email: Scalars["String"];
  eum?: Maybe<MyEnum>;
};

// ...

export type IResolversTypes = {
  Query: ResolverTypeWrapper<{}>;
  ID: ResolverTypeWrapper<Scalars["ID"]>;
  User: ResolverTypeWrapper<IUser>;
  String: ResolverTypeWrapper<Scalars["String"]>;
  MyEnum: MyEnum;
  Boolean: ResolverTypeWrapper<Scalars["Boolean"]>;
};

example here: https://codesandbox.io/s/graphql-codegen-issue-template-i2i72

btw, any reason to use both operations + react-apollo with resolvers? In most cases, they should be in different files.

@dotansimha Thanks that fixed it!.

The reason we have the react-apollo in the generated file for two reasons.

  1. We need common queries to be packaged so we can ship one package such that each microservice can use to call the gateway server.
  2. It is hard to have two separate files with types & gql queries in one and react generated code in another as mentioned in https://github.com/dotansimha/graphql-code-generator/issues/1975

Makes sense, I'm glad that it helped :)

Keeping open because we are tracking the waiting-for-release issues :)

Had the same problem and this fixed it for me too. 馃憤

Available in 1.4.0 馃帀

Was this page helpful?
0 / 5 - 0 ratings