Skip to content

Instantly share code, notes, and snippets.

Avatar

Benjie benjie

View GitHub Profile
View README.md

PostGraphile reproduction script

Hello, you've probably been sent here because you've found an issue in your usage of PostGraphile. I (Benjie) deal with a lot of support requests that have insufficient information for me to diagnose the problem, so I'll ask for a minimal reproduction. Making a minimal reproduction allows me to diagnose (and potentially fix!) the issue much more easily, and is also a valuable exercise for you during your debugging - who knows, maybe whilst attempting to create the reproduction you'll figure out what went wrong and fix your own issue?

To use this, please download the script locally, and then populate the DATABASE

@benjie
benjie / swc.js
Last active Oct 26, 2021
A script for running SWC in a monorepo that uses TypeScript project references (`tsc --build` / `tsc -b`) compiling everything in parallel with optional watch mode. For this to work you MUST have `isolatedModules: true`.
View swc.js
const swc = require("@swc/core");
const chokidar = require("chokidar");
const glob = require("glob");
const { promises: fsp } = require("fs");
const { basename, dirname } = require("path");
const { createHash } = require("crypto");
const mkdirp = require("mkdirp");
const WATCH = process.argv[2] === "--watch";
View timethor.sh
#!/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"
View makeSetDefaultOrderByForFieldPlugin.ts
import { Plugin } from 'postgraphile';
/**
* Allows you to override the default orderBy for a given field to be the
* orderBy with the given name. Usage:
*
* ```
* const MyOrderByPlugin = makeSetDefaultOrderByForFieldPlugin(
* 'MyTableName',
* 'myRelationName',
@benjie
benjie / strip_tags_from_introspection.js
Created May 21, 2021
A Graphile Engine plugin to strip all tags/descriptions from introspection results
View strip_tags_from_introspection.js
module.exports = (builder) => {
builder.hook("build", (build) => {
const oldPgAugmentIntrospectionResults =
build.pgAugmentIntrospectionResults;
build.pgAugmentIntrospectionResults = (inIntrospectionResult) => {
let pgIntrospectionResultsByKind = inIntrospectionResult;
if (oldPgAugmentIntrospectionResults) {
pgIntrospectionResultsByKind = oldPgAugmentIntrospectionResults(
pgIntrospectionResultsByKind,
);
View Map-vs-object-perf.js
const symbols = [];
for (let i = 0; i < 1000; ++i) {
symbols[i] = Symbol(`key_${i}`);
//symbols[i] = `key_${i}`;
}
function test1() {
const map = new Map();
for (let i = 0; i < 10000; ++i) {
View SizeOfTable.sql
SELECT
pg_size_pretty(total_bytes) AS total,
pg_size_pretty(index_bytes) AS index,
pg_size_pretty(toast_bytes) AS toast,
pg_size_pretty(total_bytes - index_bytes - toast_bytes) AS table
FROM (
SELECT
pg_total_relation_size(c.oid) AS total_bytes,
pg_indexes_size(c.oid) AS index_bytes,
coalesce(pg_total_relation_size(reltoastrelid), 0) AS toast_bytes
@benjie
benjie / loop_perf.js
Last active Jun 4, 2021
Comparing loop method performance in JS
View loop_perf.js
const arr = [];
const ARRAY_SIZE = parseInt(process.argv[2], 10) || 10;
const RUNS = Math.ceil(1000000000 / ARRAY_SIZE);
console.log(`Performing ${RUNS} runs of arrays with ${ARRAY_SIZE} elements`);
for (let i = 0; i < ARRAY_SIZE; i++) arr[i] = i;
function runTest(name, testFunction) {
if (global.gc) global.gc();
@benjie
benjie / PostGraphile_introspection.sql
Created Mar 9, 2021
PostGraphile introspection query
View PostGraphile_introspection.sql
-- This is a compiled version of the PostGraphile introspection query.
-- The query that would actually run may differ from this based on
-- what version of PostgreSQL you're running and what your PostGraphile
-- options are.
-- @see https://www.postgresql.org/docs/9.5/static/catalogs.html
-- @see https://github.com/graphile/graphile-engine/blob/master/packages/graphile-build-pg/src/plugins/introspectionQuery.js
--
with
accessible_roles(_oid) as (
@benjie
benjie / oneof_syntax.md
Last active Jan 23, 2021
Potential ideas for syntax to use for the "oneof" input/argument/output polymorphism proposal to the GraphQL Spec.
View oneof_syntax.md

oneof syntax discussion

After going full circle on @oneField directive -> tagged type -> back again, the GraphQL Input Unions Working Group have determined that our current best proposal is to add a variant of the existing input object type that accepts only one field, and a variant of object fields that accept only one argument. Though the changes to introspection for this are likely to be relatively small (e.g. adding something like __Type.isOneOf: Boolean and `__Field.isOneOf: