Skip to content

ManyToMany relation with wrong type #253

Closed
@amille44420

Description

@amille44420

I got into an issue with DjangoObjectType to convert a Django model into a GraphQL type ; On many to many relationships I get a list of the model owning the relation instead of the related model.

Here are the models to reproduce the issue

from django.db import models

class Child(models.Model):
    pass

class Parent(models.Model):
    children = models.ManyToManyField(Child)

and the schema definition

import graphene
from graphene_django import DjangoObjectType
from . import models

class Child(DjangoObjectType):
    class Meta:
        model = models.Child

class Parent(DjangoObjectType):
    class Meta:
        model = models.Parent

class Query(graphene.ObjectType):
    parents = graphene.List(Parent)

    def resolve_parents(self, args, context, info):
        return models.Parent.objects.all()

schema = graphene.Schema(query=Query)

And the test file

import graphene
from django.test import TestCase
from graphene_django.fields import DjangoListField
from . import schema

class M2MTestCase(TestCase):
    def setUp(self):
        schema.schema.build_typemap()
        # get the graphql type for the field related to the children
        self.field_type = schema.Parent._meta.fields['children'].get_type()

    def test_graphql_types(self):
        # we must get a django list field
        self.assertEquals(isinstance(self.field_type, DjangoListField), True)
        # behind it we've a graphql list
        self.assertEquals(isinstance(self.field_type.type, graphene.List), True)
        # it must be a list of Child
        self.assertEquals(self.field_type.type.of_type, schema.Child)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions