在生产模式下是否有禁用游乐场的选项?

Is there any option to disable playground when in production mode?

apollo v3 中是否有任何选项可以在生产模式下禁用 playground? 在 v2 中,您可以通过将 playground: false 传递给 ApolloServer 选项来禁用它,但在 v3 中找不到任何选项。

我正在使用 apollo-server-fastify。

编辑:我已禁用游乐场,但我仍然可以访问 localhost:4000/graphql 并收到此消息:

GET query missing.

我的代码:

const app = fastify({ trustProxy: true });
const server = new ApolloServer({
  schema,
  context: ({ request }: { request: FastifyRequest }) => {
    const auth = request.headers.authorization || "";
    return {
      auth,
      session: request.session,
      sessionStore: request.sessionStore,
    };
  },
  plugins: [
    myPlugin,
    env === "production"
      ? ApolloServerPluginLandingPageDisabled()
      : ApolloServerPluginLandingPageGraphQLPlayground(),
  ],
});
const corsOptions = {
  origin: true,
  optionsSuccessStatus: 200,
  credentials: true,
};
app.register(fastifyCookie);
app.register(fastifySession, {
  secret:
    "secreet",
  saveUninitialized: false,
  cookieName: "GraphSSid",
  cookie: {
    maxAge: 60 * 60 * 24 * 1000 * 7,
    secure: false,
    sameSite: true,
  },
});
existsSync(path.join(__dirname, "../files")) ||
  mkdirSync(path.join(__dirname, "../files"));
existsSync(path.join(__dirname, "../templates")) ||
  mkdirSync(path.join(__dirname, "../templates"));
existsSync(path.join(__dirname, "../logs")) ||
  mkdirSync(path.join(__dirname, "../logs"));

app.register(fastifyStatic, {
  root: path.join(__dirname, "../files"),
  prefix: "/files",
});
app.register(fastifyStatic, {
  root: path.join(__dirname, "../templates"),
  prefix: "/templates",
  decorateReply: false, // the reply decorator has been added by the first plugin registration
});
app.addContentTypeParser("multipart", (request: any, payload, done) => {
  request.isMultipart = true;
  done(null);
});
app.addHook("preValidation", async function (request: any, reply) {
  if (!request.isMultipart) {
    return;
  }

  request.body = await processRequest(request.raw, reply.raw);
});
app.get("/api/update-product-available-inv", async (req, res) => {
  try {
    await saveAvailable();
    console.log("success");
  } catch (err) {
    console.log(err);
  }
  return "Success";
}); 
if (env === "production") {
  app.register(async (instance, opts, next) => {
    instance.register(fastifyStatic, {
      root: path.join(__dirname, "build"),
      prefix: "/",
    });
    instance.setNotFoundHandler((req, res) => {
      res.sendFile("index.html", path.join(__dirname, "build"));
    });
    next();
  });
}
server.start().then(() => {
  app.register(server.createHandler({ path: "/graphql", cors: corsOptions }));
  app.listen(PORT, "0.0.0.0", (err) => {
    if (err) {
      console.error(err);
    } else {
      console.log("Server is ready at port 4000");
    }
  });
});

在 Apollo Server 3 中,Playground 默认处于禁用状态。事实上,他们推出了自己的“游乐场”,因为 Playground has been retired。它有一个“着陆页”。

因此,为了回答您的问题,在生产环境中关闭它已不再是一件真正的事情。

为了回答我假设的下一个问题,这里是将 Playground 带回“非生产”的代码:

import { ApolloServerPluginLandingPageGraphQLPlayground,
         ApolloServerPluginLandingPageDisabled } from 'apollo-server-core';
new ApolloServer({
  plugins: [
    process.env.NODE_ENV === 'production'
      ? ApolloServerPluginLandingPageDisabled()
      : ApolloServerPluginLandingPageGraphQLPlayground(),
  ],
});