简体   繁体   English

阿波罗联邦 typescript 解析器 arguments

[英]apollo federation typescript resolver arguments

Hello i have a question related to apollo federation, ive been using apollo server to experiment with graphql and typescript i had everything working but i wanted to move to apollo federation, i had to change the schema building method to buildSubgraphSchema and i am having some troubles with the type definitions of my resolvers.你好,我有一个与 apollo federation 有关的问题,我一直在使用 apollo 服务器来试验 graphql 和 typescript 我一切正常,但我想转移到 apollo federation,我不得不将模式构建方法更改为 buildSubgraphSchema,我遇到了一些麻烦使用我的解析器的类型定义。

this is my graphql schema:这是我的 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;

this was my resolver in apollo server:这是我在 apollo 服务器中的解析器:

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;

When i pass that resolver to buildSubgraphSchema, typescript shows this error:当我将该解析器传递给 buildSubgraphSchema 时,typescript 显示此错误:

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; }'.

the error gets solved if i set args: any on all the resolver methods.如果我在所有解析器方法上设置args: any ,错误就会得到解决。 i wanted to know if that is the correct way considering that it loses the type definitions there.考虑到它在那里丢失了类型定义,我想知道这是否是正确的方法。

thanks.谢谢。

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

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