GraphQL 實作教學:從概念到程式碼以 Express.js、Apollo Server 為例
目錄
1. GraphQL 基本觀念介紹
GraphQL 是由 Facebook 提出的 API 查詢語言,用於前後端資料交換。不同於 RESTful API,GraphQL 提供更精確且彈性的資料請求方式,可避免資源過度或不足的問題。
2. 使用套件說明
本次使用的套件(package.json):
- Express:架設伺服器。
- Apollo Server:實作 GraphQL Server。
- Express Integration:整合 Apollo 與 Express。
- cors:處理跨來源請求。
- graphql:提供 GraphQL 核心功能。
- nodemon(開發階段):即時重啟伺服器。
3. GraphQL Server 基礎實作步驟
建立 Schema(型別定義)
定義資料的形狀與可操作的 Query 和 Mutation。
type Book {
id: ID!
title: String!
author: String!
publishedYear: Int
}
type Query {
books: [Book!]!
book(id: ID!): Book
hello: String
}
type Mutation {
addBook(title: String!, author: String!, publishedYear: Int): Book!
}
建立 Resolvers(解析器)
Resolvers 負責處理資料請求並返回相應結果。
const resolvers = {
Query: {
books: () => books,
book: (_, { id }) => books.find(book => book.id === id),
hello: () => "你好!歡迎使用 GraphQL API!",
},
Mutation: {
addBook: (_, { title, author, publishedYear }) => {
const newBook = {
id: String(books.length + 1),
title,
author,
publishedYear,
};
books.push(newBook);
return newBook;
},
},
};
建立伺服器(Server)
使用 Express 和 Apollo Server 架設 GraphQL 伺服器。
import express from "express";
import { ApolloServer } from "@apollo/server";
import { expressMiddleware } from "@as-integrations/express5";
import cors from "cors";
async function startServer() {
const server = new ApolloServer({ typeDefs, resolvers });
const app = express();
await server.start();
app.use("/graphql", cors(), express.json(), expressMiddleware(server));
app.get("/", (req, res) => {
res.json({
message: "🚀 GraphQL API 伺服器運行中!",
graphql: "http://localhost:4000/graphql",
note: "請訪問 /graphql 來使用 GraphQL Playground",
});
});
app.listen(4000, () => {
console.log("🚀 伺服器運行在 http://localhost:4000");
console.log("📊 GraphQL 端點:http://localhost:4000/graphql");
});
}
startServer().catch(console.error);
4. 範例解說與操作
啟動伺服器後,可透過 /graphql 端點使用 GraphQL Playground 進行查詢與修改資料。
範例 Query:
query {
books {
id
title
author
publishedYear
}
}
範例 Mutation:
mutation {
addBook(title: "新的書籍", author: "作者", publishedYear: 2024) {
id
title
author
}
}