简体   繁体   English

Graphql apollo 服务器解析器 arguments 类型

[英]Graphql apollo server resolvers arguments types

Type script is showing error not mentioning argument type for each arguments:类型脚本显示错误,未提及每个 arguments 的参数类型:

  Mutation: {
    createUser: (parent, args, context, info) =>{

    }

I can solve by using any type, but what are the correct types?我可以使用任何类型来解决,但正确的类型是什么?

  Mutation: {
    createUser: (parent: any, args: any, context: any, info: any) =>{

    }

在此处输入图像描述

If you enable all strict type-checking options in tsconfig.json , you should add TS type for everything.如果在tsconfig.json中启用所有严格的类型检查选项,则应为所有内容添加 TS 类型。

Let's take a look at the type of resolver.我们来看看解析器的类型。

export declare type IFieldResolver<TSource, TContext, TArgs = Record<string, any>> = (source: TSource, args: TArgs, context: TContext, info: GraphQLResolveInfo & {
 mergeInfo: MergeInfo;
}) => any;

Resolver arguments:旋转变压器 arguments:

parent - For resolvers of top-level fields with no parent (such as fields of Query , Mutation ), this value is undefined . parent - 对于没有父级的顶级字段的解析器(例如QueryMutation的字段),此值为undefined So the TS type is undefined所以 TS 类型是undefined

args - As you can see from the type, it must meet the type parameters in generic constraints Record<string, any> . args - 从类型中可以看出,它必须满足泛型约束Record<string, any>中的类型参数。 Since the actual parameter is passed from the Graphql client, we need to define the Args type/interface for each resolver.由于实际参数是从 Graphql 客户端传递的,因此我们需要为每个解析器定义Args类型/接口。

context - It's a generic parameter, we need to define the application context interface by ourselves. context - 这是一个通用参数,我们需要自己定义应用程序上下文接口。

info - The TS type already there GraphQLResolveInfo & { mergeInfo: MergeInfo } info - TS 类型已经存在GraphQLResolveInfo & { mergeInfo: MergeInfo }

An working example:一个工作示例:

Eg例如

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'));

package versions: package 版本:

"typescript": "^3.9.6",
"apollo-server": "^2.15.1",
"graphql": "^14.6.0",

GraphQL Query in client-side: GraphQL 在客户端查询:

mutation{
  createUser(email: "teresa@gmail.com", password: "1234")
}

Response:回复:

{
  "data": {
    "createUser": true
  }
}

Logs in server-side:登录服务器端:

Apollo server started at http://localhost:8080
undefined

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM