Graphql apollo 服务器解析器参数类型
Graphql apollo server resolvers arguments types
类型脚本显示错误,未提及每个参数的参数类型:
Mutation: {
createUser: (parent, args, context, info) =>{
}
我可以使用任何类型来解决,但正确的类型是什么?
Mutation: {
createUser: (parent: any, args: any, context: any, info: any) =>{
}
如果在 tsconfig.json
中启用所有严格类型检查选项,则应为所有内容添加 TS 类型。
让我们来看看解析器的类型。
export declare type IFieldResolver<TSource, TContext, TArgs = Record<string, any>> = (source: TSource, args: TArgs, context: TContext, info: GraphQLResolveInfo & {
mergeInfo: MergeInfo;
}) => any;
解析器参数:
parent
- 对于没有父级的顶级字段的解析器(例如 Query
、Mutation
的字段),此值为 undefined
。所以TS类型是undefined
args
——从类型可以看出,它必须满足泛型约束中的类型参数Record<string, any>
。由于实际参数是从Graphql客户端传递过来的,所以我们需要为每个解析器定义Args
type/interface
context
- 泛型参数,需要我们自己定义应用上下文接口。
info
- TS 类型已经存在 GraphQLResolveInfo & { mergeInfo: MergeInfo }
一个工作示例:
例如
import express from 'express';
import { ApolloServer, gql, MergeInfo } from 'apollo-server-express';
import { GraphQLResolveInfo } from 'graphql';
const app = express();
const typeDefs = gql`
type User {
email: String!
}
type Query {
user: User
}
type Mutation {
createUser(email: String!, password: String!): Boolean
}
`;
export declare type IFieldResolver<TSource, TContext, TArgs = Record<string, any>> = (source: TSource, args: TArgs, context: TContext, info: GraphQLResolveInfo & {
mergeInfo: MergeInfo;
}) => any;
type CreateUserArgs = {
email: string;
password: string;
};
interface AppContext {
userService: UserService;
}
const resolvers = {
Query: {},
Mutation: {
createUser: (
parent: undefined,
args: CreateUserArgs,
context: AppContext,
info: GraphQLResolveInfo & { mergeInfo: MergeInfo },
) => {
console.log(parent);
return context.userService.createUser(args.email, args.password);
},
},
};
interface UserService {
createUser(email: string, password: string): boolean;
}
class UserServiceImpl {
createUser(email: string, password: string) {
return true;
}
}
const server = new ApolloServer({
typeDefs,
resolvers,
context: {
userService: new UserServiceImpl(),
},
});
server.applyMiddleware({ app, path: '/graphql' });
app.listen(8080, () => console.log('Apollo server started at http://localhost:8080'));
包版本:
"typescript": "^3.9.6",
"apollo-server": "^2.15.1",
"graphql": "^14.6.0",
客户端中的 GraphQL 查询:
mutation{
createUser(email: "teresa@gmail.com", password: "1234")
}
回复:
{
"data": {
"createUser": true
}
}
服务器端登录:
Apollo server started at http://localhost:8080
undefined
类型脚本显示错误,未提及每个参数的参数类型:
Mutation: {
createUser: (parent, args, context, info) =>{
}
我可以使用任何类型来解决,但正确的类型是什么?
Mutation: {
createUser: (parent: any, args: any, context: any, info: any) =>{
}
如果在 tsconfig.json
中启用所有严格类型检查选项,则应为所有内容添加 TS 类型。
让我们来看看解析器的类型。
export declare type IFieldResolver<TSource, TContext, TArgs = Record<string, any>> = (source: TSource, args: TArgs, context: TContext, info: GraphQLResolveInfo & {
mergeInfo: MergeInfo;
}) => any;
解析器参数:
parent
- 对于没有父级的顶级字段的解析器(例如 Query
、Mutation
的字段),此值为 undefined
。所以TS类型是undefined
args
——从类型可以看出,它必须满足泛型约束中的类型参数Record<string, any>
。由于实际参数是从Graphql客户端传递过来的,所以我们需要为每个解析器定义Args
type/interface
context
- 泛型参数,需要我们自己定义应用上下文接口。
info
- TS 类型已经存在 GraphQLResolveInfo & { mergeInfo: MergeInfo }
一个工作示例:
例如
import express from 'express';
import { ApolloServer, gql, MergeInfo } from 'apollo-server-express';
import { GraphQLResolveInfo } from 'graphql';
const app = express();
const typeDefs = gql`
type User {
email: String!
}
type Query {
user: User
}
type Mutation {
createUser(email: String!, password: String!): Boolean
}
`;
export declare type IFieldResolver<TSource, TContext, TArgs = Record<string, any>> = (source: TSource, args: TArgs, context: TContext, info: GraphQLResolveInfo & {
mergeInfo: MergeInfo;
}) => any;
type CreateUserArgs = {
email: string;
password: string;
};
interface AppContext {
userService: UserService;
}
const resolvers = {
Query: {},
Mutation: {
createUser: (
parent: undefined,
args: CreateUserArgs,
context: AppContext,
info: GraphQLResolveInfo & { mergeInfo: MergeInfo },
) => {
console.log(parent);
return context.userService.createUser(args.email, args.password);
},
},
};
interface UserService {
createUser(email: string, password: string): boolean;
}
class UserServiceImpl {
createUser(email: string, password: string) {
return true;
}
}
const server = new ApolloServer({
typeDefs,
resolvers,
context: {
userService: new UserServiceImpl(),
},
});
server.applyMiddleware({ app, path: '/graphql' });
app.listen(8080, () => console.log('Apollo server started at http://localhost:8080'));
包版本:
"typescript": "^3.9.6",
"apollo-server": "^2.15.1",
"graphql": "^14.6.0",
客户端中的 GraphQL 查询:
mutation{
createUser(email: "teresa@gmail.com", password: "1234")
}
回复:
{
"data": {
"createUser": true
}
}
服务器端登录:
Apollo server started at http://localhost:8080
undefined