[Scala 3] Field derivation from case class methods #2041
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.,
The methods can also be annotated with any other annotation that is normally used to annotate case class fields:
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 ofGQLExclude
) - @ghostdogpr any thoughts on the naming?)