I have two models:
class Type(models.Model):
name = models.CharField(max_length=255)
def __str__(self):
return f'{self.name}'
class Pet(models.Model):
name = models.CharField(max_length=255)
color = models.CharField(max_length=255)
type = models.ForeignKey(Type, related_name="pets", on_delete=models.CASCADE,
null=True, blank=True)
def __str__(self):
return f'{self.type.name} {self.name}'
The schema:
class Type(DjangoObjectType):
class Meta:
model = TypeModel
@classmethod
def get_node(cls, info, id):
return TypeModel.objects.get(id=id)
class TypeConnector(graphene.Connection):
count = graphene.Int()
class Meta:
node = Type
def resolve_count(root, info):
return len(root.edges)
class Pet(DjangoObjectType):
class Meta:
model = PetModel
interfaces = (relay.Node,)
@classmethod
def get_node(cls, info, id):
return PetModel.objects.get(id=id)
class PetConnector(graphene.Connection):
count = graphene.Int()
class Meta:
node = Pet
def resolve_count(root, info):
return len(root.edges)
class Schema(ObjectType):
pets = graphene.ConnectionField(PetConnector)
types = graphene.ConnectionField(TypeConnector)
def resolve_pets(self, info, **kwargs):
# TODO: Query for requested fields only
return PetModel.objects.all()
def resolve_types(self, info, **kwargs):
# TODO: Query for requested fields only
return TypeModel.objects.all()
One of the goals of GraphQL is the performance. To make it the GraphQL must request to the database only the requested fields via GraphQL request (e.g: GraphiQL)
If I request the following query:
{
pets {
edges {
node {
color
type {
name
}
}
}
}
}
graphene-django library generate the follow SQL:
2020-01-03 03:16:18.184 UTC [136] LOG: statement: SELECT "pets_pet"."id", "pets_pet"."name", "pets_pet"."color", "pets_pet"."type_id" FROM "pets_pet"
2020-01-03 03:16:18.189 UTC [136] LOG: statement: SELECT "pets_type"."id", "pets_type"."name" FROM "pets_type" WHERE "pets_type"."id" = 1 LIMIT 21
It gets all the fields of the models! Is the same of the API Rest problems and does not conest with the GraphQL guidelines.
If I request the field color
from the pets
model I hope that the query will be:
SELECT "pets_pet"."color" FROM "pets_pet"
And... how does foreign key resolve with this approach?