在GraphQL中使用许多用例创建突变 [英] Create mutations in GraphQL with a lot of use cases

查看:58
本文介绍了在GraphQL中使用许多用例创建突变的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是GraphQL的新手,在GraphQL中创建突变(尤其是 graphene-python )时需要一些建议(最佳实践).假设我们有一些 Task 和一个 User .现在,我想创建 Task 变异,它涵盖三种情况:

I'm a newbie in GraphQL and need some advice (best practice) in creating mutations in GraphQL (particular with graphene-python). Let's suppose we have some Task and a User. Now I want to create Task mutation, that covers three cases:

  1. 创建任务.
  2. 创建 Task 并将现有的 User 分配给此 Task .
  3. 创建 Task 并将新创建的 User 分配给此 Task .
  1. Create Task.
  2. Create Task and assign existing User to this Task.
  3. Create Task and assign newly created User to this Task.

那么,将其作为单个QraphQL入口点"实现是一个好主意,还是为第三种情况创建另一个变异(也许)更好?

So, is this a good idea to implement this as a single QraphQL "entry point", or it's better to create another mutation for the third case (maybe)?

      mutation {
       createTask(taskTitle: "Do some stuff"){
        task {
         id
        }
       }
      }

      mutation {
       createTask(taskTitle: "Do some stuff",
                  user: {id: "ggdf00askladnl42"}){
        task {
         id
        }
       }
      }

      mutation {
       createTask(taskTitle: "Do some stuff",
                  user: {email: "j.doe@example.com", fullName: "John Doe"}){
        task {
         id
        }
       }
      }

graphene-python 中的相应突变:

class CreateTODO(graphene.Mutation):

    class Arguments:
        task_title = graphene.NonNull(graphene.String)
        user = UserInput()

    task = graphene.Field(lambda: Task)

    def mutate(self, info, task_title, user=None):
        #
        #  Do some stuff here
        #
        return CreateTODO(task=task) 

推荐答案

对于3.,如果事实证明您仍然需要 createUser 突变,那么它将是一个更简单的实现始于两个单独的突变:

For 3., if it turns out that you're going to need a createUser mutation anyway, then it's going to be a simpler implementation to start out with two separate mutations:

  1. createUser
  2. createTask (使用从 createUser
  3. 返回的用户
  1. createUser
  2. createTask (using the user returned from createUser

尽管如此,您将无法将这两个突变组合成一个HTTP请求.

You won't be able to combine these two mutations into a single HTTP request though.

这篇关于在GraphQL中使用许多用例创建突变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆