-
Notifications
You must be signed in to change notification settings - Fork 822
Help with constructing an arbitrary ObjectType
#699
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
Comments
To answer my own question, I had to make sure to define a classmethod on UserObjectType named is_type_of(cls, value,context,info) to return a bool value on whether to accept the "returned data type" that your custom code returns from the resolver for your ObjectType. See graphene/graphene/types/objecttype.py Lines 61 to 65 in d46d8e8
It's too bad there isn't better documentation. We have to rely on the code. However, the code isnt even really commented much, like the line above. |
@jchang10 another approach to resolving interface types is to define a (undocumented) classmethod class UserInterface(graphene.Interface):
@classmethod
def resolve_type(cls, data, info):
if data['type'] == 'human':
return Human
return Droid Then your class NotesType(ObjectType):
user = graphene.Field(UserInterface)
def resolve_user(self, args, context, info):
return some_init_dictionary I've found this very useful when implementing interfaces. |
Cool. Just saw this. Thanks for the update! |
Uh oh!
There was an error while loading. Please reload this page.
Hi, I am still just learning. Looking for advice on an issue.
The simple question is, what is the best way to instantiate an ObjectType?
Here is a longer drawn out explanation for a simple question. I currently have something like below that returns a UserObjectType. Everything is great and user field resolves correctly. Returns the UserObjectType that is initialized by 'some_init_dictionary' which graphql takes care of on backend somewhere.
Looking at examples, I decided I want to get fancy with Interfaces. Basically, I am trying to copy the example schema for StarWars and define the user field as a UserInterface. Then return an instance of UserObjectType.
I simply replaced UserObjectType with UserInterface. That all works fine, too.
However, if some_init_dictionary has an extra field, then I get the "is an invalid keyword argument" error. In the original with UserObjectType, the extra field is ignored and everything merrily works fine.
Thus, am I missing something here? I would rather not have to do something like:
and explicity call the constructor with each argument and field. I just want to duplicate how graphql handles the instantiation of UserObjectType as in the original version.
I hope that made sense. This should be a fairly simple answer/question. Thank you!
Jae
The text was updated successfully, but these errors were encountered: