dgs-codegen icon indicating copy to clipboard operation
dgs-codegen copied to clipboard

Generate generic Pagination API for connections

Open JLLeitschuh opened this issue 1 year ago • 5 comments

When attempting to simply using dgs for a code generator for API clients, there aren't great solutions for generating generic pagination APIs. Each connection type is unique, and there doesn't exist a common interface for all edge/node types.

If you enable generateKotlinClosureProjections but also attempt to do type mapping for simple list connections, eg...

typeMapping["RecipeRunHistoryConnection"] = "graphql.relay.SimpleListConnection<$packageName.types.RecipeRunHistoryEdge>"

Then the projection's don't get generated correctly (you lose the projections for edges), meaning you can't request paginated elements.

Overall, this results in a less than stellar experience for trying to use dgs for pure client side code

JLLeitschuh avatar Jan 06 '24 04:01 JLLeitschuh

@mbossenbroek - could you please take a look when you get a chance?

srinivasankavitha avatar Jan 08 '24 22:01 srinivasankavitha

@JLLeitschuh you're right - when creating the projections I was converting the type to kotlin first & then deriving the name of the projection by picking the innermost type. Obviously this failed if it found a mapped generic type & picked the inner type of the edge instead of the connection.

This should fix it by looking instead at the name of the GQL type itself for determining the name of the projection, effectively ignoring the mapped types when building the client. I updated one of the examples to use the relay mapping as well to confirm the behavior: https://github.com/Netflix/dgs-codegen/pull/642

mbossenbroek avatar Jan 08 '24 23:01 mbossenbroek

@srinivasankavitha looks like the build passed on that PR; feel free to merge/release at your convenience

mbossenbroek avatar Jan 08 '24 23:01 mbossenbroek

Does SimpleListConnection work for client types? I'm hoping it does.

Also, couldn't DGS automatically detect pagination structures and auto-generate the correct paginated API for the model types?

JLLeitschuh avatar Jan 09 '24 05:01 JLLeitschuh

I'm not sure I follow what you mean by this:

Does SimpleListConnection work for client types?

You can specify that you'd like to use that class instead of the generated ones & it will put it in there. As to whether or not it deserializes correctly depends on the json library you're using.

Also, couldn't DGS automatically detect pagination structures and auto-generate the correct paginated API for the model types?

There is no specified "correct" paginated type as GQL doesn't specify that sort of thing. There are the relay types you found, which can certainly be used, but they have some faults of their own. Namely, they won't differentiate between explicit null fields and those that are simply not requested. Left alone, the code-gen will generate the appropriate pagination structures for a client without having to involve the relay classes.

mbossenbroek avatar Jan 09 '24 18:01 mbossenbroek