Currently when you do a mutation, you just have createPost(field1, field2, ... , fieldN)
, which means it's quite difficult to use variables on complex mutations.
A better route would be to allow a input type
, for instance: createPost(post: Post)
— in this case just using the Post
type.
A further extension on this would be to allow using "Input Types", which are effectively subsets of the entity's Type, for instance: createPost(post: CreatePostInput)
Where Post
is defined as:
type Post {
id: ID!
createdAt: DateTime
updatedAt: DateTime
title: String!
body: String!
poster: User!
}
And CreatePostInput
is defined as:
type CreatePostInput {
title: String!
body: String!
}
By allowing these "Input Types", we can effectively achieve finer grain creation. It also prevents code like the following (taken from graphql-training/graphql-training.github.io)
// You can also use `graphql` for GraphQL mutations
export default graphql(gql`
mutation createTrainingRequest(
$name: String!
$email: String!
$companyName: String
$attendees: Int!
$modules: [TRAINING_REQUEST_MODULES!]!
$additionalDetails: String
$city: String!
$country: String!
$diversityRequested: Boolean!
$onsiteRequested: Boolean!
) {
createTrainingRequest(
name: $name
email: $email
companyName: $companyName
attendees: $attendees
modules: $modules
additionalDetails: $additionalDetails
city: $city
country: $country
diversityRequested: $diversityRequested
onsiteRequested: $onsiteRequested
) {
id
}
}
`)(RequestForm);
Instead, I could've just done:
// You can also use `graphql` for GraphQL mutations
export default graphql(gql`
mutation createTrainingRequest(
$trainingRequest: CreateTrainingRequestInput!
) {
createTrainingRequest(trainingRequest: $trainingRequest) {
id
}
}
`)(RequestForm);