Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/.htaccess/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
GraphQLAPI-嵌套对象的突变_Graphql_Api Design - Fatal编程技术网

GraphQLAPI-嵌套对象的突变

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:

假设我有两种通过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: 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和名称是可选的,这意味着如果用户未指定它们,它们将不会被更新