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

How to use union types with graphql-yoga? #121

Closed
tibotiber opened this issue Jan 16, 2018 · 13 comments
Closed

How to use union types with graphql-yoga? #121

tibotiber opened this issue Jan 16, 2018 · 13 comments

Comments

@tibotiber
Copy link
Contributor

Hi, is there any support for union types yet in graphql-yoga? It seems to need a resolveType function to determine the actual type of an object at runtime. I couldn't find any place refering to supporting this so far.

Taking this opportunity to express my deep admiration for the work you guys at graphcool have been doing. Amazing developer experience across the board since the release of the graphcool framework, and really excited at what the stack is becoming with native gql db + yoga as api gateway 👍.

@idibidiart
Copy link

idibidiart commented Jan 21, 2018

I just ran into this too...

EDIT:

never mind... doh.

you need to provide a _resolveType function for union types

less magical than I thought but at least now it's not a mystery how it determines the type lol

@tibotiber
Copy link
Contributor Author

@idibidiart do you mind expending on your solution?

@kbrandwijk
Copy link
Contributor

kbrandwijk commented Jan 31, 2018

When you implement your resolvers, all 'standard' keys for it are available, so fragment, resolve, __resolveType and __isTypeOf:

From https://github.com/apollographql/graphql-tools/blob/608414b0dfd2ff7ba8a27389e0bb8c400fb631c6/src/Interfaces.ts#L23-L28:

export interface IResolverOptions {
  resolve?: IFieldResolver<any, any>;
  subscribe?: IFieldResolver<any, any>;
  __resolveType?: GraphQLTypeResolver<any, any>;
  __isTypeOf?: GraphQLIsTypeOfFn<any, any>;
}

@idibidiart
Copy link

Sorry didn’t see this before. I used __resolveType for efficient resolution. I assume isTypeOf is a hint field that has to be in the entity or API being queried but I’m not sure. I think you have to specify how to differentiate types. I’m not sure if GraphQL actually has pattern matching to resolve the different types in a Union all on its own. I don’t think that scales for long lists.

@schickling schickling changed the title support for union type? How to use union types with graphql-yoga? Feb 1, 2018
@maticzav
Copy link
Contributor

maticzav commented Feb 2, 2018

Is there any example tackling basic union types?

@maticzav
Copy link
Contributor

@eliperelman this is super useful! Thanks!

@schickling
Copy link
Contributor

@eliperelman would you be open to adding an example via PR demonstrating how to use interfaces + unions?

@tibotiber
Copy link
Contributor Author

tibotiber commented Apr 2, 2018

@schickling I've written a medium post covering Interfaces / Unions with Prisma & Yoga. Let me know which is the best way, if any, for me to help take parts of that and add to the docs. Also if there are any additions, clarifications, errors in my post, happy to correct things.

https://medium.com/@tibotiber/graphql-interfaces-and-union-types-with-prisma-and-yoga-7224f9e1d9ad

@schickling
Copy link
Contributor

Thanks a lot for this great blog post @tibotiber! Would be great if you could create a PR adding an example demonstrating how to use interfaces + unions. 🙏

@simonjoom
Copy link

hello
Who want to learn Prisma or contribute in our Project?:
We have got a ready react-native project who use prisma already setup with all techno apollo2 webpack4 react-scripts inside:
It's here
https://github.com/simonjoom/react-native-project
Read README, We are ready to hire competent and efficient developper who use prisma API CRUD in efficient way for our project

@stale
Copy link

stale bot commented Nov 23, 2018

Due to inactivity of this issue we have marked it stale. It will be closed if no further activity occurs.

@stale stale bot added the stale label Nov 23, 2018
@tibotiber
Copy link
Contributor Author

Hey stale bot, you happen to be right. I think we're done here as docs has been added to cover the question. Since I opened, I'll close :). 👋

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

No branches or pull requests

7 participants