Skip to Content

PostgreSQL DataSource

The PostgreSQL DataSource allows you to turn any PostgreSQL protocol compatible Database into a secure production-grade GraphQL API.

Simply point WunderGraph to your Database and we take care of all the heavy lifting.

In contrast to other offerings, the resulting API is production-ready from the start with authentication & authorization out of the box.

No data gets exposed if you didn't explicitly allowed it.

Market Positioning#

With a PostgreSQL DataSource, WunderGraph positions itself as an alternative to Prisma and Hasura. We'd like to outline which alternative makes the most sense in each scenario.

You'll see that there's no one perfect solution but rather different options for different needs.

vs Hasura#

Hasura turns a PostgreSQL Database into a GraphQL API. If you want to bring this API intro production, you'd have to do some extra steps like adding a service for authentication. Services start to emerge to fill the gaps.

The upside of Hasura is that you get a plain GraphQL API which is compatible with any GraphQL Client.

WunderGraph one the other hand discourages to expose the whole generated GraphQL API. Although it's possible to expose a GraphQL API with WunderGraph (e.g. during development) we consider this insecure in production. Instead, WunderGraph compiles all Queries during the deployment and generates a typesafe RPC client. This reduces the attack surface and makes WunderGraph extremely performant. On the other hand, this means you're no longer able to use other GraphQL clients.

Keep in mind that because Hasura gives you a valid GraphQL API, WunderGraph is able to consume this API and wrap it with its layer of security. This way, you're able to leverage the full power of both Hasura and WunderGraph at the same time.

vs Prisma#

Prisma generates a ORM on top of your PostgreSQL Database. This gives you full flexibility as you can build custom backend services on top of this ORM. While giving full flexibility, this also comes at a cost. You have to implement, maintain, test, deploy and monitor your custom backend.

In cases where you have requirements to build a lot of custom business logic, it makes a lot of sense to use an ORM like prisma.

If what you need is access to a subset of the data in your DataBase through a secure API layer, WunderGraph gives you very good speed of development

Highlights#

Injecting Claims#

WunderGraph, by default, does NOT expose the generated (virtual) GraphQL API. To expose some functionality of the Graph, you have to write a Query or Mutation. The Query will be compiled into a secure RPC Endpoint.

With that in mind, it should be clear that clients are not able to modify Queries at runtime. All they can do is invoke the compiled RPC Endpoint.

Keeping Queries on the backend gives WunderGraph superpowers. If you look at the following Query, you'll see a custom @fromClaim directive.

mutation AddMessage (
$email: String! @fromClaim(name: EMAIL)
$name: String! @fromClaim(name: NAME)
$message: String!
) {
createOnemessages(data: {message: $message users: {connectOrCreate: {create: {name: $name email: $email} where: {email: $email}}}}){
id
message
}
}

This directive does two things. First, it enforces that a user must be authenticated to be able to use this method. Second, we take the email and name Claims of the user and inject them into the Mutation.

Claims are name value pairs of information about the authenticated user.

This method allows us to add a layer of authentication & authorization by just writing a directive.

Live Queries#

WunderGraph allows you to turn any GraphQL Query into a Live-Query. Live-Queries are regular Queries that automatically update the user interface. Data gets updated by polling the upstream DataSource continually using a predefined interval which you can configure.

Combined with a PostgreSQL Database, you're able to easily build Realtime Applications like a Chat.

Examples#

Realtime Chat With NextJS & TypeScript

Reference#

Follow this Link to find more information on how to use the PostgreSQL DataSource.


Product

Subscribe to our newsletter!

Stay informed when great things happen! Get the latest news about APIs, GraphQL and more straight into your mailbox.

© 2021 WunderGraph