在GraphQL中使用许多用例创建突变 [英] Create mutations in GraphQL with a lot of use cases
问题描述
我是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:
- 创建
任务
. - 创建
Task
并将现有的User
分配给此Task
. - 创建
Task
并将新创建的User
分配给此Task
.
- Create
Task
. - Create
Task
and assign existingUser
to thisTask
. - Create
Task
and assign newly createdUser
to thisTask
.
那么,将其作为单个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:
-
createUser
-
createTask
(使用从createUser
返回的用户
createUser
createTask
(using the user returned fromcreateUser
尽管如此,您将无法将这两个突变组合成一个HTTP请求.
You won't be able to combine these two mutations into a single HTTP request though.
这篇关于在GraphQL中使用许多用例创建突变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!