'Handle many models using graphene in django

In my django project i have many different models which i want to query using GraphQL. For instance:

class Country():
    name = CharField(max_length=255)

class Competition():
    id = IntegerField()

class Season():
    start = DateTimeField(null=False, blank=False)
    end = DateTimeField(null=False, blank=False)

class Team():
    name = CharField(max_length)

... etc

In my schema.py I need to create the GraphQL Types:

class CountryType():
    class Meta:
        model = models.Country
        fields = '__all__'

class CompetitionType():
    class Meta:
        model = models.Competition
        fields = '__all__'

class SeasonType():
    class Meta:
        model = models.Season
        fields = '__all__'

class TeamType():
    class Meta:
        model = models.Team
        fields = '__all__')

... etc

And last but not least I have to create the Query class in the schema.py:

class Query(graphene.ObjectType):
    country = graphene.relay.Node.Field(CountryType)
    competition = graphene.relay.Node.Field(CompetitionType)
    season = graphene.relay.Node.Field(SeasonType)
    team = graphene.relay.Node.Field(TeamType)

    all_country = graphene.List(CountryType)
    all_competition = graphene.List(CompetitionType)
    all_season = graphene.List(SeasonType)
    all_team = graphene.List(TeamType)

    def resolve_all_countries(root, info):
       return models.Country.objects.all()

    def resolve_all_competitions(root, info):
       return models.Competition.objects.all()

    def resolve_all_seasons(root, info):
       return models.Season.objects.all()

    def resolve_all_teams(root, info):
       return models.Team.objects.all()

    def resolve_country_by_name(root, info, name):
        try:
            return Country.objects.get(name = name)
        except Country.DoesNotExist:
            return None
    ... etc

This all seems like much boilerplate code which adds unnecessary complexity and overhead. Of course for sophisticated queries you would need to write your own functions, but is this really "the way to go" for those general queries and *Type Classes?



Solution 1:[1]

Using DjangoObjectType from graphene-django you do not need to define all the basic queries like these parts:

def resolve_all_countries(root, info):
   return models.Country.objects.all()

def resolve_all_competitions(root, info):
   return models.Competition.objects.all()

def resolve_all_seasons(root, info):
   return models.Season.objects.all()

def resolve_all_teams(root, info):
   return models.Team.objects.all()

everything else will still be needed though.

Your specific example using DjangoObjectType

import graphene
from graphene_django import DjangoObjectType
from graphene_django.filter import DjangoFilterConnectionField


class CountryType(DjangoObjectType):
    class Meta:
        model = models.Country
        interfaces = (relay.Node, )
        filter_fields = ["name"]

class CompetitionType(DjangoObjectType):
    class Meta:
        model = models.Competition
        interfaces = (relay.Node, )

class SeasonType(DjangoObjectType):
    class Meta:
        model = models.Season
        interfaces = (relay.Node, )

class TeamType(DjangoObjectType):
    class Meta:
        model = models.Team
        interfaces = (relay.Node, )


class Query(graphene.ObjectType):
    country = graphene.relay.Node.Field(CountryType)
    competition = graphene.relay.Node.Field(CompetitionType)
    season = graphene.relay.Node.Field(SeasonType)
    team = graphene.relay.Node.Field(TeamType)

    all_country = DjangoFilterConnectionField(CountryType)
    all_competition = DjangoFilterConnectionField(CompetitionType)
    all_season = DjangoFilterConnectionField(SeasonType)
    all_team = DjangoFilterConnectionField(TeamType)

You may find this link useful for your specific use case:

https://docs.graphene-python.org/projects/django/en/latest/tutorial-relay/

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 marc_s