NestJS如何从graphql模块获取apolloClient以使用“apollo-server-testing”进行测试?

Mic*_*elB 5 graphql apollo-server nestjs nestjs-testing apollo-testing

虽然标题类似于Access to Apollo server for NestJS GraphQL test,但问题本身很旧,并且该问题中的答案都解决了 Nestjs 中似乎不再存在的行为。

我遵循了这里的 PR https://github.com/nestjs/graphql/pull/1104以及其他几个线程,试图从测试模块获取 apolloClient 来对其运行测试查询,但没有成功。

Typescript 编译器支持的普遍错误是它根本不存在于 GraphQlModule 上。它在哪里?为什么它被删除了?我如何访问这个关键功能?

TypeError: Cannot read properties of undefined (reading 'executeOperation')

      71 | export function createTestClient(testingModule: TestingModule): apolloServerTesting.ApolloServerTestClient {
      72 |     const apolloServer = getApolloServer(testingModule);
    > 73 |     return apolloServerTesting.createTestClient(apolloServer as any);
         |                                ^
      74 | }
Run Code Online (Sandbox Code Playgroud)

基于PR创建的功能:

export function getApolloServer(app: INestApplicationContext): ApolloServerBase {
    try {
        const graphqlModule = app.get(GraphQLModule);
        return (graphqlModule as any).apolloServer;
    } catch (error) {}
    throw new Error(
        'Nest could not find either the GraphQLFederationModule or GraphQLModule. Both modules are not registered in the given application.',
    );
}

export function createTestClient(testingModule: TestingModule): apolloServerTesting.ApolloServerTestClient {
    const apolloServer = getApolloServer(testingModule);
    return apolloServerTesting.createTestClient(apolloServer as any);
}
Run Code Online (Sandbox Code Playgroud)

版本:

TypeError: Cannot read properties of undefined (reading 'executeOperation')

      71 | export function createTestClient(testingModule: TestingModule): apolloServerTesting.ApolloServerTestClient {
      72 |     const apolloServer = getApolloServer(testingModule);
    > 73 |     return apolloServerTesting.createTestClient(apolloServer as any);
         |                                ^
      74 | }
Run Code Online (Sandbox Code Playgroud)

具体测试中的用法:

        const result = await apolloClient.query({
            query: gql`.........
Run Code Online (Sandbox Code Playgroud)

如果我删除“任意”,vscode 智能感知会让我知道该 PR 中的功能不起作用/不准确:

在此输入图像描述