Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Scala 3] Field derivation from case class methods #2041

Merged
merged 7 commits into from
Dec 17, 2023

Conversation

kyri-petrou
Copy link
Collaborator

@kyri-petrou kyri-petrou commented Dec 16, 2023

Currently, derivation of graphql fields works only with case class arguments. With this PR, we add a new annotation (available for Scala 3 only) which allows us to annotate a method to include in field derivation. This is useful in cases that a field can be resolved from the values of other fields, e.g.,

case class Stock(apples: Int, oranges: Int) {
  @GQLField def total: Int = apples + oranges
}

The methods can also be annotated with any other annotation that is normally used to annotate case class fields:

case class Stock(apples: Int, oranges: Int) {
  @GQLField
  @GQLDescription("The total number of fruits in stock")
  def total: Int = apples + oranges
}

Derivation via this case class methods relies partially on Java runtime reflection. This is mostly because the macros required for compiler-based derivation are extremely complex and very hard to maintain. This means that this annotation should NOT be used when Ahead-of-time compilation is required (e.g., GraalVM native-image).

Regarding Scala 2: We could potentially add this method to Scala 2 as well, but we'd have to port the Scala 3 macro to Scala 2 to make this work. Also I'm not too sure whether it's even possible given that we use magnolia for the derivation

PS: I'm not sure regarding the naming of the annotation. Another option is to use @GQLInclude (as opposite of GQLExclude) - @ghostdogpr any thoughts on the naming?)

@kyri-petrou kyri-petrou merged commit 6c1dac4 into series/2.x Dec 17, 2023
10 checks passed
@kyri-petrou kyri-petrou deleted the scala3/fields-from-methods branch December 17, 2023 01:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants