无法将类型“ ApolloServer”的参数分配给类型“ ApolloServerBase”的参数

问题描述 投票:2回答:1

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

'ApolloServer'类型的参数不能分配给'ApolloServerBase'类型的参数。属性“ requestOptions”的类型不兼容。类型“ Partial>”不可分配给类型“ Partial>”。属性'documentStore'的类型不兼容。键入'import(“ / Users / ldu020 / workspace / github.com / mrdulin / cnodejs-graphql-api-apollo / node_modules / apollo-server-caching / dist / InMemoryLRUCache”)。未定义”无法分配为类型“ import(“ / Users / ldu020 / workspace / github.com / mrdulin / cnodejs-graphql-api-apollo / node_modules / apollo-server-testing / node_modules / apollo-server-caching / dist / InMemoryLRUCache“)。InMemoryLRUCache ...'。输入'import(“ / Users / ldu020 / workspace / github.com / mrdulin / cnodejs-graphql-api-apollo / node_modules / apollo-server-caching / dist / InMemoryLRUCache”)。InMemoryLRUCache'不能分配给'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功能的接口是:

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

因此,当我尝试执行这样的类型断言时:

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

[tsc引发新类型错误:

类型为'import(“ / Users / ldu020 / workspace / github.com / mrdulin / cnodejs-graphql-api-apollo / node_modules / apollo-server-core / dist / ApolloServer”的参数。ApolloServerBase'不能分配给类型为'import(“ / Users / ldu020 / workspace / github.com / mrdulin / cnodejs-graphql-api-apollo / node_modules / apollo-server-testing / node_modules / apollo-server-core / dist / ApolloServer”的参数)。 ApolloServerBase”。属性“ requestOptions”的类型不兼容。类型“ Partial>”不可分配给类型“ Partial>”。属性'documentStore'的类型不兼容。键入'import(“ / Users / ldu020 / workspace / github.com / mrdulin / cnodejs-graphql-api-apollo / node_modules / apollo-server-caching / dist / InMemoryLRUCache”)。未定义”无法分配为类型“ import(“ / Users / ldu020 / workspace / github.com / mrdulin / cnodejs-graphql-api-apollo / node_modules / apollo-server-testing / node_modules / apollo-server-caching / dist / InMemoryLRUCache“)。InMemoryLRUCache ...'。输入'import(“ / Users / ldu020 / workspace / github.com / mrdulin / cnodejs-graphql-api-apollo / node_modules / apollo-server-caching / dist / InMemoryLRUCache”)。InMemoryLRUCache'不能分配给'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);这样的断言。它可以工作,但是我想正确地输入类型。

typescript apollo-server apollo-server-testing
1个回答
0
投票

我认为此问题是由apollo-server-expressapollo-server-testing之间的不同版本引起的。我已经更新到apollo-server-express和apollo-server-testing到2.9.16。然后,此类型错误已消失。请尝试。

© www.soinside.com 2019 - 2024. All rights reserved.