-
Notifications
You must be signed in to change notification settings - Fork 829
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
Union types documentation #1426
base: master
Are you sure you want to change the base?
Union types documentation #1426
Conversation
Also add an additional test case, update existing ones, to cover `resolve_type` and `Meta.possible_types` more
Thanks for the effort! Shouldn't defining the union work without having to specify |
Thank you for the question @erikwrede.
I don't think the current example in the documentation works as-is (at least for the Union, may be different for Interfaces), although I'd need to check with a fresh project to be sure. |
I just tried this out, I took one of the test methods from the code base and just defined the Unions, without any of the methods mentioned in my update, just like in the current example. Here's the adjusted test I just wrote:
Here's the specific error:
|
What version of Graphene are you on? This works for me (3.0): from fastapi import FastAPI
import graphene
from starlette_graphene3 import GraphQLApp, make_playground_handler
class A(graphene.ObjectType):
a = graphene.String()
class B(graphene.ObjectType):
b = graphene.String()
class BCD(graphene.Union):
class Meta:
types=[A,B]
name="ABC"
def create_app() -> FastAPI:
app = FastAPI()
# graphql endpoint
class Query(graphene.ObjectType):
abc = graphene.Field(BCD)
@classmethod
def resolve_abc(cls,a,b):
return A(a="Hi")
schema = graphene.Schema(query=Query)
app.mount("/graphql", GraphQLApp(schema, on_get=make_playground_handler()))
return app
app = create_app() |
And it should be working just like that due to this method: graphene/graphene/types/union.py Line 74 in 72c2fd5
EDIT: I see what you're doing, you're initializing one_object instead of the objecttype while I'm creating a new instance of the ObjectType. These are two separate cases, but both are valid so both should be documented. Because doing it like this is also viable. What do you think? |
@erikwrede Agreed, and yes, there are different use cases. I have run into problems with this using the Django integration with the example as provided in this repository, after I've started creating some custom types for use in an Union. I myself would have been glad to have a clearer distinction between using an Would you agree to the above, @erikwrede? Or did I make some false assumption somewhere? |
Well, docs need a general overhaul so issues like you described with the object types have clear solution. Best |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See comment
Adds some documentation and examples to help those struggling with the type resolution of Unions (like I myself was just recently).
As this is my first real contribution to this project I am glad for any pointers or improvements to get my PR accepted without an unnecessary delay. Thanks!