是否有任何选项在阿波罗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 Server 3中,Playground是默认禁用的。事实上,他们有自己的"游乐场"。因为游乐场已经退役了。取而代之的是"登陆页"。
所以回答你的问题,在生产中关闭它已经不再是一件真正的事情了。
为了回答我认为是您的下一个问题,下面是将Playground恢复为"非生产"状态的代码:
import { ApolloServerPluginLandingPageGraphQLPlayground,
ApolloServerPluginLandingPageDisabled } from 'apollo-server-core';
new ApolloServer({
plugins: [
process.env.NODE_ENV === 'production'
? ApolloServerPluginLandingPageDisabled()
: ApolloServerPluginLandingPageGraphQLPlayground(),
],
});