如何在Django+GraphQL中正确处理多对多关系?

huangapple go评论85阅读模式
英文:

How can I properly interact with many-to-many relationship in Django+GraphQL?

问题

我如何正确地在Django + GraphQL中处理多对多关系?

我有一个Game模型,它与User模型有多对多的关系。

  1. class Game(models.Model):
  2. name = models.CharField(max_length=256, blank=True)
  3. users = models.ManyToManyField(to=User, blank=True, related_name='games')
  4. class User(AbstractUser):
  5. email = models.EmailField(max_length=128, verbose_name='email address', blank=False)

我需要将它连接到GraphQL,以便通过mutations读取和修改这些字段。

我已经有一个可以处理Game的模式,但现在我也需要添加users

  1. {
  2. games {
  3. edges {
  4. node {
  5. id
  6. name
  7. }
  8. }
  9. }
  10. }

但是当我添加users时,它不起作用,因为数据库中的Game表没有users字段。它有一个描述它们关系的games_game_users表。

我应该如何修改我的代码以便能够使用game.users字段?

英文:

How do I properly interact with many-to-many relationship in Django+GraphQL?

I have a Game model which has a many to many relationship to User model.

  1. class Game(models.Model):
  2. name = models.CharField(max_length=256, blank=True)
  3. users = models.ManyToManyField(to=User, blank=True, related_name='games')
  4. class User(AbstractUser):
  5. email = models.EmailField(max_length=128, verbose_name='email address', blank=False)

I need to connect it to GraphQL so I can read and modify that fields via mutations.

I already have a schema to work with Game without users but now I need to add users as well

  1. {
  2. games {
  3. edges {
  4. node {
  5. id
  6. name
  7. }
  8. }
  9. }
  10. }

But it doesn't work when I add users because the Game table in db doesn't have a users field. It has the games_game_users table that describes their relationship.

How can I modify my code to be able to work with game.users field?

答案1

得分: 0

如果您想在Django+GraphQL中与多对多关系进行交互,您应该正确定义您的节点。

这是我的情况下应该如何定义的方式:

  1. class UserNode_(DjangoObjectType):
  2. class Meta:
  3. model = User
  4. interfaces = (graphene.relay.Node,)
  5. class GameNode(DjangoObjectType):
  6. class Meta:
  7. model = Game
  8. interfaces = (graphene.relay.Node,)
  9. users = graphene.List(UserNode_)
  10. def resolve_users(value_obj, info):
  11. return value_obj.users.all()

因此,这个GraphQL请求将显示您的游戏和连接的用户:

  1. {
  2. games {
  3. edges {
  4. node {
  5. id
  6. title
  7. users {
  8. id
  9. username
  10. email
  11. }
  12. }
  13. }
  14. }
  15. }

要向游戏中添加或删除用户,请使用add()remove()方法:

  1. game.users.add(new_user)
  2. game.users.remove(old_user)
英文:

If you want to interact with many-to-many relationship in Django+GraphQL you should define your Nodes correctly.

That's how it should be defined for my case:

  1. class UserNode_(DjangoObjectType):
  2. class Meta:
  3. model = User
  4. interfaces = (graphene.relay.Node,)
  5. class GameNode(DjangoObjectType):
  6. class Meta:
  7. model = Game
  8. interfaces = (graphene.relay.Node,)
  9. users = graphene.List(UserNode_)
  10. def resolve_users(value_obj, info):
  11. return value_obj.users.all()

So this GraphQL request will display your games and connected users

  1. {
  2. games {
  3. edges {
  4. node {
  5. id
  6. title
  7. users {
  8. id
  9. username
  10. email
  11. }
  12. }
  13. }
  14. }
  15. }

To add or to remove users from the game use add() and remove() methods:

  1. game.users.add(new_user)
  2. game.users.remove(old_user)

huangapple
  • 本文由 发表于 2023年5月30日 00:43:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/76359014.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定