Create mutations in GraphQL with a lot of use case

2019-08-04 02:13发布

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. Create Task.
  2. Create Task and assign existing User to this Task.
  3. Create Task and assign newly created User to this Task.

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
        }
       }
      }

and respective mutation in 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) 

1条回答
2楼-- · 2019-08-04 02:42

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 (using the user returned from createUser

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

查看更多
登录 后发表回答