尝试打开 UI 时出现 Postgraphile 错误

时间:2021-03-31 14:38:43

标签: graphql nestjs postgraphile

当我尝试打开 postgraphile UI 时,它不起作用。我收到以下错误。

<块引用>

构建初始架构时发生严重错误。退出 因为 retryOnInitFail 未设置。

错误:连接意外终止
在连接。 (D:\cars-assignment\node_modules\pg\lib\client.js:132:73)
在 Object.onceWrapper (events.js:421:28)
在 Connection.emit (events.js:315:20)
在套接字。 (D:\cars-assignment\node_modules\pg\lib\connection.js:108:12)
在 Socket.emit (events.js:327:22)
在 endReadableNT (_stream_readable.js:1201:12)
在 processTicksAndRejections (internal/process/task_queues.js:84:21)

我正在使用以下命令 npx postgraphile -c postgres://username:SECRET@localhost:5000/db

如何解决这个问题?

2 个答案:

答案 0 :(得分:1)

我认为错误输出比@deluxan 显示的要多。至少对我来说是这样。我认为问题在于 postgraphile 忽略了连接字符串中指定的端口。首先,我在 5555 端口上公开的 Docker 容器中运行 PostgreSQL。

export class ListClientComponent implements OnInit {

  source: LocalDataSource;
  pageSize = 30;
  currentPage = 1;
  showPerPage = 10;
  totalCount;
  constructor(private service: ClientService,private router: Router) {
    this.initData();
  }

  public settings = {
    mode: 'external', 
    pager:{
      display: true,
      perPage: this.showPerPage,
    },
    actions: {
      add:false,
      edit:true,
      delete:false
    },
        columns: {
      clientId: {
        title: 'CI',
      },
      name: {
        title: 'Name'
      },
      instagramName: {
        title: 'Instagram'
      },
      type: {
        title: 'Type'
      },
      phone: {
        title: 'Phone'
      },
      gender: {
        title: 'Gender'
      },
      age: {
        title: 'Age'
      },
      city: {
        title: 'City'
      },
      email: {
        title: 'Email'
      }
    },
  };

  ngOnInit() {
    this.initOnChagedData();
  }

  onEdit(event){
    console.log(event.data.clientId);
    this.router.navigate(['/clients/update-client/'+ event.data.clientId]) ;  
  }

  initData(){
    this.source = new LocalDataSource();
    this.service.getClients(this.currentPage, this.pageSize).subscribe( (result: HttpResponse<any>) => {
      if(!result){
        return;
      }
      this.source.load(result.body);
      this.totalCount = JSON.parse(result.headers.get("X-Pagination"));
      this.totalCount = this.totalCount["totalCount"];
      console.log(this.source.count());
    }
    )
  }

  initOnChagedData(){
    this.source.onChanged().subscribe((change) => {
      if (change.action === 'page') {
        this.pageChange(change.paging.page);
      }
    });
  }

  pageChange(pageIndex) {
    var getNew = pageIndex * this.showPerPage;
    if( getNew >= this.source.count() && getNew < this.totalCount){
      this.currentPage = this.currentPage + 1;
      this.service.getClients(this.currentPage, this.pageSize).subscribe( result => {
        if(!result){
          return;
        }
        result.body.forEach(element => {
          this.source.add(element);
        });
      })
    }
  }
}

然后当我尝试通过 Docker 运行 Postgraphile 时...

17502bafdf52        exspda_postgis                   "docker-entrypoint.s…"   2 months ago        Up 5 minutes        0.0.0.0:5555->5432/tcp          exspda_postgis

...完整的输出是:

$ docker run --init -p 5000:5000 graphile/postgraphile --connection postgres://hippo:mypassword@localhost:5555/first_geo --schema public --watch

输出 PostGraphile v4.12.1 server listening on port 5000 ? ‣ GraphQL API: http://0.0.0.0:5000/graphql ‣ GraphiQL GUI/IDE: http://0.0.0.0:5000/graphiql (RECOMMENDATION: add '--enhance-graphiql') ‣ Postgres connection: postgres://hippo:[SECRET]@localhost/first_geo (watching) ‣ Postgres schema(s): public ‣ Documentation: https://graphile.org/postgraphile/introduction/ ‣ Node.js version: v12.22.1 on linux x64 ‣ Join Storyscript in supporting PostGraphile development: https://graphile.org/sponsor/ * * * A serious error occurred when building the initial schema. Exiting because `retryOnInitFail` is not set. Error details: Error: connect ECONNREFUSED 127.0.0.1:5432 at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1144:16) 不包括我在连接字符串中指定的端口。实际错误似乎也表明未使用我指定的端口:Postgres connection: postgres://hippo:[SECRET]@localhost/first_geo。当我指定 5555 时,它试图使用端口 5432。我怀疑这与 @deluxan 经历的事情相同。端口 5432 是在 postgraphile 中硬编码的吗?如何修复识别用户提供的连接字符串中指定的端口?

请注意,我是 Postgraphile 的新手,因此对于愚蠢的(即对其他人显而易见的)疏忽和错误,我深表歉意!

答案 1 :(得分:0)

这是 Node 和 Postgres 之间的连接错误。尝试直接连接pg模块;它应该以同样的方式失败,您可以从那里继续调试:

const { Pool } = require('pg');

const pool = new Pool({
  connectionString: "postgres://username:SECRET@localhost:5000/db"
});

async function main() {
  await pool.query('select 1');
  pool.end();
  console.log('All good.');
}

main().catch(e => {
  console.error(e);
  process.exit(1);
});

我怀疑 :5000 是错误的,因为这是 PostGraphile 默认尝试侦听的端口,而 postgres 默认使用 :5432。