2

This is my first experience building a GraphQL API. My team is using Netflix DGS with Spring Boot on the backend side to implement this API. I found a situation that I am not sure what is the proper way to handle. Please see the code snippets below:

GraphQL schema

type LegalEntity {
  id: UUID!
  name: String
  # other properties (omitted for brevity)
}

type Contract {
  id: UUID!
  startDate: Date!
  endDate: Date
  licensor: LegalEntity!
  # other properties (omitted for brevity)
}

Generated Kotlin code

public data class LegalEntity(
  @JsonProperty("id")
  public val id: String,
  @JsonProperty("name")
  public val name: String? = null
) {
  public companion object
}

public data class Contract(
  @JsonProperty("id")
  public val id: String,
  @JsonProperty("startDate")
  public val startDate: LocalDate,
  @JsonProperty("endDate")
  public val endDate: LocalDate? = null,
  @JsonProperty("licensor")
  public val licensor: LegalEntity
) {
  public companion object
}

My doubt is about how to handle the non-nullable Contract.licensor property when the backend receives a call that doesn't request the licensor, e.g.:

query contract($id: UUID!) {
  contract(id: $id) {
    id
    startDate
    endDate
  }
}

Ideally I don't want to load the licensor only because the property on the generated class is non-nullable (i.e. to make the compiler happy), that would be wasteful since the frontend would discard the licensor anyway.

What am I supposed to do in this situation? Retrieve the licensor anyway and return it, set a fake value and return it, or do something else?

Thanks!

Matheus Moreira
  • 2,338
  • 4
  • 24
  • 31

0 Answers0