Аргумент типа «ApolloServer» не может быть назначен параметру типа «ApolloServerBase» - PullRequest
3 голосов
/ 06 января 2020

Я пишу интеграционные тесты, используя пакет apollo-server-testing, следуйте этому официальному документам . Но когда я передаю экземпляр класса ApolloServer методу createTestClient, tsc выдает ошибку, несовместимую с типом.

Аргумент типа 'ApolloServer' не может быть назначен параметру типа ' ApolloServerBase. Типы свойств 'requestOptions' несовместимы. Тип «Частичный>» нельзя назначить типу «Частичный>». Типы свойства documentStore несовместимы. Введите 'import ("/ Users / ldu020 / workspace / github.com / mrdulin / cnodejs -graphql-api-apollo / node_modules / apollo-server-caching / dist / InMemoryLRUCache"). InMemoryLRUCache | undefined "нельзя назначить типу" import ("/ Users / ldu020 / workspace / github.com / mrdulin / cnodejs -graphql-api-apollo / node_modules / apollo-сервер-тестирование / 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-сервер-тестирование / node_modules / apollo-сервер-кэширование / 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 / рабочая область / github.com / mrdulin / cnodejs -graphql-апи-аполлон / node_modules / аполлон-сервер тестирования / node_modules / аполлон-сервера ядро ​​/ DIST / ApolloServer "). ApolloServerBase. Типы свойств 'requestOptions' несовместимы. Тип «Частичный>» нельзя назначить типу «Частичный>». Типы свойства documentStore несовместимы. Введите 'import ("/ Users / ldu020 / workspace / github.com / mrdulin / cnodejs -graphql-api-apollo / node_modules / apollo-server-caching / dist / InMemoryLRUCache"). InMemoryLRUCache | undefined 'нельзя назначить типу' import ("/ Users / ldu020 / workspace / github.com / mrdulin / cnodejs -graphql-api-apollo / node_modules / apollo-сервер-тестирование / 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-сервер-тестирование / node_modules / apollo-сервер-кэширование / dist / InMemoryLRUCache "). InMemoryLRUCache '.

Я не хочу делать утверждения типа, такие как createTestClient(server as any);. Это работает, но я хочу сделать тип правильно.

1 Ответ

1 голос
/ 15 января 2020

Я думаю, что эта проблема вызвана различными версиями между apollo-server-express и apollo-server-testing. Я обновился до apollo-server- express и apollo-server-testing до 2.9.16. Затем эта ошибка типа исчезла. Пожалуйста, попробуйте.

...