typescript - 'ApolloServer' türündeki argüman, 'ApolloServerBase' türündeki parametreye atanamaz


apollo-server-testing paketini kullanarak entegrasyon testleri yazıyorum, resmi dokümanları takip ediyorum. Ancak ApolloServer sınıfının örneğini createTestClient metoduna geçtiğimde, tsc tür uyumsuzluğu hatası veriyor.

Tür türündeki 'ApolloServer' argümanı, 'ApolloServerBase' türündeki parametreye atanamaz. 'requestOptions' özelliğinin türleri uyumsuz. 'Partial' türü, 'Partial' türüne atanamaz. 'documentStore' özelliğinin türleri uyumsuz. 'import("/Users/ldu020/workspace/github.com/mrdulin/cnodejs-graphql-api-apollo/node_modules/apollo-server-caching/dist/InMemoryLRUCache").InMemoryLRUCache | undefined' türü, 'import("/Users/ldu020/workspace/github.' türüne atanamaz. 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' türü, 'import("/Users/ldu020/workspace/github.com/mrdulin/cnodejs-graphql-api-apollo/node_modules/apollo-server-testing/node_modules/apollo-server-caching/dist/InMemoryLRUCache").InMemoryLRUCache' türüne atanabilir değil.

İşte minimum çoğaltan kod:

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 };

Bağımlılık sürümleri:

"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 fonksiyonunun arayüzü şudur:

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

Yani bu şekilde tür atamalarını yapmaya çalıştığımda:

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

tsc yeni bir tip hatası fırlatır:

Türü ''import("/Users/ldu020/workspace/github.com/mrdulin/cnodejs-graphql-api-apollo/node_modules/apollo-server-core/dist/ApolloServer").ApolloServerBase'' olan argüman, türü ''import("/Users/ldu020/workspace/github.com/mrdulin/cnodejs-graphql-api-apollo/node_modules/apollo-server-testing/node_modules/apollo-server-core/dist/ApolloServer").ApolloServerBase'' olan parametreye atanamaz. 'requestOptions' özelliğinin türleri uyumsuz. Tür ''Partial>'' türüne atanamaz ''Partial>''. 'documentStore' özelliğinin türleri uyumsuz. Tür ''import("/Users/ldu020/workspace/github.com/mrdulin/cnodejs-graphql-api-apollo/node_modules/apollo-server-caching/dist/InMemoryLRUCache").' 'InMemoryLRUCache | undefined', tür 'import("/Users/ldu020/workspace/github.com/mrdulin/cnodejs-graphql-api-apollo/node_modules/apollo-server-testing/node_modules/apollo-server-caching/dist/InMemoryLRUCache").InMemoryLRUCache ...' ile atanabilir değil. Tür 'import("/Users/ldu020/workspace/github.com/mrdulin/cnodejs-graphql-api-apollo/node_modules/apollo-server-caching/dist/InMemoryLRUCache").InMemoryLRUCache', tür 'import("/Users/ldu020/workspace/github.com/mrdulin/cnodejs-graphql-api-apollo/node_modules/apollo-server-testing/node_modules/apollo-server-caching/dist/InMemoryLRUCache").InMemoryLRUCache' ile atanabilir değil.

Type assertions yapmak istemiyorum, örneğin createTestClient(server as any);. Bu çalışıyor, ama türü doğru şekilde yapmak istiyorum.

Cevap #1

Bence bu sorun, apollo-server-express ve apollo-server-testing arasındaki farklı sürümlerden kaynaklanıyor. apollo-server-express ve apollo-server-testing'i 2.9.16 sürümüne güncelledim. Sonra bu tür hatası kayboldu. Lütfen deneyin.

İlgili makaleler