graphql-code-generator icon indicating copy to clipboard operation
graphql-code-generator copied to clipboard

Mapped types are dropped when combined with unions at non-trivial depth.

Open jacob-indieocean opened this issue 5 years ago • 7 comments

Issue workflow progress

Progress of the issue based on the Contributor Workflow

Make sure to fork this template and run yarn generate in the terminal.

Please make sure the Codegen and plugins version under package.json matches yours.

  • [ ] 2. A failing test has been provided

  • [ ] 3. A local solution has been provided

  • [ ] 4. A pull request is pending review


Describe the bug The resolver type for a type that contains a type that contains a union of types that are mapped, drops knowledge of the mapped type.

That a mouthful, but here is a minimal reproduction:

To Reproduce LIve Code Sandbox: https://codesandbox.io/s/inspiring-shape-ts0t5

  1. My GraphQL schema:
type Query {
    feed: FeedConnection!
}

type FeedConnection {
    edge:FeedEdge
}

type FeedEdge {
    node: FeedNode
}

union FeedNode = PostFeedNode | PhotoFeedNode

type PostFeedNode {
    text:String
}

type PhotoFeedNode {
    url:String
}
  1. My GraphQL operations: N/A

  2. My codegen.yml config file:

schema: schema.graphql
generates:
  types.ts:
    plugins:
      - typescript
      - typescript-resolvers
    config:
      mappers:
        PostFeedNode: ./model#PostFeedNodeModel
        PhotoFeedNode: ./model#PhotoFeedNodeModel

Expected behavior The resolver type should be: FeedConnection:ResolverTypeWrapper<Omit<FeedConnection, 'edge'> & { node?: Maybe<ResolversTypes['FeedEdge']> }>;

But the generated type is: FeedConnection: ResolverTypeWrapper<FeedConnection>;

jacob-indieocean avatar May 14 '21 05:05 jacob-indieocean

Hi @jacob-indieocean and thank you for the report!

Sorry but I'm not adding a lot here but just labeling it according to our new Contribution Guide and issue flow.

It seems already got into stage 1 thanks to your reproduction! Thank you for that!

Now in order to advance to stage 2 we'll need a failing test, would be great if someone could help progress the issues through the stages.

Thank you and sorry that this comment is not a complete solution (yet).

Urigo avatar May 14 '21 10:05 Urigo

Just thought I would add a little bit of context that I probably should have mentioned right from the start, for the sake of SEO and for the benefit of users struggling with a similar use case.

I alluded to it in the names for the types in the schema, but to be more explicit, I'm running in to this issue while conforming to the Relay spec for connections, where the node type is a union. This is probably a fairly common use case and I am surprised it has not come up already.

Actually, maybe you have seen it before. Is this a regression of #3979?

jacob-indieocean avatar May 14 '21 15:05 jacob-indieocean

Is this a regression of #3979?

Checking should be as easy as upgrading my repro to the latest version of the codegen.

darkbasic avatar May 18 '21 17:05 darkbasic

Also bumped into this today!

mengyazhu96 avatar Aug 04 '21 16:08 mengyazhu96

Bumping this again as we just ran into it.

aitskovi avatar Apr 21 '22 20:04 aitskovi

is anyone up to move this into stage 2 by creating a failing test?

Urigo avatar Apr 23 '22 13:04 Urigo

This no longer reproduces on current master (b71b9d5). With mappers for PostFeedNode and PhotoFeedNode, FeedEdge.node resolves to PostModel | PhotoModel in ResolversTypes. Probably safe to close.

nomadical avatar Nov 25 '26 01:11 nomadical