问题
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:
- Create
Task
. - Create
Task
and assign existingUser
to thisTask
. - Create
Task
and assign newly createdUser
to thisTask
.
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:
createUser
createTask
(using the user returned fromcreateUser
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