Is it possible to map Apollo GraphQL responses into a single type?

Viewed 210

I have a few different mutations and queries that return a Profile object. I'd like to make a singular Profile object that is fulfilled by all of these different calls, rather than the default Apollo behavior of nesting types beneath the namespace of the call.

Pseudo

Right now:

SignInWithApple() -> SignInWithApple.Profile
SignInWithGoogle() -> SignInWithGoogle.Profile
SignInWithEmail() -> SignInWithEmail.Profile
GetProfile() -> GetProfile.Profile

Would like:

SignInWithApple() -> Profile
SignInWithGoogle() -> Profile
SignInWithEmail() -> Profile
GetProfile() -> Profile

Edit* I've been able to accomplish this so far by skipping the generated response from Apollo and decoding the raw data into a Profile object I made, but I still have to make special DeCodable containers so that the namespace of each query/mutation can be unwrapped/removed. Seems like there must be a better way...

1 Answers

You can use Union Interfaces to create a Profile type that can be any of those.

Like this

union Profile = SignInWithApple.Profile | 
  SignInWithGoogle.Profile |
  SignInWithEmail.Profile |
  GetProfile.Profile
Related