首页 > 解决方案 > 无法使用 postgraphile 连接数据库

问题描述

我有一个 pgadmin 名称为“mydatabase”的数据库,表名是“mytable”。它运行在http://127.0.0.1:33859/browser/. 我试图使用 postgraphile 来连接它。这是代码

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

const app = express();

app.use(postgraphile("postgres://postgres:postgres@localhost:33859/mydatabase -s public"));

app.listen(3000);

我的超级用户用户名是“postgres”,密码是“postgres”。该数据库由名为“test”的用户拥有,密码为“1234”。用node运行脚本后,终端没有报错。当我点击端口 3000 时,它显示Cannot GET /. 对此有什么帮助吗?

标签: postgresqlgraphqlpostgraphile

解决方案


因为 PostGraphile 中间件预计将与您的 Node.js 应用程序的其余部分一起安装,所以它不会接管根 URL,而是/graphql默认情况下使 GraphQL 端点可用。要更改这一点,请在 Library Usage 页面中记录graphqlRoutegraphiqlRoute选项。

这里有一些不错的选择可以帮助您入门:

const isDev = process.env.NODE_ENV !== "production" && process.env.NODE_ENV !== "staging";    
const DB = "postgres://postgres:postgres@localhost:33859/mydatabase -s public";
const SCHEMA = "public";

app.use(postgraphile(DB, SCHEMA, {
  // Enable the GraphiQL IDE in development
  graphiql: isDev,
  // Add some enhancements (headers, formatting, etc)
  enhanceGraphiql: isDev,

  // Makes GraphiQL available at http://localhost:3000/ rather than /graphiql
  graphiqlRoute: '/',

  // Watch DB for changes
  watchPg: isDev,

  // Use JSON objects rather than strings
  dynamicJson: true,

  // Debugging
  showErrorStack: isDev,
  extendedErrors:
    isDev
      ? [
          "errcode",
          "severity",
          "detail",
          "hint",
          "positon",
          "internalPosition",
          "internalQuery",
          "where",
          "schema",
          "table",
          "column",
          "dataType",
          "constraint",
          "file",
          "line",
          "routine",
        ]
      : ["errcode"],

    // For use with e.g. apollo-link-batch-http
    enableQueryBatching: true,

}));

您可能会发现bootstrap-react-apollo installPostGraphile.js文件是开始的好地方。


推荐阅读