GraphQL API 设计原则
学习 GraphQL 的基础知识,包括架构、查询、变更和订阅,以设计灵活的 API
GraphQL API 设计原则 是 CoddyKit 上的免费 Node.js Backend Development Bootcamp 课时。 这是第 2 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Node.js Backend Development Bootcamp 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Node.js Backend Development Bootcamp 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
What is GraphQL?
Welcome to GraphQL API Design! GraphQL is a powerful query language for your APIs and a runtime for fulfilling those queries with your existing data.
Think of it as a way for clients (like your mobile app) to ask for exactly the data they need, no more, no less. It's an alternative to traditional REST APIs.

GraphQL vs. REST APIs
While REST APIs typically have multiple endpoints, each returning a fixed data structure, GraphQL uses a single endpoint.
- REST: Often leads to over-fetching (getting more data than needed) or under-fetching (needing multiple requests for related data).
- GraphQL: Solves this by allowing clients to specify the data shape, reducing network requests and improving efficiency.
The Core: GraphQL Schema
At the heart of every GraphQL API is its schema. The schema defines the entire API's capabilities: what data can be queried, what data can be modified, and what types of data exist.
It acts as a contract between the client and the server, ensuring both sides understand the available operations and data structures.
Schema Definition Language (SDL)
GraphQL schemas are written using the Schema Definition Language (SDL). It's a simple, intuitive language for defining types and operations.
Let's look at a basic example of defining a User type with some fields:
type User {
id: ID!
name: String!
email: String
age: Int
}Understanding SDL Types
In the previous example:
type User: Defines a new object type namedUser.id: ID!:idis a field of typeID. The!means it's non-nullable (always present).String,Int,ID: These are scalar types, GraphQL's built-in basic data types.- You can also define custom object types like
PostorComment.
Root Type: Query
The Query root type is special. It defines all the entry points for reading data from your API. Think of these as the 'GET' operations in REST.
Here's how you might add operations to fetch users or a single user by ID:
type Query {
users: [User!]!
user(id: ID!): User
}
type User {
id: ID!
name: String!
email: String
age: Int
}Executing a GraphQL Query
Once the Query type is defined, clients can request data. They specify which fields they want from the available operations. This is how you avoid over-fetching!
To get all user names and IDs:
query GetUsers {
users {
id
name
}
}Root Type: Mutation
The Mutation root type defines all the entry points for writing or changing data in your API. These are like 'POST', 'PUT', 'PATCH', and 'DELETE' operations in REST.
Mutations often take input arguments and return the modified object.
type Mutation {
createUser(name: String!, email: String, age: Int): User!
updateUser(id: ID!, name: String, email: String, age: Int): User
deleteUser(id: ID!): Boolean!
}Executing a GraphQL Mutation
Similar to queries, clients send mutations to perform data modifications. They specify the mutation name, its arguments, and what fields of the result they want back.
Here's an example to create a new user:
mutation CreateNewUser {
createUser(name: "Alice", email: "alice@example.com", age: 30) {
id
name
email
}
}Schema Design Quick Check
Consider the following GraphQL schema snippet. Which statements about it are TRUE?
type Book {
id: ID!
title: String!
author: Author!
}
type Author {
id: ID!
name: String!
books: [Book!]
}
type Query {
books: [Book!]!
book(id: ID!): Book
authors: [Author!]!
}
type Mutation {
createBook(title: String!, authorId: ID!): Book!
}Recap & Beyond
Great job! You've learned the core principles of GraphQL API design:
- GraphQL allows clients to request specific data.
- The Schema Definition Language (SDL) defines the API's contract.
- Object types define data structures.
- The
Queryroot type handles data fetching. - The
Mutationroot type handles data modification.
Next, we'll dive into building a GraphQL server with Apollo to bring these designs to life!
常见问题解答
「GraphQL API 设计原则」课时是免费的吗?
是的 — 「GraphQL API 设计原则」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Node.js Backend Development Bootcamp 课程的其余内容,请升级到 CoddyKit PRO。 Node.js Backend Development Bootcamp 课程共包含 4 节课。
「GraphQL API 设计原则」这节课中我会学到什么?
学习 GraphQL 的基础知识,包括架构、查询、变更和订阅,以设计灵活的 API 你通过在浏览器中直接运行的动手代码来练习 Node.js Backend Development Bootcamp,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Node.js Backend Development Bootcamp 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Node.js Backend Development Bootcamp 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 2 节课,共 4 节。
「GraphQL API 设计原则」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Node.js Backend Development Bootcamp 课中编写并运行代码吗?
能。每节 Node.js Backend Development Bootcamp 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- Node.js 无服务器简介
- GraphQL API 设计原则
- 使用 Apollo 构建 GraphQL 服务器
- 用于实时数据的 GraphQL 订阅