这是一个很好的解释:https ://www.graphile.org/postgraphile/usage-library/
这是您可以开始的示例:
const express = require("express");
const { postgraphile } = require("postgraphile");
const morgan = require("morgan");
const port = process.env.PORT || 5000;
const production = process.env.NODE_ENV === "production";
const app = express();
app.use(morgan("combined"));
const postgraphileOptionsDev = {
subscriptions: true,
watchPg: true,
dynamicJson: true,
setofFunctionsContainNulls: false,
ignoreRBAC: false,
showErrorStack: "json",
extendedErrors: ["hint", "detail", "errcode"],
appendPlugins: [require("@graphile-contrib/pg-simplify-inflector")],
exportGqlSchemaPath: "schema.graphql",
graphiql: true,
enhanceGraphiql: true,
allowExplain(req) {
// TODO: customise condition!
return true;
},
enableQueryBatching: true,
legacyRelations: "omit",
pgSettings(req) {
/* TODO */
},
};
const postgraphileOptionsProd = {
subscriptions: true,
retryOnInitFail: true,
dynamicJson: true,
setofFunctionsContainNulls: false,
ignoreRBAC: false,
extendedErrors: ["errcode"],
appendPlugins: [require("@graphile-contrib/pg-simplify-inflector")],
graphiql: false,
enableQueryBatching: true,
disableQueryLog: true,
legacyRelations: "omit",
pgSettings(req) {
/* TODO */
},
};
app.use(
postgraphile(
process.env.DATABASE_URL,
"app_public",
production ? postgraphileOptionsProd : postgraphileOptionsDev
)
);
app.listen(port, "0.0.0.0", () => {
console.log(`${production ? " PRODUCTION" : " DEV"} server`);
console.log(`GraphQL endpoint: http://localhost:${port}/graphql`);
console.log(
`GraphiQL (GraphQL IDE) endpoint: http://localhost:${port}/graphiql`
);
});