0

我没有找到太多关于在 express.js 中集成 postgraphile 的文档,如果有人详细了解如何集成这些技术,我将不胜感激。非常感谢文档只给了我这个:

如果有人给我一个指南的例子,我将不胜感激。

const express = require("express");
const { postgraphile } = require("postgraphile");

const app = express();

app.use(postgraphile(process.env.DATABASE_URL || "postgres:///"));

app.listen(process.env.PORT || 3000);

我希望输出是基本代码或在 express.js 中实现 postgraphile 的简单示例

4

1 回答 1

0

这是一个很好的解释: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`
  );
});

于 2022-01-03T22:02:04.917 回答