跳转到主要内容

什么是PostGraphile?

如果您熟悉Spring Data JPA,那么理解PostGraphile将非常容易。但没关系。让我们来看看。PostgreSQL数据库是一个非常流行的高性能应用数据库。ProstGraphile与PostgreSQL数据库和GraphQL配合使用。

它在几秒钟内从PostgreSQL模式提供了即时和高性能的GraphQLAPI,并提供了更多功能。

PostGraphile的特点

  • 令人惊叹的展示
  • 数据库关系自动发现,CRUD自动突变

例如:vehicleById、createVehicle、updateVehicle、deleteVehicle等。

  • 启用查询功能,如排序、筛选、分页等。
  • 非常容易启用PostGraphile并使用。
  • 模式文档将通过CLI生成
  • 您可以通过客户端应用程序直接调用PostGraphile端点。

没有中间件服务的PostGraphile

您可能认为我们可以在没有服务的情况下使用PostGraphile。因为PostGraphile提供了GraphQLAPI,所以客户端可以直接调用这些API。是的,这是可能的,但如果我们需要一些定制,或者如果我们需要从任何其他web服务(例如:REST)请求数据,我们需要有一个中间件服务,如NestJS、Spring Boot等。它会是这样的。

Install PostGraphile

Hit this command to install PostGraphile globally.

npm install -g postgraphile

PostGraphile CLI interface

My TestDB2 database has only one table called Vehicle. This is my DB structure.

然后,我运行以下命令以获得PostGraphile CLI界面。你可以从Postgaohile文档中找到这一点。

npx postgraphile -c ‘postgres://postgres:1234@localhost/TestDB2’ — watch — enhance-graphiql — dynamic-json

正如您所看到的,我的数据库是TestDB2,用户名是postgres,密码是1234。一旦我在终端中点击了这个命令,它将给出下面提到的一些细节。还有一些其他CLI选项。您可以在官方文档中找到更多详细信息。

If you can go to the http://localhost:5000/graphiql endpoint, you will be able to the PostGraphile CLI interface.

在Explore中,您可以看到Query,也可以创建Mutations。这与GraphQL游乐场非常相似。所有查询和突变都是自动生成的。因此,您无需担心,只需点击查询或突变,然后点击播放按钮即可执行查询。

请查看以下视频以了解使用PostGraphile的完整流程。

https://youtu.be/KCsQNAV1_to