Skip to content

Commit

Permalink
refactor: convert to TypeScript (#149)
Browse files Browse the repository at this point in the history
  • Loading branch information
mattbretl authored Apr 4, 2021
1 parent 1c7fa26 commit 8a848f5
Show file tree
Hide file tree
Showing 55 changed files with 2,608 additions and 2,098 deletions.
10 changes: 5 additions & 5 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
module.exports = {
root: true,
parser: "@typescript-eslint/parser",
env: {
node: true,
es6: true,
"jest/globals": true,
},
parserOptions: {
ecmaVersion: 9,
},
plugins: ["jest"],
plugins: ["@typescript-eslint", "jest"],
extends: [
"eslint:recommended",
"plugin:@typescript-eslint/recommended",
"plugin:jest/recommended",
"plugin:prettier/recommended",
"prettier",
],
rules: {
"jest/expect-expect": ["off"],
Expand Down
16 changes: 16 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Debug Jest Tests",
"type": "node",
"request": "launch",
"runtimeArgs": ["--inspect-brk", "${workspaceRoot}/node_modules/.bin/jest", "--runInBand"],
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen"
}
]
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
module.exports = function CustomOperatorsPlugin(builder) {
import type { Plugin } from "graphile-build";
import { Build } from "postgraphile-core";
import { AddConnectionFilterOperator } from "../src/PgConnectionArgFilterPlugin";

const CustomOperatorsPlugin: Plugin = (builder) => {
builder.hook("build", (_, build) => {
const {
pgSql: sql,
graphql: { GraphQLInt, GraphQLBoolean },
addConnectionFilterOperator,
} = build;
} = build as Build & {
addConnectionFilterOperator: AddConnectionFilterOperator;
};

// simple
addConnectionFilterOperator(
Expand All @@ -23,7 +29,7 @@ module.exports = function CustomOperatorsPlugin(builder) {
() => GraphQLInt,
(i, v) => sql.fragment`${i} <> ${v}`,
{
resolveSqlIdentifier: i => sql.fragment`family(${i})`,
resolveSqlIdentifier: (i) => sql.fragment`family(${i})`,
}
);

Expand All @@ -35,10 +41,12 @@ module.exports = function CustomOperatorsPlugin(builder) {
() => GraphQLBoolean,
(i, v) => sql.fragment`family(${i}) = ${v}`,
{
resolveInput: input => (input === true ? 4 : 6),
resolveInput: (input) => (input === true ? 4 : 6),
}
);

return _;
});
};

export default CustomOperatorsPlugin;
122 changes: 0 additions & 122 deletions __tests__/helpers.js

This file was deleted.

78 changes: 78 additions & 0 deletions __tests__/helpers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import * as pg from "pg";
import { parse, buildASTSchema, GraphQLSchema } from "graphql";
import { printSchema } from "graphql/utilities";

export async function withPgPool<T>(
cb: (pool: pg.Pool) => Promise<T>
): Promise<T> {
const pool = new pg.Pool({
connectionString: process.env.TEST_DATABASE_URL,
});
try {
return await cb(pool);
} finally {
pool.end();
}
}

export async function withPgClient<T>(
cb: (client: pg.PoolClient) => Promise<T>
): Promise<T> {
return withPgPool(async (pool) => {
const client = await pool.connect();
try {
return await cb(client);
} finally {
client.release();
}
});
}

export async function withTransaction<T>(
cb: (client: pg.PoolClient) => Promise<T>,
closeCommand = "rollback"
): Promise<T> {
return withPgClient(async (client) => {
await client.query("begin");
try {
return await cb(client);
} finally {
await client.query(closeCommand);
}
});
}

export function printSchemaOrdered(originalSchema: GraphQLSchema): string {
// Clone schema so we don't damage anything
const schema = buildASTSchema(parse(printSchema(originalSchema)));

const typeMap = schema.getTypeMap();
Object.keys(typeMap).forEach((name) => {
const gqlType = typeMap[name];

// Object?
if ("getFields" in gqlType) {
const fields = gqlType.getFields();
const keys = Object.keys(fields).sort();
keys.forEach((key) => {
const value = fields[key];

// Move the key to the end of the object
delete fields[key];
fields[key] = value;

// Sort args
if ("args" in value) {
value.args.sort((a, b) => a.name.localeCompare(b.name));
}
});
}

// Enum?
if ("getValues" in gqlType) {
gqlType.getValues().sort((a, b) => a.name.localeCompare(b.name));
}
});

return printSchema(schema);
}
Loading

0 comments on commit 8a848f5

Please sign in to comment.