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

Kotlin2 codegen implementation is eager

Open zwolsman opened this issue 2 years ago • 3 comments

Hey all,

Thanks for this amazing library. In our current setup I ran into the following. When building the query with Kotlin projections (love the feature) it's eager. When you select a field or create a projection, the string is build eagerly.

Compared to the Java implementation the fields are stored to a map and serialised when you call the .serialize() function. The result is, when you are selecting the same field multiple times it's only included once.

We try to split our projection selection which might result in duplication of selecting a field (for example a name or id property) and this is now included in the query. GraphQL does accept it but it adds extra noise to the query which is not needed.

Proposed solution

Instead of moving to the DgsClient.buildQuery still support the split between Query and Projection which is done by using the GraphQLQueryRequest. This then can use the same underlying builder pattern that is now for the Projections but instead of calling functions it's done with the same solution that is being used for the Kotlin2 implementation.

Example

Current java result

  public XProjectionRoot y() {
    getFields().put("y", null);
    return this;
  }

Current kotlin2 implementation

  public val y: XProjection
    get() {
      field("y")
      return this
    }

Proposed kotlin2 implementation

  public val y: XProjection
    get() {
      getFields().put("y", null);
      return this
    }

And instead of using the DgsClient.buildQuery function we can provide the projection to the current GraphQLQueryRequest

zwolsman avatar Jul 16 '22 18:07 zwolsman

cc @mbossenbroek

srinivasankavitha avatar Sep 01 '22 20:09 srinivasankavitha

I made a change recently to use the GQL builders instead of constructing strings: https://github.com/Netflix/dgs-codegen/pull/423/commits/3778f55bc0cb147b66eec2ddc1bf07a7b8b18296

It was over a month ago, so that should have been released by now - can you confirm you're on that version?

I'm not sure I understand the issue you're having either - do you have a simple example with the behavior that's undesirable?

mbossenbroek avatar Sep 01 '22 22:09 mbossenbroek

Just noticed you posted this a few days before I made that commit - give that a try & let me know. Also feel free to tag me directly in the future for any kotlin2 questions

mbossenbroek avatar Sep 01 '22 23:09 mbossenbroek