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

#400: Add support for GraphQL union type #1469

Merged
merged 2 commits into from
Jul 20, 2022

Conversation

craig-day
Copy link
Contributor

@craig-day craig-day commented Jul 18, 2022

I eagerly started with defining the spec, but also wanted to get an implementation done.

This adds support for creating GraphQL union types using an annotation @Union on a Java interface. I chose an empty Java interface so there would be an explicit layer of abstraction to represent the union itself.

Example:

@Union 
interface MyUnion {
}

class Foo implements MyUnion {
  String getName();
}

class Bar implements MyUnion {
  int getCount();
}

would create

type Foo {
  name: String
}

type Bar {
  count: Int
}

union MyUnion = Foo | Bar

An alternative could have been to directly annotate a type with the name of the union(s) it should be a member of, then generate the union from there.

Expand for alternative Java example
  @Union("MyUnion")
  class Foo {
    String getName();
  }
  
  @Union("MyUnion")
  class Bar {
    int getCount();
  }

Event thought this is a little less code, I didn't like it because the @Union annotation no longer describes an object directly, rather a relationship between objects. This relationship now only exists in the world of strings, and has no concrete representation in code.

For lots more thoughts, see the PR description here: eclipse/microprofile-graphql#459

Resolves #400

@phillip-kruger
Copy link
Member

@t1 @andymc12 @jmartisk @WhiteCat22 @kaqqao any thoughts on this ?

@phillip-kruger
Copy link
Member

@craig-day the current main in SmallRye is using jakarta and are not yet release. Maybe you can also open a PR for the 1.6.x (javax) branch ?

@craig-day craig-day changed the title add support for GraphQL union type #400: Add support for GraphQL union type Jul 19, 2022
@craig-day
Copy link
Contributor Author

@phillip-kruger done! #1470

@t1
Copy link
Collaborator

t1 commented Jul 19, 2022

I like the idea of using an interface. My first thought had been to use a wrapper type where exactly one field can be set, but the interface is better. Thanks @craig-day !

@phillip-kruger phillip-kruger added this to the 2.0.0 milestone Jul 20, 2022
@phillip-kruger
Copy link
Member

The failure of Quarkus build is expected with the Jakarta branch

Copy link
Member

@phillip-kruger phillip-kruger left a comment

Choose a reason for hiding this comment

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

Thanks !

@phillip-kruger phillip-kruger merged commit a7216b5 into smallrye:main Jul 20, 2022
@craig-day craig-day deleted the unions branch July 20, 2022 17:05
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.

Union Types
3 participants