GraphQL 变更:修改数据
不要止步于读取数据:学习在 Spring Boot 中定义并实现 GraphQL 变更,以创建、更新和删除数据。
GraphQL 变更:修改数据 是 CoddyKit 上的免费 GraphQL APIs with Spring Boot 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 GraphQL APIs with Spring Boot 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 GraphQL APIs with Spring Boot 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
From Reading to Writing
You can now define types, resolvers, and run queries. But real APIs must also change data.
In GraphQL, every write goes through a mutation — the counterpart to a query.
Query vs Mutation
The shapes look similar, but intent differs:
- Query: read data, no side effects, may run in parallel.
- Mutation: change data, executed sequentially top to bottom.
Declaring a Mutation in the Schema
Mutations live under the special Mutation type and usually return the affected object.
type Mutation {
createBook(title: String!, author: String!): Book
}Input Types for Complex Arguments
For many fields, group arguments into an input type instead of a long argument list.
input BookInput {
title: String!
author: String!
}
type Mutation {
createBook(book: BookInput!): Book
}Implementing the Resolver
In Spring Boot, annotate the handler with @MutationMapping.
@Controller
class BookMutation {
@MutationMapping
Book createBook(@Argument String title, @Argument String author) {
return new Book(title, author);
}
}Returning the Result
Returning the created or updated object lets the client immediately read back any fields it needs.
mutation {
createBook(title: "Clean Code", author: "Martin") {
id
title
}
}Update and Delete Mutations
The same pattern covers all writes.
type Mutation {
updateBook(id: ID!, book: BookInput!): Book
deleteBook(id: ID!): Boolean
}A Self-Contained Logic Example
The resolver logic is plain Java; here is the core idea as a runnable snippet.
import java.util.*;
public class Main {
static Map<Integer,String> store = new HashMap<>();
static int seq = 0;
static int createBook(String title){ int id = ++seq; store.put(id, title); return id; }
public static void main(String[] a){
int id = createBook("Clean Code");
System.out.println("Created id=" + id + " title=" + store.get(id));
}
}Validating Input
Mutations are the right place to enforce rules: reject empty titles, check permissions, ensure referenced records exist.
Return clear errors so clients can react meaningfully.
Sequential Execution Matters
Unlike query fields, top-level mutation fields run one after another.
This guarantees that if a single request contains several mutations, earlier ones complete before later ones begin — important for consistency.
Best Practices
- Use input types for multi-field arguments.
- Return the affected object so clients can re-read fields.
- Validate and authorize inside resolvers.
- Name mutations as verbs: createBook, updateBook, deleteBook.
Quick Check
Test your understanding of GraphQL mutations.
Recap
You learned to change data with GraphQL mutations.
- Define them under the Mutation type, often with input types.
- Implement with @MutationMapping in Spring Boot.
- They run sequentially and should validate and return the affected object.
常见问题解答
「GraphQL 变更:修改数据」课时是免费的吗?
是的 — 「GraphQL 变更:修改数据」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 GraphQL APIs with Spring Boot 课程的其余内容,请升级到 CoddyKit PRO。 GraphQL APIs with Spring Boot 课程共包含 4 节课。
「GraphQL 变更:修改数据」这节课中我会学到什么?
不要止步于读取数据:学习在 Spring Boot 中定义并实现 GraphQL 变更,以创建、更新和删除数据。 你通过在浏览器中直接运行的动手代码来练习 GraphQL APIs with Spring Boot,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 GraphQL APIs with Spring Boot 需要有经验吗?
无需任何先前经验。CoddyKit 上的 GraphQL APIs with Spring Boot 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。
「GraphQL 变更:修改数据」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 GraphQL APIs with Spring Boot 课中编写并运行代码吗?
能。每节 GraphQL APIs with Spring Boot 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- 定义自定义数据类型
- 实现数据解析器
- 执行简单的 GraphQL 查询
- GraphQL 变更:修改数据