My app has several many-to-many relationships with a through-model like so:
class Person(models.Model):
name = models.CharField()
class Group(models.Model):
name = models.CharField()
members = models.ManyToManyField(Person, through='Membership')
class Membership(models.Model):
person = models.ForeignKey(Person)
group = models.ForeignKey(Group)
date_joined = models.DateField() # Extra info on the relationship
It would seem intuitive to represent this data in graphql without an intermediate type for Membership (option A):
{
"data": {
"persons": [
{
"id": "1",
"name": "Jack",
"groups": [
{
"id": 3, # From Group-model
"name": "Students", # From Group-model
"date_joined": "2019-01-01" # From Membership-model
},
...
]
}
]
}
}
vs. option B:
{
"data": {
"persons": [
{
"id": "1",
"name": "Jack",
"memberships": [
{
"id": 9,
"date_joined": "2019-01-01"
"group": {
"id": 3,
"name": "Students"
}
},
...
]
}
]
}
}
I could not find any examples on how to implement option A with (django-)graphene. How could it be done and is this supported to work out of the box?
What are the pros and cons on both approaches? The data needs to be also mutated quite often, does it alter the verdict?