graphql-python / graphene-django

Build powerful, efficient, and flexible GraphQL APIs with seamless Django integration.
http://docs.graphene-python.org/projects/django/en/latest/
MIT License
4.31k stars 770 forks source link

Proxied models dont have associated many to many relationship access #1379

Open TomsOverBaghdad opened 1 year ago

TomsOverBaghdad commented 1 year ago

This issue is loosely related to https://github.com/graphql-python/graphene-django/issues/319 to provide further support for proxy models

Currently, django models that are a proxy of another model don't include their many to many relationships that you get by default without proxy

So if my models.py looks like this

# models.py

from django.db import models

class Publication(models.Model):
    ...

class SciencePublication(Publication):
    class Meta:
        proxy = True

class Article(models.Model):
    publications = models.ManyToManyField(
        Publication, 
        related_name="articles"
    )

and my graphene types in types.py

# types.py

import graphene_django
from .models import (
    Publication as PublicationModel, 
    SciencePublication as SciencePublicationModel
)

class Publication(graphene_django.DjangoObjectType):
    class Meta:
        model = PublicationModel

class SciencePublication(graphene_django.DjangoObjectType):
    class Meta:
        model = SciencePublicationModel

When attempting to access the articles relationship through the publication type in the graphql query I can only access it via the Publication graphql type but not through the SciencePublication type

this is fine...

query {
  getPublications() {
    articles {
      id
    }
  }
}

this is errors

query {
  getSciencePublications() {
    articles { <<<< cannot query field "articles" on type "SciencePublication"
      id
    }
  }
}

many to many relationships persist with proxy models so that this query doesn't error

query {
  getSciencePublications() {
    articles { # no errors, just smiles :)
      id
    }
  }
}