【问题标题】:Is there any option to disable playground when in production mode?在生产模式下是否有任何选项可以禁用游乐场?
【发布时间】:2021-12-05 09:42:59
【问题描述】:

apollo v3 中是否有任何选项可以在生产模式下禁用游乐场? 在 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 apollo-server fastify


    【解决方案1】:

    在 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(),
      ],
    });
    

    【讨论】:

    • 谢谢。但是我仍然可以访问 localhost:4000/graphql 并且无法获取。
    • 如果您收到“无法获取”,则可能是插件设置不正确
    • 我已经更新了问题
    • 我会console.log(env)。如果这是 apollo-server@3,并且您收到的是该消息而不是新的“Apollo 登陆页面”,那么您正在正确注册 ApolloServerPluginLandingPageDisabled 插件。仅基于该代码(以及这些假设),我猜您的 env 设置为 production
    • 我假设 localhost:4000 是你的网站,localhost:4000/graphql 是你得到“GET query missing”的GraphQL API。
    猜你喜欢
    • 2022-01-21
    • 2017-11-12
    • 2020-01-08
    • 2019-01-31
    • 1970-01-01
    • 2020-07-02
    • 1970-01-01
    • 2020-10-14
    • 1970-01-01
    相关资源
    最近更新 更多