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

2023-12-21

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

我正在使用 apollo-server-fastify。

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

GET query missing.

my code:

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 默认是禁用的。事实上,他们开辟了自己的“游乐场”,因为游乐场已退役 https://github.com/graphql/graphql-playground/issues/1143。相反,它有一个“登陆页面”。

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

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

import { ApolloServerPluginLandingPageGraphQLPlayground,
         ApolloServerPluginLandingPageDisabled } from 'apollo-server-core';
new ApolloServer({
  plugins: [
    process.env.NODE_ENV === 'production'
      ? ApolloServerPluginLandingPageDisabled()
      : ApolloServerPluginLandingPageGraphQLPlayground(),
  ],
});
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

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

  • 当游乐场被禁用时,apollo-server 返回缺少 GET 查询

    当我效仿的时候production aka playground is disabled 然后我回到我的根 http localhost 9000 我收到以下回复 400 Bad Request GET query missing 有没有办
  • 在生产模式下是否有禁用游乐场的选项?

    apollo v3 中是否有任何选项可以在生产模式下禁用游乐场 在 v2 中你可以通过传递来禁用它playground false到 ApolloServer 选项 但在 v3 中找不到任何选项 我正在使用 apollo server fa
  • Apollo 服务器解析大数据时性能缓慢

    在解析大数据时 我注意到从解析器将结果返回给客户端的那一刻起 性能非常慢 我假设apollo server迭代我的结果并检查类型 无论哪种方式 操作都花费太长时间 在我的产品中 我必须一次性返回大量数据 因为它被一次性用于在 UI 中绘制图
  • GraphQL:成功突变后如何发出警告?

    让我们想象一下我有一个createPost插入新帖子的突变 在典型的应用程序中 该突变可以 成功 返回一个Post 失败 抛出错误 我使用阿波罗错误 https github com thebigredgeek apollo errors来
  • 是否可以防止在组件状态更改/重新渲染时重新获取“useLazyQuery”查询?

    目前我有一个useLazyQuery按下按钮 搜索表单的一部分 时触发的钩子 钩子行为正常 并且仅在按下按钮时触发 然而 一旦我触发了它一次 它就会在每次组件重新渲染时被触发 通常是由于状态变化 因此 如果我搜索一次 然后编辑搜索字段 结果
  • 为什么我们需要“cacheRedirects”?

    我阅读了文档与缓存数据交互 https www apollographql com docs react caching cache interaction cache redirects with cacheredirects虽然我明白如
  • Apollo Server Express:请求实体太大

    我需要在 GraphQL 突变中发布大量有效负载 如何提高 Apollo Server 的主体大小限制 我在用着apollo server express版本 2 9 3 我的代码 简化 const myGraphQLSchema new
  • Apollo-客户端自签名证书

    ApolloClient 有没有办法接受来自具有自签名证书的服务器的请求 import ApolloClient from apollo boost const client new ApolloClient uri https windo
  • Apollo GraphQL 突变(对象参数)

    所以 我正在尝试设计我的 Apollo 服务器 我想创建一个以对象作为参数的突变 这是我的架构的一个片段 它以某种方式导致了问题 我认为它在语法上是正确的 但我遇到了这个错误 errors message 预期的输入类型 您必须将 INPU
  • 使用 NestJS 和 Fastify 时多部分表单数据正文为空

    我们正在迁移自express to fastify in our nestJS应用 这multipart form data后置控制器中支持的 json 正文而不是文件 在迁移后不起作用 我们有typeorm swagger也已插入 如有任
  • React Apollo GraphQL 搜索/过滤

    我有一个使用 Apollo 客户端的带有 GraphQL 服务器的 React 项目 我试图弄清楚如何根据搜索文本和过滤选择更改查询结果 如果有人可以查看我的代码并给我一些帮助 我将不胜感激 对所有代码感到抱歉 我想也许它们都是相关的 服务
  • 如何使用“apollo-server”加载 .graphql 文件?

    我目前正在使用单独的加载 GraphQL 架构 graphql文件 但它封装在字符串中 schema graphql const schema type CourseType id String name String type Query
  • AWS AppSync:如何通过 DynamoDB 返回有效的 JSON

    我有一个 AppSync GraphQL API 可以对 DynamoDB 进行查询并返回 JSON 字符串 但是在我的响应映射模板中 我使用内置 util parseJson 功能如所列here https docs aws amazon
  • 如何将参数传递给graphql查询?

    我正在尝试在 Meteor blaze 项目中使用 Apollo graphql 我正在使用来自swydo blaze apollo 使用graphql查询从mongoDB获取数据就可以了 Using this one can get da
  • 如何在 Nest js 中使用 fastify-adapter 配置速率限制

    我刚刚开始实现 API 的 Nest js 并且正在使用 Fastify 适配器 我需要帮助在 Nest JS 中使用 FastifyAdapter 配置速率限制 async function bootstrap const app awa
  • React 自定义挂钩内的 Apollo GraphQL 查询

    我正在尝试列出 Rick Morty API 中的所有角色 我编写了以下钩子以在我的组件中使用 该组件将呈现结果 当我对值进行硬编码时 例如 page 1 filter name Rick 查询运行得很好 如果我尝试使用变量 它会返回错误
  • 连接 Apollo 和 mongodb

    我想将我的 Apollo 服务器与我的 mongoDB 连接 我知道那里有很多例子 但我陷入了异步部分 没有找到解决方案或示例 这很奇怪 我完全错了吗 我从 next js 的示例开始https github com zeit next j
  • 如何使用 Apollo 后端在 TypeScript Angular 应用程序中输入部分类型?

    编辑 我正在寻找来自 Graphql Angular 社区的权威和来源答案 以提供最佳实践示例 例如 我们在 TypeScript 中定义了一个 Person 类型 interface Person firstName string las
  • 如何在 Vanilla JS 中使用 Apollo Client 创建 GraphQL 订阅

    最近 Apollo Client 发布了 websocket 订阅功能 但到目前为止我只看到它通过在 componentWillMount 生命周期钩子中使用 subscribeToMore 启动查询来使用 这是一个来自的例子https d
  • Apollo 更新查询未调用?

    我正在 GitHunt React 和 GitHunt API 中研究 Apollo pub sub 当我运行这些应用程序并输入新评论时 评论将通过调用提交来保存 然后 updateQueries 代码块在此处运行 const Commen

随机推荐