Nestjs - @nestjs/graphql GraphQLGatewayModule 总是返回错误请求

Posted

技术标签:

【中文标题】Nestjs - @nestjs/graphql GraphQLGatewayModule 总是返回错误请求【英文标题】:Nestjs - @nestjs/graphql GraphQLGatewayModule always returns Bad Request 【发布时间】:2020-07-01 18:44:16 【问题描述】:

我正在尝试使用来自 @nestjs/graphql 的 GraphQLGatewayModule 模块。

为了测试,我正在免费使用 GraphQL API:https://anilist.co/graphiql

但我总是得到 HTTP 400 状态。

我的代码:

import  Module  from '@nestjs/common';
import  GraphQLGatewayModule  from '@nestjs/graphql';

@Module(
  imports: [
    GraphQLGatewayModule.forRoot(
      server: 
        // ... Apollo server options
        debug: true,
        cors: true
      ,
      gateway: 
        debug: true,
        serviceList: [ name: 'TEST_GRAPHQL_GATEWAY', url: 'http://user-service:3000/graphql' ],
      
    )
  ]
)
export class AppModule  

我总是得到 HTTP 400 状态。但是这个api没问题。我收到这条消息:

Encountered error when loading TEST_GRAPHQL_GATEWAY at http://user-service:3000/graphql: 400: Bad Request
[INFO] Fri Mar 20 2020 17:52:20 GMT+0000 (Coordinated Universal Time) apollo-gateway: Gateway successfully loaded schema.
    * Mode: unmanaged
(node:368) UnhandledPromiseRejectionWarning: Error: Expected undefined to be a GraphQL schema.
    at assertSchema (/app/node_modules/graphql/type/schema.js:41:11)
    at validateSchema (/app/node_modules/graphql/type/validate.js:44:28)
    at assertValidSchema (/app/node_modules/graphql/type/validate.js:68:16)
    at assertValidExecutionArguments (/app/node_modules/graphql/execution/execute.js:136:35)
    at executeImpl (/app/node_modules/graphql/execution/execute.js:86:3)
    at Object.execute (/app/node_modules/graphql/execution/execute.js:64:63)
    at Object.generateSchemaHash (/app/node_modules/apollo-server-core/dist/utils/schemaHash.js:14:32)
    at ApolloServer.generateSchemaDerivedData (/app/node_modules/apollo-server-core/dist/ApolloServer.js:282:41)
    at ApolloServerBase.schemaDerivedData._schema.then.schema (/app/node_modules/apollo-server-core/dist/ApolloServer.js:206:66)
    at process._tickCallback (internal/process/next_tick.js:68:7)
(node:368) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:368) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

我是否以错误的方式使用 GraphQLGatewayModule?

谢谢!

【问题讨论】:

【参考方案1】:

解决了!

我使用的是旧版本的 @nestjs/graphql,它没有 GraphQLFederationModule 模块。我试图将 USER api 与 GraphQLModule 模块一起使用

【讨论】:

以上是关于Nestjs - @nestjs/graphql GraphQLGatewayModule 总是返回错误请求的主要内容,如果未能解决你的问题,请参考以下文章

Nestjs Graphql

更新nestjs/graphql后没有通过标头

从 NestJS + GraphQL 更改 ValidationPipe 错误

NestJs GraphQL 游乐场访问

错误:使用 Nestjs + Graphql + Typeorm 时无法确定 GraphQL 输入类型

如何将自定义标量导入 NestJs 中的 .graphql 文件?