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

Add derivation of Schema for union types (towards #1926) #2215

Merged

Conversation

oyvindberg
Copy link
Contributor

@oyvindberg oyvindberg commented May 7, 2024

Finally found time to implement a first iteration of this this.
A patch for the codegen will probably be forthcoming, but this at least allows us to use union types with a bit more ease than before.

Usage looks like this:

final case class Foo(value: String) derives Schema.SemiAuto
final case class Bar(foo: Int) derives Schema.SemiAuto
type Payload = Foo | Bar
given Schema[Any, Payload] = Schema.unionType[Payload]

This will produce a schema like this:

schema {
  query: Query
}

union Payload = Foo | Bar

type Bar {
  foo: Int!
}

type Foo {
  value: String!
}

type Query {
  testQuery(isFoo: Boolean!): Payload!
}

Limitations:

  • in order to capture the name of the type alias, users will need to explicitly provide a type name to Schema.unionType. The compile will fail otherwise.
  • No annotations are picked up yet. It probably works the same as for the other cases, but I didn't have time to implement it now.

@oyvindberg oyvindberg force-pushed the schema-derivation-for-union-types branch from 0865937 to 110f244 Compare May 7, 2024 13:20
@oyvindberg oyvindberg force-pushed the schema-derivation-for-union-types branch 5 times, most recently from b9824cb to afc5f4e Compare May 14, 2024 12:28
@oyvindberg oyvindberg force-pushed the schema-derivation-for-union-types branch from afc5f4e to 1f52a28 Compare May 15, 2024 05:50
@kyri-petrou
Copy link
Collaborator

Awesome! Love it! Do you know whether it also works with opaque types?

Also, totally out of the scope of this PR, but would a similar approach also work for using intersection types for interfaces?

@oyvindberg oyvindberg force-pushed the schema-derivation-for-union-types branch from 1f52a28 to 29b37f1 Compare May 15, 2024 20:16
@oyvindberg
Copy link
Contributor Author

Awesome! Love it! Do you know whether it also works with opaque types?

My understanding is that opaque types really just need to fetch an instance of the underlying type and expose it in its companion object where it's known that the two types are the same. I haven't tried it yet, but there is a chance it could be easy.

Also, totally out of the scope of this PR, but would a similar approach also work for using intersection types for interfaces?

Interesting idea. This code here follows Or AST nodes, I suppose a variant of that code could follow And-nodes or similar instead and do something productive with it.

I rebased the branch again now, so hopefully it's good to go.

@ghostdogpr
Copy link
Owner

  • No annotations are picked up yet. It probably works the same as for the other cases, but I didn't have time to implement it now.

Is this still missing?

@ghostdogpr
Copy link
Owner

For annotations it would just be calls to getName, getDescription and getDirectives in DerivationUtils, similar to

Some(getName(annotations, info)),

Do you think you'll be able to add them? If not, we can probably merge it and someone else opens an other PR.

@ghostdogpr
Copy link
Owner

ghostdogpr commented May 21, 2024

Btw I checked that the magnolia macro we use to get annotations works on type aliases: https://scastie.scala-lang.org/rI5LUKi5TrKCCbZ3tkrZNQ

@oyvindberg
Copy link
Contributor Author

Thanks for the pointers. Added annotation handling now 👍

Copy link
Owner

@ghostdogpr ghostdogpr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@ghostdogpr ghostdogpr merged commit 32bb7ac into ghostdogpr:series/2.x May 22, 2024
11 checks passed
@oyvindberg oyvindberg deleted the schema-derivation-for-union-types branch May 23, 2024 07:38
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.

3 participants