Argument of type ‘ApolloServer’ is not assignable to parameter of type ‘ApolloServerBase’.

huangapple go评论65阅读模式
英文:

Argument of type 'ApolloServer' is not assignable to parameter of type 'ApolloServerBase'

问题

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

以下是最小的可复现代码:

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抛出了错误
  });
});

__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<import("/Users/ldu020/workspace/github.com/mrdulin/cnodejs-graphql-api-apollo/node_modules/apollo-server-core/dist/graphqlOptions").GraphQLServerOptions<any, any>>'不能赋值给类型'Partial<import("/Users/ldu020/workspace/github.com/mrdulin/cnodejs-graphql-api-apollo/node_modules/apollo-server-testing/node_modules/apollo-server-core/dist/graphqlOptions").GraphQLServerOptions<any, any>>'。
属性'documentStore'的类型不兼容。
类型'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/graphql/language/ast").DocumentNode> | undefined'不能赋值给类型'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/graphql/language/ast").DocumentNode> ...'。
类型'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/graphql/language/ast").DocumentNode>'不能赋值给类型'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/graphql/language/ast").DocumentNode>'。

我不想像createTestClient(server as any);那样进行类型断言。它可以工作,但我希望类型正确。

英文:

I write integration tests using apollo-server-testing package follow this official docs. But when I pass the instance of the ApolloServer class to createTestClient method, tsc throw a type incompatible error.

> Argument of type 'ApolloServer' is not assignable to parameter of type 'ApolloServerBase'.
Types of property 'requestOptions' are incompatible.
Type 'Partial<import("/Users/ldu020/workspace/github.com/mrdulin/cnodejs-graphql-api-apollo/node_modules/apollo-server-core/dist/graphqlOptions").GraphQLServerOptions<any, any>>' is not assignable to type 'Partial<import("/Users/ldu020/workspace/github.com/mrdulin/cnodejs-graphql-api-apollo/node_modules/apollo-server-testing/node_modules/apollo-server-core/dist/graphqlOptions").GraphQLServerOptions<any, any>>'.
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<import("/Users/ldu020/workspace/github.com/mrdulin/cnodejs-graphql-api-apollo/node_modules/graphql/language/ast").DocumentNode> | 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<import("/Users/ldu020/workspace/github.com/mrdulin/cnodejs-graphql-api-apollo/node_modules/graphql/language/ast").DocumentNode> ...'.
Type '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/graphql/language/ast").DocumentNode>' 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<import("/Users/ldu020/workspace/github.com/mrdulin/cnodejs-graphql-api-apollo/node_modules/graphql/language/ast").DocumentNode>'.

Here are the minimal reproducing code:

topic.integration.test.ts:

import { constructTestServer } from &#39;../../../__utils&#39;;
import { createTestClient } from &#39;apollo-server-testing&#39;;

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

__util.ts:

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

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

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

  return { server, cnodeAPI };
};

export { constructTestServer };

Dependencies versions:

&quot;apollo-datasource-rest&quot;: &quot;^0.6.10&quot;,
&quot;apollo-server&quot;: &quot;^2.9.14&quot;,
&quot;apollo-server-express&quot;: &quot;^2.9.14&quot;,
&quot;apollo-server-testing&quot;: &quot;^2.9.15&quot;,
&quot;typescript&quot;: &quot;^3.7.4&quot;

The interface of createTestClient function is:

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

So when I try to do the type assertions like this:

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

tsc throw a new type error:

> 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<import("/Users/ldu020/workspace/github.com/mrdulin/cnodejs-graphql-api-apollo/node_modules/apollo-server-core/dist/graphqlOptions").GraphQLServerOptions<any, any>>' is not assignable to type 'Partial<import("/Users/ldu020/workspace/github.com/mrdulin/cnodejs-graphql-api-apollo/node_modules/apollo-server-testing/node_modules/apollo-server-core/dist/graphqlOptions").GraphQLServerOptions<any, any>>'.
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<import("/Users/ldu020/workspace/github.com/mrdulin/cnodejs-graphql-api-apollo/node_modules/graphql/language/ast").DocumentNode> | 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<import("/Users/ldu020/workspace/github.com/mrdulin/cnodejs-graphql-api-apollo/node_modules/graphql/language/ast").DocumentNode> ...'.
Type '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/graphql/language/ast").DocumentNode>' 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<import("/Users/ldu020/workspace/github.com/mrdulin/cnodejs-graphql-api-apollo/node_modules/graphql/language/ast").DocumentNode>'.

I don't want to do type assertions like createTestClient(server as any);. It works, but I want to make the type correctly.

答案1

得分: 9

这个问题可能是由于apollo-server-expressapollo-server-testing之间的不同版本引起的。
我已经将apollo-server-expressapollo-server-testing更新到2.9.16。然后,这个类型错误就消失了。
请尝试一下。

英文:

I think this problem caused by different versions between apollo-server-express and apollo-server-testing.
I've updated to apollo-server-express and apollo-server-testing to 2.9.16. Then, this type error has disappeared.
Please try it.

huangapple
  • 本文由 发表于 2020年1月6日 16:38:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/59608947.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定