Create mutations in GraphQL with a lot of use cases

笑着哭i 提交于 2020-01-24 23:07:06

问题


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:


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.



来源:https://stackoverflow.com/questions/49788006/create-mutations-in-graphql-with-a-lot-of-use-cases

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!