typescript - 类型 'ApolloServer' 的参数不可分配给类型 'ApolloServerBase' 的参数

标签 typescript apollo-server

我使用 apollo-server-testing 包编写集成测试,遵循官方 docs 。但是当我将 ApolloServer 类的实例传递给 createTestClient 方法时,tsc 抛出类型不兼容的错误。

Argument of type 'ApolloServer' is not assignable to parameter of type 'ApolloServerBase'. Types of property 'requestOptions' are incompatible. Type 'Partial>' is not assignable to type 'Partial>'. Types of property 'documentStore' are incompatible. Type 'import("/Users/ldu020/workspace/github.com/mrdulin/cnodejs-graphql-api-apollo/node_modules/apollo-server-caching/dist/InMemoryLRUCache").InMemoryLRUCache | undefined' is not assignable to type 'import("/Users/ldu020/workspace/github.com/mrdulin/cnodejs-graphql-api-apollo/node_modules/apollo-server-testing/node_modules/apollo-server-caching/dist/InMemoryLRUCache").InMemoryLRUCache ...'. Type 'import("/Users/ldu020/workspace/github.com/mrdulin/cnodejs-graphql-api-apollo/node_modules/apollo-server-caching/dist/InMemoryLRUCache").InMemoryLRUCache' is not assignable to type 'import("/Users/ldu020/workspace/github.com/mrdulin/cnodejs-graphql-api-apollo/node_modules/apollo-server-testing/node_modules/apollo-server-caching/dist/InMemoryLRUCache").InMemoryLRUCache'.

以下是最小的复制代码:

topic.integration.test.ts:

import { constructTestServer } from '../../../__utils';
import { createTestClient } from 'apollo-server-testing';

describe('topic integration test suites', () => {
  it('should ', () => {
    const { server, cnodeAPI } = constructTestServer();
    const { query } = createTestClient(server); // tsc throw an error here
  });
});

__util.ts:

import { ApolloServer } from 'apollo-server';
import { contextFunction as defaultContext } from '../graphql/context';
import { schema } from '../graphql/schema';
import { CnodeAPI } from '../graphql/api';

const constructTestServer = ({ context = defaultContext } = {}) => {
  const cnodeAPI = new CnodeAPI();

  const server = new ApolloServer({
    schema,
    dataSources: () => ({ cnodeAPI }),
    context,
  });

  return { server, cnodeAPI };
};

export { constructTestServer };

依赖版本:

"apollo-datasource-rest": "^0.6.10",
"apollo-server": "^2.9.14",
"apollo-server-express": "^2.9.14",
"apollo-server-testing": "^2.9.15",
"typescript": "^3.7.4"

createTestClient函数的接口(interface)为:

import { ApolloServerBase } from 'apollo-server-core';
// ...
(server: ApolloServerBase): ApolloServerTestClient

所以当我尝试执行这样的类型断言时:

import { ApolloServerBase } from 'apollo-server-core';
// ...
createTestClient(server as ApolloServerBase);

tsc 抛出新类型错误:

Argument of type 'import("/Users/ldu020/workspace/github.com/mrdulin/cnodejs-graphql-api-apollo/node_modules/apollo-server-core/dist/ApolloServer").ApolloServerBase' is not assignable to parameter of type 'import("/Users/ldu020/workspace/github.com/mrdulin/cnodejs-graphql-api-apollo/node_modules/apollo-server-testing/node_modules/apollo-server-core/dist/ApolloServer").ApolloServerBase'. Types of property 'requestOptions' are incompatible. Type 'Partial>' is not assignable to type 'Partial>'. Types of property 'documentStore' are incompatible. Type 'import("/Users/ldu020/workspace/github.com/mrdulin/cnodejs-graphql-api-apollo/node_modules/apollo-server-caching/dist/InMemoryLRUCache").InMemoryLRUCache | undefined' is not assignable to type 'import("/Users/ldu020/workspace/github.com/mrdulin/cnodejs-graphql-api-apollo/node_modules/apollo-server-testing/node_modules/apollo-server-caching/dist/InMemoryLRUCache").InMemoryLRUCache ...'. Type 'import("/Users/ldu020/workspace/github.com/mrdulin/cnodejs-graphql-api-apollo/node_modules/apollo-server-caching/dist/InMemoryLRUCache").InMemoryLRUCache' is not assignable to type 'import("/Users/ldu020/workspace/github.com/mrdulin/cnodejs-graphql-api-apollo/node_modules/apollo-server-testing/node_modules/apollo-server-caching/dist/InMemoryLRUCache").InMemoryLRUCache'.

我不想执行像 createTestClient(server as any); 这样的类型断言。它有效,但我想正确设置类型。

最佳答案

我认为这个问题是由apollo-server-expressapollo-server-testing之间的版本不同引起的。 我已将 apollo-server-express 和 apollo-server-testing 更新至 2.9.16。然后,这种类型的错误就消失了。 请尝试一下。

关于typescript - 类型 'ApolloServer' 的参数不可分配给类型 'ApolloServerBase' 的参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59608947/

相关文章:

firefox - 如何让双向数据绑定(bind) <select> 在 Firefox 中工作

typescript - 在 TypeScript 中从元组尾部获取第 n 个类型

angular - 将多个点击事件映射到一个方法

mongodb - apollostack/graphql-server - 如何从解析器获取查询中请求的字段

node.js - 如何在 NodeJS 中使用带有 SharpJS 的 Graphql Apollo 处理上传的图像?

orm - GraphQL, Dataloader, [ORM 与否], 有很多关系理解

javascript - 可观察速记