Skip to content

Instantly share code, notes, and snippets.

@benjie
Created October 26, 2021 09:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save benjie/518fbe7bbaaa96dbeae173976a28e442 to your computer and use it in GitHub Desktop.
Save benjie/518fbe7bbaaa96dbeae173976a28e442 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
set -e
# What DB are we messing with? (THIS DB WILL BE OVERWRITTEN!)
DATABASE="timethor"
SCHEMA="main"
# Populate database
dropdb --if-exists "$DATABASE"
createdb "$DATABASE"
psql -X1v ON_ERROR_STOP=1 "$DATABASE" <<HERE
create schema $SCHEMA;
CREATE TABLE main.user(id int PRIMARY KEY, foo text);
CREATE TABLE main.product(id int primary key, bar text);
CREATE TYPE main.table_history_audit_entry AS ( audit_by int, baz text);
COMMENT ON TYPE main.table_history_audit_entry IS E'@foreignKey (audit_by) references main.user (id)';
CREATE OR REPLACE FUNCTION main."product_auditFull"(t main.product) RETURNS SETOF main.table_history_audit_entry AS $\$ select (1, 'hi')::main.table_history_audit_entry $\$ LANGUAGE sql STABLE;
HERE
# Install deps
yarn add --dev postgraphile @graphile-contrib/pg-simplify-inflector
# Run PostGraphile
yarn postgraphile \
--subscriptions \
--watch \
--dynamic-json \
--no-setof-functions-contain-nulls \
--no-ignore-rbac \
--show-error-stack=json \
--extended-errors hint,detail,errcode \
--append-plugins @graphile-contrib/pg-simplify-inflector \
--graphiql "/" \
--enhance-graphiql \
--allow-explain \
--enable-query-batching \
--legacy-relations omit \
--connection "$DATABASE" \
--schema "$SCHEMA"
@benjie
Copy link
Author

benjie commented Oct 26, 2021

With the above DB schema, the below query is valid:

{
  products {
    nodes {
      id
      bar
      auditFull {
        nodes {
          auditBy
          baz
          userByAuditBy {
            id
            foo
          }
        }
      }
    }
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment