无法将数据库与postgraphile连接

时间:2019-01-11 10:00:14

标签: postgresql graphql postgraphile

我在pgadmin中有一个数据库,名称为“ mydatabase”,表名为“ mytable”。它在http://127.0.0.1:33859/browser/中运行。我正在尝试使用后石墨与之联系。这是代码

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 /。有什么帮助吗?

1 个答案:

答案 0 :(得分:1)

由于预计PostGraphile中间件将与您的Node.js应用程序的其余部分一起安装,因此它不会接管根URL,而是默认在/graphql处提供GraphQL端点。要更改此设置,可以使用graphqlRoutegraphiqlRoute选项documented in the Library Usage page

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

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文件是入门的好地方。