英文:
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 '../../../__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 };
Dependencies versions:
"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"
The interface of createTestClient
function is:
import { ApolloServerBase } from 'apollo-server-core';
// ...
(server: ApolloServerBase): ApolloServerTestClient
So when I try to do the type assertions like this:
import { ApolloServerBase } from 'apollo-server-core';
// ...
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-express
和apollo-server-testing
之间的不同版本引起的。
我已经将apollo-server-express
和apollo-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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论