GraphQL
Beginner
1 min read
Writing Mutations with Input Types
Example
# Schema
input CreatePostInput {
title: String!
body: String!
authorId: ID!
tags: [String!]
publishNow: Boolean = false
}
input UpdatePostInput {
title: String
body: String
tags: [String!]
}
type MutationResult {
success: Boolean!
message: String
}
type Mutation {
createPost(input: CreatePostInput!): Post!
updatePost(id: ID!, input: UpdatePostInput!): Post!
deletePost(id: ID!): MutationResult!
publishPost(id: ID!): Post!
}
# Client mutation with variables
mutation CreatePost($input: CreatePostInput!) {
createPost(input: $input) {
id
title
author { name }
createdAt
}
}
# Variables:
# {
# "input": {
# "title": "GraphQL Mutations",
# "body": "Mutations write data...",
# "authorId": "3",
# "tags": ["graphql", "api"],
# "publishNow": true
# }
# }
Related Resources
GraphQL Reference
Complete tag & property list
GraphQL How-To Guides
Step-by-step practical guides
GraphQL Exercises
Practice what you've learned
More in GraphQL