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)