Graphql-code-generator: Is there a way to have a combined type instead of a collection of <Pick...>'s?

Created on 7 Nov 2019  路  3Comments  路  Source: dotansimha/graphql-code-generator

Hi guys

It was kind of difficult to explain my question in the title but I'll try to elaborate a bit more.

When I use GraphQL Codegen to generate TypeScript types for my operations, the return values of said operations are never just 1 type but a collection of <Pick >'s.

To illustrate:

Screenshot 2019-11-07 at 09 27 10

The problem is that, when you have a query that returns a list of values, like for example a list of Posts, there is no way to reference the type of an item in that list since it is not stored separately. It is just that list of <Pick>'s as seen in the screenshot.

Is there a way to alter my codegen setup to make this work? I had no problems getting this result while using Apollo Codegen but I really would love it if I could just use GraphQL Codegen for everything going forward.

Or maybe there is a typescript feature that would let me reference the type of one array element?

Thanks for the help!

Most helpful comment

You can use preResolveTypes: true to resolve the primitives. For the subtypes you may want to create Fragments since those also get typed explicitly.

All 3 comments

I'm not sure if codegen provides an option to generate these types, but you should be able to get what you need with

type TechnicianJob = JobsForTechnicianQuery['jobsForTechnician']['nodes'][0];

[0] extracts the type of the first array element. You can specify any index here, as all elements in a TypeScript array have the same type.

You can use preResolveTypes: true to resolve the primitives. For the subtypes you may want to create Fragments since those also get typed explicitly.

I'm closing this issue now. You can use @pachuka 's solution to get rid of Picks in your output. Feel free to open a new one if you have another issue.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

bastman picture bastman  路  3Comments

edorivai picture edorivai  路  3Comments

leonardfactory picture leonardfactory  路  3Comments

dotansimha picture dotansimha  路  3Comments

NickClark picture NickClark  路  3Comments