Reputation: 694
Let assume that I have 2 types that are managed via 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!
}
Now, I am wondering what arguments should accept updateChild
.
In the example above, the only childID
is required. Do you think it is acceptable to also require from users providing parentID
?
updateChild(childID:ID!, parentID: ID!, in: ChildInput)
Upvotes: 1
Views: 1102
Reputation: 90447
I am using the suggestion from here which each mutation only has one input object argument which consolidate all the necessary fields rather than flattening these fields into many different input arguments. Also I tend to have one input object for each mutation (Github mutation API also use such design style)
So for the updateChild
mutation , I would have the following input type :
mutations {
updateChild(input: UpdateChildInput): Child
}
input UpdateChildInput {
childId : ID!
parentId: ID
name:String
}
What fields to be included in the UpdateChildInput
depends on your API requirements. Include the fields that your API need to support. So if your API allows user to change the parent of a child , you have to include it in the input.
Please note that parentID and the name are optional , which means if user does not specify them , they will not be updated.
Upvotes: 2