如何在无服务器生产环境中禁用GraphQL Playground?

3

有没有办法在生产环境中禁用playground功能?

在我的serverless.yml文件中

functions:
  graphql:
    # this is formatted as <FILENAME>.<HANDLER>
    handler: handler.graphqlHandler
    events:
    - http:
        path: api/v1
        method: post

  playground:
    handler: handler.playgroundHandler
    events:
    - http:
        path: playground
        method: get

在我的handler.js文件中。
import { ApolloServer, gql } from "apollo-server-lambda";
import lambdaPlayground from "graphql-playground-middleware-lambda";

// Construct a schema, using GraphQL schema language
const typeDefs = gql`
  type Query {
    hello: String
  }
`;

// Provide resolver functions for your schema fields
const resolvers = {
  Query: {
    hello: () => "Hello world!"
  }
};

const server = new ApolloServer({ typeDefs, resolvers });

export const graphqlHandler = server.createHandler({
  cors: {
    origin: "*",
    credentials: true
  }
});
export const playgroundHandler = lambdaPlayground({
  endpoint: "/api/v1"
});

1
Apollo Server 2 自带一个“在生产环境中禁用”的 playground(即 process.env.NODE_ENV === 'production')。或者,它允许您专门为 playground 设置布尔值,并将其连接到环境变量。因此,也许更新可能是值得的? - Herku
1个回答

0

您需要将 NODE_ENV 设置为 production 或设置 playgroundAlways:false

const server = new ApolloServer({
  schema, 
  introspection: false,
  playground: false,
});

在开发中,Apollo Server 可以在与 GraphQL 服务器本身相同的 URL 上启用 GraphQL Playground(例如 http://localhost:4000/graphql),并自动将 GUI 提供给 Web 浏览器。当 NODE_ENV 设置为 production 时,GraphQL Playground(以及内省)会被禁用,这是一种生产最佳实践。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接