我遵循Apollo Federation的指南:实施服务
代码在这里:
import { ApolloServer, gql, IResolvers } from 'apollo-server';
import { buildFederatedSchema } from '@apollo/federation';
import faker from 'faker';
const MemoryDB = {
users: [
{ id: 1, name: faker.name.findName() },
{ id: 2, name: faker.name.findName() },
],
};
const typeDefs = gql`
type Query {
userById(id: ID!): User
}
type User @key(fields: "id") {
id: ID!
name: String!
}
`;
const resolvers: IResolvers = {
Query: {
me: (_, { id }, { db }) => {
return db.users.find((u) => u.id === +id);
},
},
User: {
__resolveReference(user, { db }) {
return db.users.find((u) => u.id === user.id);
},
},
};
const server = new ApolloServer({
schema: buildFederatedSchema([{ typeDefs, resolvers }]), // TSC throw error here
});
TSC 抛出类型错误:
类型 'IResolvers<any, any>' 不可分配给类型 'GraphQLResolverMap'。索引签名不兼容。键入'(() => 任何) | GraphQLScalarType | IEnumResolver | IResolverObject<任何,任何,任何> | IResolverOptions<any, any, any>' 不可分配给类型 'GraphQLScalarType | { [枚举值:字符串]:字符串 | 数字; } | { [fieldName: string]: GraphQLFieldResolver<any, any, { [argName: string]: any; }> | { 需要?:字符串 | 不明确的; 解决:GraphQLFieldResolver<...>; }; }'。类型 '() => any' 不可分配给类型 'GraphQLScalarType | { [枚举值:字符串]:字符串 | 数字; } | { [fieldName: string]: GraphQLFieldResolver<any, any, { [argName: string]: any; }> | { 需要?:字符串 | 不明确的; 解决:GraphQLFieldResolver<...>; }; }'。类型 ' () => any' 不能分配给类型 '{ [fieldName: string]: GraphQLFieldResolver<any, any, { [argName: string]: any; }> | { 需要?:字符串 | 不明确的; 解析:GraphQLFieldResolver<any, any, { [argName: string]: any; }>; }; }'。'() => any'.ts(2322) 类型中缺少索引签名
软件包版本:
"apollo-server": "^2.12.0",
"@apollo/federation": "^0.17.0",
好像IResolvers
接口不兼容,但是GraphQLResolverMap
接口没有暴露,所以我无法导入和使用。我不想将解析器的类型强制转换为any
类型。这是一个错误吗?
更新
apollo-server
更新后问题依旧"apollo-server": "^2.15.1",