Skip to content

Help with constructing an arbitrary ObjectType #699

@jchang10

Description

@jchang10

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.

class NotesType(ObjectType):
    user = graphene.Field(UserObjectType)

    def resolve_user(self, args, context, info):
        return some_init_dictionary

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.

class NotesType(ObjectType):
    user = graphene.Field(UserInterface)

    def resolve_user(self, args, context, info):
        return UserObjectType(**some_init_dictionary)

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:

return UserObjectType(arg1='arg1', arg2='arg2', ... etc.)

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions