Node.js+TypeScript GraphQL服务:用户创建Mutation结构问题
实现嵌套结构的User Mutation
我来帮你搞定这个嵌套形式的Mutation问题!你想要的mutation { user { create(...) {...} } }这种结构,本质上是把所有用户相关的Mutation操作都归类到user这个顶层字段下面,下面是具体的实现步骤和代码:
1. 完善User类型定义
先补全你的UserType,确保它包含你需要返回的核心字段:
import { GraphQLObjectType, GraphQLNonNull, GraphQLString } from 'graphql'; export const UserType = new GraphQLObjectType({ name: 'User', fields: () => ({ id: { type: new GraphQLNonNull(GraphQLString) }, name: { type: new GraphQLNonNull(GraphQLString) }, // 可根据业务需求添加其他字段,比如email、createdAt等 }), });
2. 创建用户输入类型(UserInput)
因为创建用户需要传入结构化的data参数,我们需要定义一个GraphQL输入对象类型来约束传入的数据格式:
import { GraphQLInputObjectType, GraphQLNonNull, GraphQLString } from 'graphql'; export const UserInputType = new GraphQLInputObjectType({ name: 'UserInput', fields: () => ({ name: { type: new GraphQLNonNull(GraphQLString) }, // 可添加其他必填/可选输入字段,比如email、password等 }), });
3. 定义User专属的Mutation类型
接下来创建一个包含所有用户操作(比如create、update、delete)的Mutation类型,这样就能把这些操作统一嵌套在user字段下:
import { GraphQLObjectType, GraphQLNonNull } from 'graphql'; import { UserType } from './userType'; import { UserInputType } from './userInputType'; // 假设你有一个处理用户业务逻辑的服务类(实际项目中替换成你的数据库操作逻辑) import { UserService } from '../services/userService'; const userService = new UserService(); export const UserMutation = new GraphQLObjectType({ name: 'UserMutation', fields: () => ({ create: { type: UserType, args: { data: { type: new GraphQLNonNull(UserInputType) }, }, // 处理创建用户的核心逻辑 resolve: async (_parent, args) => { // 调用服务层写入数据库,返回新创建的用户 const newUser = await userService.createUser(args.data); return newUser; }, }, // 可在这里添加其他用户相关Mutation,比如update、delete // update: { ... }, // delete: { ... }, }), });
4. 构建根Mutation类型
把UserMutation作为根Mutation的一个字段,实现你想要的嵌套结构:
import { GraphQLObjectType } from 'graphql'; import { UserMutation } from './userMutation'; export const RootMutation = new GraphQLObjectType({ name: 'Mutation', fields: () => ({ user: { type: UserMutation, // 这个resolve只需返回空对象,实际逻辑在子Mutation中处理 resolve: () => ({}), }, // 如果有其他业务模块的Mutation,比如post、comment,也可以在这里添加 // post: { type: PostMutation, resolve: () => ({}) }, }), });
5. 整合到GraphQL Schema并启动服务
最后把RootMutation和你的RootQuery整合到Schema里,配置到express-graphql中:
import { GraphQLSchema } from 'graphql'; import { RootQuery } from './rootQuery'; // 假设你已定义根查询类型 import { RootMutation } from './rootMutation'; export const schema = new GraphQLSchema({ query: RootQuery, mutation: RootMutation, });
在Express服务器中使用这个Schema:
import express from 'express'; import { graphqlHTTP } from 'express-graphql'; import { schema } from './schema'; const app = express(); app.use('/graphql', graphqlHTTP({ schema: schema, graphiql: true, // 开启GraphiQL可以在浏览器直接测试你的Mutation })); const PORT = 4000; app.listen(PORT, () => { console.log(`GraphQL Server running at http://localhost:${PORT}/graphql`); });
现在你就可以用你想要的格式调用Mutation了:
mutation { user { create( data: { name: "Foo Bar" } ) { id, name } } }
关键说明
- 这种嵌套结构能让Schema按业务模块归类,结构更清晰
- 根Mutation里的
user字段resolve只需返回空对象,实际业务逻辑都在UserMutation的子字段resolve中处理 - 后续添加用户相关Mutation时,直接在
UserMutation里新增字段即可,无需修改根Mutation结构
内容的提问来源于stack exchange,提问作者Øyvind
相关产品推荐
相关产品推荐

