GraphQLAPI-嵌套对象的突变
假设我有两种通过GraphQL API管理的类型:GraphQLAPI-嵌套对象的突变,graphql,api-design,Graphql,Api Design,假设我有两种通过GraphQL API管理的类型: type Parent { id: ID name: String children: [Child!] } type Child { id: ID name: String } query { parents(): Parent } mutations { addParent(in: ParentInput!): Parent! addChild(parentID:
type Parent {
id: ID
name: String
children: [Child!]
}
type Child {
id: ID
name: String
}
query {
parents(): Parent
}
mutations {
addParent(in: ParentInput!): Parent!
addChild(parentID: ID!, in: ChildInput!): Child!
updateChild(childID:ID!, in: ChildInput!): Child!
}
现在,我想知道哪些参数应该接受updateChild
。
在上面的示例中,只需要childID
。您认为还可以要求提供parentID
的用户提供吗
updateChild(childID:ID!, parentID: ID!, in: ChildInput)
我使用的建议是,每个变异只有一个输入对象参数,它整合了所有必要的字段,而不是将这些字段平铺成许多不同的输入参数。此外,我倾向于为每个变异使用一个输入对象(Github变异API也使用这种设计风格) 因此,对于
updateChild
变异,我将使用以下输入类型:
mutations {
updateChild(input: UpdateChildInput): Child
}
input UpdateChildInput {
childId : ID!
parentId: ID
name:String
}
UpdateChildInput
中包含哪些字段取决于您的API要求。包括API需要支持的字段。因此,如果API允许用户更改子级的父级,则必须将其包含在输入中
请注意,parentID和名称是可选的,这意味着如果用户未指定它们,它们将不会被更新