阿波罗联邦打字稿解析器参数

Posted

技术标签:

【中文标题】阿波罗联邦打字稿解析器参数【英文标题】:apollo federation typescript resolver arguments 【发布时间】:2022-01-17 05:45:36 【问题描述】:

您好,我有一个与 apollo federation 有关的问题,我一直在使用 apollo server 来试验 graphql 和 typescript 我一切正常,但我想搬到 apollo federation,我不得不将架构构建方法更改为 buildSubgraphSchema,我是我的解析器的类型定义有些问题。

这是我的 graphql 架构:

import  gql  from 'apollo-server-koa';

const typeDefs = gql` 
type State 
  stateId: Int
  name: String


extend type Query 
  state(stateId: Int!): State
  stateList: [State]


extend type Mutation 
  createState(
    name: String!
  ) : State

  updateState(
    stateId: Int!
    name: String!
  ) : State

  deleteState(
    stateId: Int!
  ) : State

`;

export default typeDefs;

这是我在阿波罗服务器中的解析器:

import  PrismaClient  from '@prisma/client';
import  State, UpdateStateInput, DeleteStateInput  from '../types/state';

const prisma = new PrismaClient();

const stateResolver = 
  Query: 
    state: (_params: any, args:  stateId: number ) => 
      return prisma.state.findUnique(
        where:  stateId: args.stateId || undefined ,
      );
    ,
    stateList: () => 
      return prisma.state.findMany();
    ,
  ,
  Mutation: 
    createState: (_parent: any, args: State) => 
      return prisma.state.create( data:  name: args.name  );
    ,
    updateState: (_parent: any, args: UpdateStateInput) => 
      const  stateId, ...data  = args;
      return prisma.state.update( where:  stateId , data );
    ,
    deleteState: (_parent: any, args: DeleteStateInput) => 
      return prisma.state.delete( where:  stateId: args.stateId  );
    ,
  ,
  // Relation resolvers
  State: 
    notary: (parent: any) => 
      return prisma.notary.findMany( where:  stateId: parent?.stateId  );
    ,
  ,
;

export default stateResolver;

当我将该解析器传递给 buildSubgraphSchema 时,打字稿显示此错误:

Type ' Query:  state: (_params: any, args:     stateId: number;) => Prisma.Prisma__StateClient<State | null>; stateList: () => PrismaPromise<State[]>; ; Mutation:  createState: (_parent: any, args: State) => Prisma.Prisma__StateClient<...>; updateState: (_parent: any, args: UpdateStateInput) => Prisma.Prisma__StateC...' is not assignable to type 'GraphQLResolverMap<any>'.
  Property 'Query' is incompatible with index signature.
    Type ' state: (_params: any, args:     stateId: number;) => Prisma.Prisma__StateClient<State | null>; stateList: () => PrismaPromise<State[]>; ' is not assignable to type 'GraphQLScalarType |  [enumValue: string]: string | number;  |  [fieldName: string]: GraphQLFieldResolver<any, any,  [argName: string]: any; > |  ...; ; '.
      Type ' state: (_params: any, args:     stateId: number;) => Prisma.Prisma__StateClient<State | null>; stateList: () => PrismaPromise<State[]>; ' is not assignable to type ' [fieldName: string]: GraphQLFieldResolver<any, any,  [argName: string]: any; > |  requires?: string | undefined; resolve: GraphQLFieldResolver<any, any,  [argName: string]: any; >; ; '.
        Property 'state' is incompatible with index signature.
          Type '(_params: any, args:     stateId: number;) => Prisma.Prisma__StateClient<State | null>' is not assignable to type 'GraphQLFieldResolver<any, any,  [argName: string]: any; > |  requires?: string | undefined; resolve: GraphQLFieldResolver<any, any,  [argName: string]: any; >; '.
            Type '(_params: any, args:     stateId: number;) => Prisma.Prisma__StateClient<State | null>' is not assignable to type 'GraphQLFieldResolver<any, any,  [argName: string]: any; >'.
              Types of parameters 'args' and 'args' are incompatible.
                Property 'stateId' is missing in type ' [argName: string]: any; ' but required in type ' stateId: number; '.

如果我在所有解析器方法上设置args: any,错误就会得到解决。考虑到它丢失了那里的类型定义,我想知道这是否是正确的方法。

谢谢。

【问题讨论】:

【参考方案1】:

我遇到了同样的问题,但我使用的是 apollo 服务器 fastify。我使用了来自 apollo-graphql 的 GraphqlResolverMap 类型。只需将此类型提供给您的解析器并指定您的上下文类型,如下所示:GraphqlResolverMap

顺便说一句,我在启动网关时遇到了问题。如果你能帮助我,我将不胜感激。 Apollo federation can't start gateway

【讨论】:

以上是关于阿波罗联邦打字稿解析器参数的主要内容,如果未能解决你的问题,请参考以下文章

Babel 模块解析器无法按预期工作(节点 + 打字稿)

解决错误:作为解析器 ESLint 加载的带有无效接口的打字稿

如何将实例变量传递给打字稿装饰器参数?

无法为阿波罗服务器中的解析器设置上下文

打字稿方法返回未定义的方法装饰器

打字稿装饰器不能使用箭头函数