Skip to content

Instantly share code, notes, and snippets.


Benjie benjie

View GitHub Profile
benjie / PostGraphile_introspection.sql
Created March 9, 2021 19:35
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
-- @see
accessible_roles(_oid) as (
benjie /
Last active January 23, 2021 10:47
Potential ideas for syntax to use for the "oneof" input/argument/output polymorphism proposal to the GraphQL Spec.

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:

benjie / DeprecateTypePlugin.ts
Created August 13, 2020 18:38
Deprecate all fields that reference a named output type in Graphile Engine
View DeprecateTypePlugin.ts
import type { Plugin } from "graphile-build";
const makeDeprecateTypePlugin = (typeName: string, deprecationReason: string): Plugin => builder => {
builder.hook('GraphQLObjectType:fields:field', (field, { graphql }) => {
if (graphql.getNamedType(field.type).name !== typeName) {
return field;
return {
View separate_snapshots.js
const { promises: fsp } = require("fs");
async function main(name) {
const content = await fsp.readFile(`./${name}.test.ts.snap`, "utf8");
const lines = content.split("\n");
const results = {};
let header = "";
for (let lineNumber = 0; lineNumber < lines.length; lineNumber++) {
const line = lines[lineNumber];
if (line.startsWith("//")) {
benjie /
Last active May 11, 2023 09:21
Exploring how `oneOf` could work in a GraphQL schema

oneOf exploration

This document is a work-in-progress exploration of how the "oneOf" solution to input polymorphism might work within a GraphQL schema.

Base schema

For the examples below, we'll be using the following shared types using existing GraphQL syntax:

benjie / .gmrc
Created October 10, 2019 15:51
Example gmrc from (not published at time of writing)
View .gmrc
"pgSettings": {
"search_path": "app_public,app_private,app_hidden,public"
"placeholders": {
"afterReset": [
benjie / hacky-hooks.tsx
Created August 2, 2019 11:50
Hacky hooks to help you solve certain performance issues with minimal fuss; e.g. `<Foo bar={[1, 2, 3]} />` passes a new array every time, but `useDynamicToStatic` can be used to treat it as if it were the same array so as to not trigger further hook's update methods.
View hacky-hooks.tsx
* If you're sent a dynamic value but you want to treat it as static (e.g. for
* hook dependencies), you can use this function. For arrays it will compare the
* contents of the array rather than the array object itself. For objects it
* will compare the keys and values of the object, rather than the object
* itself. For functions it will just replace the function with a static
* function that calls the underlying function using references.
export function useDynamicToStatic<T>(value: T) {
let condition = [];
View PgUpsertPlugin.js
module.exports = function PgUpsertPlugin(
{ pgDisableDefaultMutations }
) {
if (pgDisableDefaultMutations) {
builder.hook("inflection", (inflection, build) =>
build.extend(inflection, {
benjie /
Created December 7, 2018 09:53
Updates the `latest` dist-tag on a package so that package@latest points to the same version as package@next
echo "Enter OTP code:"
read otp
export NPM_CONFIG_OTP=$otp
function next2latest {
V=$(npm info $1 | xargs)
npm dist-tags add $1@$V latest
next2latest graphql-parse-resolve-info
next2latest graphile-build
View emulate_require.js
* Read a file manually, but pretend it's actually a Node.js module. Tested in Node 8.11.3 only.
"use strict";
const fs = require("fs");
const path = require("path");
const { Module } = require("module");
// Get the javascript contents from wherever: filesystem, database, network, etc.
// Node calls this filename, but it's actually the full path to the file