Graphene-django: Allow fragments for models with inheritance

Created on 29 Dec 2016  路  8Comments  路  Source: graphql-python/graphene-django

The library appears to currently be lacking the ability to use fragments when using models that have subclasses.

__models.py__

class MyModel(models.Model):
    name = models.CharField(max_length=50)

class ModelA(MyModel):
    extra = models.IntegerField()

class ModelB(MyModel):
    text = models.TextField()

__fields.py__

class ModelType(DjangoObjectType):
    class Meta:
        model = MyModel

class ModelAType(DjangoObjectType):
    class Meta:
        model = ModelA

class ModelBType(DjangoObjectType):
    class Meta:
        model = ModelB

__query__

query {
    all_models {
        __typename,
        name,
        ... on ModelAType {
            extra
        },
        ... on ModelBType {
            text
        }
    }
}

Suggestion: Modify DjangoObjectType to also act like a Union, or create a DjangoUnionType, with the ability to resolve type based on subclass and using the registered type for the subclass if applicable.

help wanted wontfix

Most helpful comment

Any new progress on this? I鈥檓 also getting this error when using Django polymorphic.

All 8 comments

Create a class that inherits from graphene.types.union.Union
than in the meta class of that class have all the types you want to union.

class MyUnion(graphene.types.union.Union):
    class Meta:
        types = (ModelType, ModelAType, ModelBType)

class ModelQuery(graphene.ObjectType):
    some = graphene.Field(graphene.List(MyUnion), variable=graphene.Argument(graphene.String, required=True))
    def resolve_some(self, args, context, info):
        # do whatever to retrieve your models, in a list
        model = [ ...]
        amodel = [...]
        bmodel = [...]
        return model + amodel + bmodel

@japrogramer When I do the above, the type checks for each of those unioned models resolves to the root type or abstract type, at least when using django-typed-models and then graphene complains that I have multiple models of the same type.

image

Any new progress on this? I鈥檓 also getting this error when using Django polymorphic.

No progress as of yet. PRs welcome

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

Im not using multiple inheritance
but i am inheriting from the abstract models.Model.
and have no errors.

it seems that where Union
is imported from changed.

class TimeLineDataType(graphene.Union):
    class Meta:
        types = (PostType, CommentType, ShareType, PinType)

class ActivityType(graphene.ObjectType):
    name = 'ActivityType'
    subject = graphene.Field(TimeLineDataType)

    def resolve_subject(self, info):
        return self

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

eyalch picture eyalch  路  3Comments

Eraldo picture Eraldo  路  3Comments

licx picture licx  路  3Comments

timothyjlaurent picture timothyjlaurent  路  3Comments

Northshoot picture Northshoot  路  4Comments