如何基于给定输出与类型定义为Apollo GraphQL Server创建Resolver
基于给定数据和GraphQL类型定义创建Apollo Server Resolver
以下是根据提供的示例输出数据和GraphQL类型定义,编写的Apollo GraphQL Server Resolver实现方案,包含完整代码和说明:
1. GraphQL类型定义(SDL)
直接复用给定的类型定义:
type Bookdepartment { id: ID booksubdepartment: [Booksubdepartment] } type Booksubdepartment { id: ID departmentID: String books: [Book] } type Book { id: ID subdepartmentId: String bookTitle: String } type Query { bookdepartments: [Bookdepartment] }
2. 模拟数据源
对应示例输出数据,定义用于测试的模拟数据源:
const mockData = { department: [ { "id": "123", "booksubdepartment": [ { "id": "111", "departmentID": "123", "books": [ { "subdepartmentId":"111", "bookTitle": "Book One on sub department 111" }, { "subdepartmentId":"111", "bookTitle": "Book Two on sub department 111" } ] }, { "id": "222", "books": [ { "subdepartmentId":"222", "bookTitle": "Book one on sub department 222" }, { "subdepartmentId":"222", "bookTitle": "Book two on sub department 222" } ] } ] }, { "id": "345", "booksubdepartment": [ { "id": "333", "departmentID": "345", "books": [ { "subdepartmentId":"333", "bookTitle": "Book one on sub department 333" }, { "subdepartmentId":"333", "bookTitle": "Book two on sub department 333" } ] }, { "id": "444", "books": [ { "subdepartmentId":"444", "bookTitle": "Book one on sub department 444" }, { "subdepartmentId":"444", "bookTitle": "Book two on sub department 444" } ] } ] } ] };
3. Resolver实现
Resolver需要处理三个核心点:提供查询入口、补全子部门缺失的departmentID字段、为Book生成符合类型定义的id字段,完整代码如下:
const resolvers = { Query: { // 查询入口:返回所有部门数据 bookdepartments: () => mockData.department, }, Bookdepartment: { // 解析子部门字段,补全缺失的departmentID booksubdepartment: (parent) => { return parent.booksubdepartment.map(subDept => ({ ...subDept, departmentID: subDept.departmentID || parent.id, })); }, }, Book: { // 为Book生成唯一ID(示例数据无该字段,类型定义要求返回) id: (parent) => `${parent.subdepartmentId}-${parent.bookTitle.toLowerCase().replace(/\s+/g, '-')}`, }, };
4. 完整Apollo Server启动代码
将类型定义、数据源和Resolver整合,实现完整的服务器启动逻辑:
const { ApolloServer } = require('@apollo/server'); const { startStandaloneServer } = require('@apollo/server/standalone'); // 类型定义 const typeDefs = ` type Bookdepartment { id: ID booksubdepartment: [Booksubdepartment] } type Booksubdepartment { id: ID departmentID: String books: [Book] } type Book { id: ID subdepartmentId: String bookTitle: String } type Query { bookdepartments: [Bookdepartment] } `; // 模拟数据源(同上) const mockData = { department: [ { "id": "123", "booksubdepartment": [ { "id": "111", "departmentID": "123", "books": [ { "subdepartmentId":"111", "bookTitle": "Book One on sub department 111" }, { "subdepartmentId":"111", "bookTitle": "Book Two on sub department 111" } ] }, { "id": "222", "books": [ { "subdepartmentId":"222", "bookTitle": "Book one on sub department 222" }, { "subdepartmentId":"222", "bookTitle": "Book two on sub department 222" } ] } ] }, { "id": "345", "booksubdepartment": [ { "id": "333", "departmentID": "345", "books": [ { "subdepartmentId":"333", "bookTitle": "Book one on sub department 333" }, { "subdepartmentId":"333", "bookTitle": "Book two on sub department 333" } ] }, { "id": "444", "books": [ { "subdepartmentId":"444", "bookTitle": "Book one on sub department 444" }, { "subdepartmentId":"444", "bookTitle": "Book two on sub department 444" } ] } ] } ] }; // Resolver实现(同上) const resolvers = { Query: { bookdepartments: () => mockData.department, }, Bookdepartment: { booksubdepartment: (parent) => { return parent.booksubdepartment.map(subDept => ({ ...subDept, departmentID: subDept.departmentID || parent.id, })); }, }, Book: { id: (parent) => `${parent.subdepartmentId}-${parent.bookTitle.toLowerCase().replace(/\s+/g, '-')}`, }, }; // 启动服务器 async function startServer() { const server = new ApolloServer({ typeDefs, resolvers, }); const { url } = await startStandaloneServer(server, { listen: { port: 4000 }, }); console.log(`🚀 服务器运行在: ${url}`); } startServer();
5. 使用说明
- 安装依赖:
npm install @apollo/server graphql
- 运行代码,访问
http://localhost:4000打开Apollo Sandbox - 执行以下查询:
query GetBookDepartments { bookdepartments { id booksubdepartment { id departmentID books { id subdepartmentId bookTitle } } } }
即可得到符合类型定义的完整结果,包括补全的departmentID和自动生成的Book的id。
内容的提问来源于stack exchange,提问作者erwin
相关产品推荐
相关产品推荐

