访问 Apollo 服务器以进行 NestJS GraphQL 测试

     2023-03-08     213

关键词:

【中文标题】访问 Apollo 服务器以进行 NestJS GraphQL 测试【英文标题】:Access to Apollo server for NestJS GraphQL test 【发布时间】:2019-09-24 03:27:30 【问题描述】:

测试 Apollo GraphQL 服务器的标准方法是使用Apollo test client。 createTestClient 方法需要 server 参数。 在 NestJS/TypeGraphQL 应用程序中,从 (Jest) 测试内部访问由 GraphQLModule 创建的 Apollo 服务器的适当方法是什么?

【问题讨论】:

【参考方案1】:
    const moduleFixture = await Test.createTestingModule(
      imports: [ApplicationModule],
    ).compile()
    const app = await moduleFixture.createNestApplication(new ExpressAdapter(express)).init()
    const module: GraphQLModule = moduleFixture.get<GraphQLModule>(GraphQLModule)
    const apolloClient = createTestClient((module as any).apolloServer)

这就是我的工作

【讨论】:

在我的项目中,这会导致 TypeError: Cannot read property 'executeOperation' of undefined,因为 module.apolloServer 未定义。 NestFactory.createApplicationContext(AppModule) 也是如此。使用"@nestjs/graphql": "^6.5.1"【参考方案2】:

此 PR https://github.com/nestjs/graphql/pull/1104 使您能够使用 apollo-server-testing 编写测试。

【讨论】:

【参考方案3】:

这段代码对我有用。感谢 JustMe

import  Test, TestingModule  from '@nestjs/testing';
import  INestApplication  from '@nestjs/common';
import  createTestClient, TestQuery  from 'apollo-server-integration-testing';

import  AppModule  from './../src/app.module';
import  GraphQLModule  from '@nestjs/graphql';

describe('AppController (e2e)', () => 
  let app: INestApplication;
  // let mutateTest: TestQuery;
  let correctQueryTest: TestQuery;
  let wrongQueryTest: TestQuery;

  beforeEach(async () => 
    const moduleFixture: TestingModule = await Test.createTestingModule(
      imports: [AppModule],
    ).compile();

    app = moduleFixture.createNestApplication();
    await app.init();
    const module: GraphQLModule =
      moduleFixture.get<GraphQLModule>(GraphQLModule);
    const  query: correctQuery  = createTestClient(
      apolloServer: (module as any).apolloServer,
      extendMockRequest: 
        headers: 
          token:
            'iIsInR5cCI6IkpXVCJ9.eyJfaWQiOiI2MWFiNmY0MjQ3YjEyYWNiNzQyYmQwYmYiLCJyb2xlIjoibWFuYWdlciIsImVtYWlsIjoibGVuYUBtYWlsLmNvbSIsInBhc3N3b3JkIjoiZTEwYWRjMzk0OWJhNTlhYmJlNTZlMDU3ZjIwZjg4M2UiLCJ1c2VybmFtZSI6ImxlbmEgZG9lIiwiY3JlYXRlZEF0IjoiMjAyMS0xMi0wNFQxMzozODoxMC4xMzZaIiwidXBkYXRlZEF0IjoiMjAyMS0xMi0wNFQxMzozODoxMC4xMzZaIiwiX192IjowLCJpYXQiOjE2Mzg2NTE4MjMsImV4cCI6MTYzODY1MTg4M30.d6SCh4x6Wwpj16UWf4ca-PbFCo1FQm_bLelp8kscG8U',
        ,
      ,
    );
    const  query: wrongQuery  = createTestClient(
      apolloServer: (module as any).apolloServer,
    );
    // mutateTest = mutate;
    correctQueryTest = correctQuery;
    wrongQueryTest = wrongQuery;
  );

  it('/ Correct', async () => 
    const result = await correctQueryTest(`
        query FILTER_JOBS
          filterJobs(status: DONE) 
            title,
            status,
            description,
            lat,
            long,
            employees,
            images,
            assignedBy 
              username,
              email
            
          
        
    `);
    console.log(result);
  );
  it('/ Wrong', async () => 
    const result = await wrongQueryTest(`
        query FILTER_JOBS
          filterJobs(status: DONE) 
            title,
            status,
            description,
            lat,
            long,
            employees,
            images,
            assignedBy 
              username,
              email
            
          
        
    `);
    console.log(result);
  );
);

【讨论】:

nodejs升级后运行apollo服务器的问题(使用nestjs和fastify)

】nodejs升级后运行apollo服务器的问题(使用nestjs和fastify)【英文标题】:Problemrunningapolloserverafternodejsupgrade(usingnestjsandfastify)【发布时间】:2021-08-1417:18:50【问题描述】:我的NestJs应用出现问题。项目一直运行到昨天为止。显然... 查看详情

Apollo 服务器设置 cookie 以在下一个 js 应用程序中访问

】Apollo服务器设置cookie以在下一个js应用程序中访问【英文标题】:Apolloserversetcookietobeaccessedinnextjsapp【发布时间】:2020-02-1910:45:05【问题描述】:我有一个Next.js应用程序,我正在使用apollo-server-micro和apollo-client。当用户点击登... 查看详情

NestJS 与 Apollo 数据源

】NestJS与Apollo数据源【英文标题】:NestJSwithApolloDataSource【发布时间】:2020-11-0412:48:42【问题描述】:我一直在尝试使用NestJS重新创建Apollotutorial。但是当我尝试将apollo-datasource-rest与NestJS一起使用时,从外部数据源获取数据时失... 查看详情

通过 Apollo Server (NestJS) 处理异常

】通过ApolloServer(NestJS)处理异常【英文标题】:ProcessinganexceptionthroughApolloServer(NestJS)【发布时间】:2020-09-0323:24:19【问题描述】:有没有办法通过apollo异常处理程序手动运行异常?我在GraphQL中有90%的应用程序,但仍然有两个模块... 查看详情

CORS 以某种方式阻止我的 Angular 和 Nestjs 应用程序进行通信

】CORS以某种方式阻止我的Angular和Nestjs应用程序进行通信【英文标题】:CORSissomehowstoppingmyAngularandNestjsappsfromcommunicating【发布时间】:2020-07-1011:15:45【问题描述】:我正在使用NGXS构建一个带有Angular和NestJS的应用程序来进行状态... 查看详情

Nestjs 中的 Graphql Apollo 上传返回无效值

】Nestjs中的GraphqlApollo上传返回无效值【英文标题】:GraphqlApollouploadinNestjsreturnsinvalidvalue【发布时间】:2020-05-2010:21:42【问题描述】:我尝试使用graphql-upload的GraphQLUpload标量向GraphQL端点添加上传参数:importFileUpload,GraphQLUploadfrom\'... 查看详情

如何访问 JwtToken 以检查 nestjs 护照策略中的黑名单?

】如何访问JwtToken以检查nestjs护照策略中的黑名单?【英文标题】:HowtogetaccesstoJwtTokentocheckwithblacklistwithinnestjspassportstrategy?【发布时间】:2021-01-1003:06:22【问题描述】:我正在尝试在JWTStrategy中检查列入黑名单的JWT令牌。jwtFromReq... 查看详情

GraphQL + NestJS - 我如何在警卫中访问@Args?

】GraphQL+NestJS-我如何在警卫中访问@Args?【英文标题】:GraphQL+NestJS-howcanIaccess@Argsinaguard?【发布时间】:2020-12-1413:27:42【问题描述】:我需要以某种方式从警卫内部的@Args访问objectId,以检查发件人是否将objectId分配给他的帐户。... 查看详情

NestJs 使用 jwt 和私钥和公钥进行身份验证

...务,成功登录后,客户端获取jwt令牌,并使用此令牌可以访问另一个微服务。这里是auth服务的JwtStrategy和Au 查看详情

Nestjs websocket网关,如何从握手中解析签名cookie以进行保护授权?

】Nestjswebsocket网关,如何从握手中解析签名cookie以进行保护授权?【英文标题】:Nestjswebsocketgateway,howtoparsesignedcookiesfromhandshakeforguardauthorization?【发布时间】:2020-04-1318:27:57【问题描述】:我的守卫包含以下代码:letclient:Socket=c... 查看详情

在 nextJS 上配置 apollo 客户端以进行订阅的问题

】在nextJS上配置apollo客户端以进行订阅的问题【英文标题】:IssueconfiguringapolloclientonnextJSforsubscriptions【发布时间】:2020-01-2605:57:02【问题描述】:所以,Apollo客户端用于进行GraphQL查询和变异调用,但是当我配置websocketLink时,我... 查看详情

AWS Lambda 上的 Nestjs(无服务器框架)|如何访问事件参数?

】AWSLambda上的Nestjs(无服务器框架)|如何访问事件参数?【英文标题】:NestjsonAWSLambda(ServerlessFramework)|Howtoaccesstheeventparameter?【发布时间】:2021-03-2016:14:39【问题描述】:我在AWSLambda上托管Nestjs应用程序(使用无服务器框架)。... 查看详情

如何访问 GraphQL 解析器中的请求对象(使用 Apollo-Server-Express)

...-02-1812:01:55【问题描述】:我有一个使用GraphQL的标准快递服务器constserver=express();server.use(\'/g 查看详情

客户端应用接入apollo

...corp/apollo/wiki/Java客户端使用指南接入之前,需要先在Apollo服务器创建需要的配置项。默认访问:localhost:8070创建项目添加并发布配置项修改application.properties或bootstrap.properties配置在启动类添加@EnableApolloConfig注解。目前在我们公... 查看详情

在 Nestjs 中使用 graphql 进行授权

...ss和graphql。我在尝试授权使用jwt令牌进行身份验证的用户访问时遇到问题。我按照教程在Nestjswebsite上进行身份验证。我能够获取当前用户,但是当我尝试实现basicrolebaseaccessco 查看详情

使用 Prisma 2 和 NestJS 进行日志记录 - 依赖注入问题?

】使用Prisma2和NestJS进行日志记录-依赖注入问题?【英文标题】:LoggingwithPrisma2andNestJS-DependencyInjectionproblem?【发布时间】:2021-08-0303:28:17【问题描述】:目前在我的第一个NestJS项目中。我正在使用Prisma2,并希望以调试模式将查... 查看详情

NestJS - 如何检查 JWT 是不是可以访问特定端点

】NestJS-如何检查JWT是不是可以访问特定端点【英文标题】:NestJS-howtocheckifJWTcanaccessspecificendpointNestJS-如何检查JWT是否可以访问特定端点【发布时间】:2021-01-2705:37:45【问题描述】:假设我有3个端点:A用于生成JWT令牌,B用于访... 查看详情

如何在 NestJS 中热重载联邦网关

...决问题,但似乎不是一个优雅的解决方案。其他资源Apollo服务器支持managedfederation 查看详情