Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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>' 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'.

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>' 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'.

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

like image 621
slideshowp2 Avatar asked Jan 06 '20 08:01

slideshowp2


1 Answers

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.

like image 107
lightnet328 Avatar answered Nov 13 '22 20:11

lightnet328