Show HN: Firebase2graphql – Migrate from Firebase to GraphQL on Postgres
github.com
github.com
We use Hasura[1] to generate the realtime GraphQL API on Postgres automatically. The CLI tool's work then mostly boils down to 2 things, which we do in phases:
1. Phase I: We migrate firebase data nodes to Postgres tables in a fairly naive way. We setup automatic IDs and parent-child tables and set up relationships between the tables. This gives you a write-many type GraphQL API on postgres. Realtime GraphQL with subscriptions and live-queries replaces firebase realtime
2. Phase II: As an optional next step, we try to normalise data by detecting overlapping data between tables. We delete "subset" tables and set up relationships to the superset table.
I've written up a blogpost with more details here: https://blog.hasura.io/firebase2graphql-moving-from-firebase...
[1] https://github.com/hasura/graphql-engine , https://hasura.io
E.g. If a new item is added to the list, will it appear in the list in real-time? Also, if an item's property is changed so that its position in the list changes, will this update the list in real-time as well?
If so, how does it prevent over-fetching?
> ... prevent over-fetching
This is handled server side. Subscriptions are more like 'live queries'. The server sends the data to the client only when the result set for the given 'live-query' changes.
How does it get notified by PostgreSQL when the result of a query changes, since AFAIK PostgreSQL has no such built-in functionality? (it works if other apps change the database, right?)
[0]: https://hasura.io/
I dug into the code a bit and think it all happens here: https://github.com/hasura/graphql-engine/blob/master/server/... Which if my fledgling Haskell is correct digs into the subscription to find the sources and then adds a trigger on them.
On a related note, I've been very impressed with the Hasura GraphQL server. In particular how the authentication works with a JWT token and it's access restrictions seem pretty well thought out and flexible.
I have not yet used it in production but :fingers-crossed: it performs as well there as it has in testing and development
Postgres actually has basic pubsub via "LISTEN" and "NOTIFY". I was planning on trying out rethinkdb as a real-time database but found that approach instead described in this blog post:
http://blog.sagemath.com/2017/02/09/rethinkdb-vs-postgres.ht...
which also had a huge comment thread with related links(800+ upvotes):
We took inspiration from Postgres's RLS and basically implemented it at the API layer so that the permissions/access control system is driven by your application auth and not your database auth.
This allows Hasura to give you role-based column/row level security integrated with any authentication provider or even a JWT.
More at the docs here: https://docs.hasura.io/1.0/graphql/manual/auth/index.html