简体   繁体   English

NestJs GraphQL 游乐场访问

[英]NestJs GraphQL playground access

I can't seem to access the GraphQL Playground using NestJS.我似乎无法使用 NestJS 访问 GraphQL Playground。 I'm exploring the documentation and have followed this https://docs.nestjs.com/graphql/quick-start up to the Resolvers section to generate the schema.gql , but attempting to reach localhost:3000/graphql is not able to connect.我正在浏览文档并遵循此https://docs.nestjs.com/graphql/quick-start到 Resolvers 部分以生成schema.gql ,但尝试访问localhost:3000/graphql无法连接。

At first I thought my code was setup incorrectly, but I spent some time digging into Nest's examples and found that those also do not work when trying to access the /graphql endpoint.起初我以为我的代码设置不正确,但我花了一些时间深入研究 Nest 的示例,发现在尝试访问/graphql端点时它们也不起作用。 It does work if I setup a get endpoint to return a JSON body using the REST method.如果我设置一个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 {}

This is directly from the NestJS example.这直接来自 NestJS 示例。 My understanding is that the GraphQLModule should be setting up the connection to the /graphql endpoint.我的理解是 GraphQLModule 应该建立与/graphql端点的连接。 Following the docs, graphql, apollo-server-express, and graphql-tools were all installed.按照文档, graphql, apollo-server-express, and graphql-tools都已安装。

Any idea why the graphql route is not connecting?知道为什么 graphql 路由没有连接吗?

[Edit]: Things I've tried so far: [编辑]:到目前为止我尝试过的事情:

  • setting playground: true explicitly with GraphQLModule.forRoot使用 GraphQLModule.forRoot 显式设置playground: true
  • verified NODE_ENV is not 'production'经过验证的NODE_ENV不是“生产”
  • confirmed server works when creating resolvers using REST使用 REST 创建解析器时确认服务器工作
  • curl'd localhost:3000/graphql and receive a graphql validation error, so confirm that connects correctly localhost:3000/graphql并收到 graphql 验证错误,因此确认连接正确

Seems to be an issue with WSL2 running on Windows 10. This thread on github has some insight into the issue.在 Windows 10 上运行的 WSL2 似乎是一个问题。github 上的这个线程对这个问题有一些了解。

Disable Fast Startup to allow access to localhost.禁用快速启动以允许访问 localhost。

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

I encountered a similar issue this is what I did.我遇到了类似的问题,这就是我所做的。 I hope you will find it helpful.我希望你会发现它有帮助。 I appreciated everyone's support.我感谢大家的支持。

Step1: // app.module.ts Step1: // 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

step 2:第2步:

Make sure you have one at least query bcz Mutation is not enough if you don't GraphQL playground will not start.确保您至少有一个查询 bcz Mutation is not enough 如果您不这样做 GraphQL 游乐场将无法启动。 Read more here在这里阅读更多

I have the same error as you我和你有同样的错误

I removed the helmet from global middleware我从全局中间件中移除了头盔

sometimes helmet causes this same issue.有时helmet会导致同样的问题。 if you have helmet loaded as a middleware, it might probably also cause this.如果您将头盔作为中间件加载,也可能导致此问题。

Removing the dist folder and restarting the app worked for me.删除 dist 文件夹并重新启动应用程序对我有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM