Hacker News new | ask | show | jobs
by lldata 3082 days ago
Cool project. But potentially dangerous if used as a CRUD ORM layer.

I'd only recommend using something like this for the read model of a CQRS/Event Sourced system.

Capture the commands and send them to business logic, that then updates the database. If you let a generic framework handle writes, it is like putting the business logic in the client. In that case SQL would be simpler.

2 comments

You can manage that with mutations which correspond with PostgreSQL functions to do the writing in the Postgraphile system.

    create or replace function addFrobnoz (fooId uuid, frobnoz uuid)
    returns void as $$
    declare
        foo row;
    begin
        -- fetch and validate foo wrt. frobnoz
        select insertIntoEventStore(
               "frobnozAdded",
               json_build_object('frobnoz', frobnoz));
    end;
    $$ language plpgsql;
Where `insertIntoEventStore` does what it says, maintains a monotonic sequence, etc, etc. Postgraphile can discover the _command_ functions as mutations and map them the GraphQL mutations spec for you. Then your projections just fill in the read tables for you and that's what Postgraphile will read from.

And if you need side-effects like sending email or stream more complicated aggregates there are lots of options in PostgreSQL to use the `notify/subscribe` system or use extensions to publish off to a message queue directly.

PostGraphile is super cool. Especially if you like centralising logic at the database level. There are pros and cons to this approach.

Personally I prefer keeping my database layer as simple as possible - no stored procedures, no queues and certainly no communicating to external systems.

But I still see great value in generating a GraphQL data API and using that as the only way to interact with the database. GraphQL Bindings is a new technology that allows you write a server that acts as an advanced proxy in front of another GraphQL API. It allows you to recompose the schema, and intercept requests for specific queries or mutations.

Using an approach like this you can pass through queries to PostGraphile almost unmodified while taking full control over mutations.

It's early days, but we have a pretty cool setup that generates TypeScript type definitions based on the underlying GraphQL API. And it is all open source :-) https://blog.graph.cool/reusing-composing-graphql-apis-with-...