【问题标题】:NestJs GraphQL playground accessNestJs GraphQL 游乐场访问
【发布时间】:2021-04-03 01:53:54
【问题描述】:

我似乎无法使用 NestJS 访问 GraphQL Playground。我正在浏览文档,并按照https://docs.nestjs.com/graphql/quick-start 一直到解析器部分来生成schema.gql,但尝试到达localhost:3000/graphql 时无法连接。

起初我认为我的代码设置不正确,但我花了一些时间研究 Nest 的示例,发现在尝试访问 /graphql 端点时这些示例也不起作用。如果我设置 get 端点以使用 REST 方法返回 JSON 正文,它确实有效。

import { Module } from '@nestjs/common';
import { GraphQLModule } from '@nestjs/graphql';
import { RecipesModule } from './recipes/recipes.module';

@Module({
  imports: [
    RecipesModule,
    GraphQLModule.forRoot({
      installSubscriptionHandlers: true,
      autoSchemaFile: 'schema.gql',
    }),
  ],
})
export class AppModule {}

这直接来自 NestJS 示例。我的理解是 GraphQLModule 应该设置与/graphql 端点的连接。按照文档,graphql, apollo-server-express, and graphql-tools 全部安装完毕。

知道为什么 graphql 路由没有连接吗?

[编辑]: 到目前为止我尝试过的事情:

  • 使用 GraphQLModule.forRoot 显式设置 playground: true
  • 已验证 NODE_ENV 不是“生产”
  • 确认服务器在使用 REST 创建解析器时工作正常
  • curl'd localhost:3000/graphql 并收到 graphql 验证错误,因此请确认连接正确

【问题讨论】:

  • 检查您的process.env.NODE_ENV 是什么。如果是PRODUCTION,那么我认为apollo-server 会禁用操场。如果没有,请尝试显式添加playground: true
  • @JayMcDoniel 我应该在帖子中指定我已经尝试过的内容,抱歉。我都试过了,不幸的是,无法连接到端点。我只是卷曲端点以查看它是否连接到 graphql,并且似乎是因为它返回 GraphQL 验证错误。
  • 那你们能提供复制品吗?从您分享的内容中看不出为什么会发生这种情况
  • 所以,我在他们的 github repo (github.com/nestjs/nest/tree/master/sample/23-graphql-code-first) 中尝试了 Nest 的示例。它尽可能基本,并且与文档的示例基本相同。没有修改,只是无法连接到 gql 游乐场。同样的结果卷曲,能够看到gql验证错误,所以它在服务器上连接。
  • 对于该示例,curl 失败,但浏览器对同一位置的请求成功。可能正在查看用户代理。将交互式游乐场发送到命令行没有多大意义

标签: node.js graphql nestjs


【解决方案1】:

似乎是在 Windows 10 上运行的 WSL2 的问题。github 上的这个线程对这个问题有一些见解。

禁用快速启动以允许访问本地主机。

https://github.com/microsoft/WSL/issues/5298

【讨论】:

    【解决方案2】:

    我遇到了类似的问题,这就是我所做的。我希望你会发现它有帮助。感谢大家的支持。

    第一步: // app.module.ts

      imports: [
        UsersModule,
        GraphQLModule.forRoot({
          // autoSchemaFile: true,    did not work!
          autoSchemaFile: join(process.cwd(), 'src/schema.gql'),
          // schema.gql will automatically be created
          debug: true,
          playground: true,
        }),
      ],
      providers: [AppResolver],   // all resolvers & service should be in providers
    

    第 2 步:

    确保您至少有一个查询 bcz Mutation is not enough 如果您不这样做 GraphQL Playground 将无法启动。阅读更多here

    【讨论】:

      【解决方案3】:

      有时helmet 会导致同样的问题。如果您将头盔作为中间件加载,也可能会导致这种情况。

      【讨论】:

        猜你喜欢
        • 2021-05-13
        • 2020-01-08
        • 2020-05-29
        • 1970-01-01
        • 1970-01-01
        • 2021-09-07
        • 2019-12-03
        • 2015-12-20
        • 1970-01-01
        相关资源
        最近更新 更多