gpt4 book ai didi

graphql - 如何在无服务器生产中禁用 GraphQL Playground?

转载 作者:行者123 更新时间:2023-12-05 07:30:28 26 4
gpt4 key购买 nike

有没有办法在生产中禁用 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"
});

最佳答案

您需要设置您的 NODE_ENV=production或者设置 playgroundAlways:false

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

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

关于graphql - 如何在无服务器生产中禁用 GraphQL Playground?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52250522/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com