graphql 游乐场在 Heroku 上运行时表现奇怪

And*_*dez 2 heroku apollo-server

所以我有一个使用 apollo-server-express 运行的 apollo-server:

const PORT = process.env.PORT || 8000
const graphqlPath = process.env.GRAPHQL || 'graphql'
const app = express()

app.use(cors())
app.use(bodyParser.text({ type: 'application/graphql' }))

const gqlServer = new ApolloServer({
  typeDefs: schema,
  resolvers,
  context: {
    me: users[1]
  },
  debug: true,
  tracing: true
})

gqlServer.applyMiddleware({ app, path: `/${graphqlPath}` })

app.listen(PORT, () => console.log(`graphql listening on port ${PORT}`))

module.exports = app
Run Code Online (Sandbox Code Playgroud)

它在本地主机上完美运行,但是将其部署到heroku,当我运行操场时,它变得很奇怪:

  • 它说无法访问服务器
  • 我无法获取架构
  • 查询运行(有时),在我看到结果后,它立即更改为该 json 错误对象
  • 当我复制curl请求并在终端上运行它时,我总是得到结果,没有任何问题

在此输入图像描述

以下是控制台错误的示例:

POST <graphql_server_url> 400(错误请求)
错误:响应不成功:收到状态代码 400

小智 6

尝试将内省和游乐场添加到 ApolloServer 配置中并设置为 true。当 Node 环境设置为生产环境时,它们会自动关闭,但这样您就可以在产品中启用它。参考: https: //www.apollographql.com/docs/apollo-server/v2/testing/graphql-playground/#enabling-graphql-playground-in-product

const gqlServer = new ApolloServer({
  typeDefs: schema,
  resolvers,
  context: {
    me: users[1]
  },
  debug: true,
  tracing: true,
  introspection: true,
  playground: true
})
Run Code Online (Sandbox Code Playgroud)