Skip to content

Commit 8a848f5

Browse files
authored
refactor: convert to TypeScript (#149)
1 parent 1c7fa26 commit 8a848f5

File tree

55 files changed

+2608
-2098
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+2608
-2098
lines changed

.eslintrc.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
module.exports = {
2+
root: true,
3+
parser: "@typescript-eslint/parser",
24
env: {
35
node: true,
46
es6: true,
57
"jest/globals": true,
68
},
7-
parserOptions: {
8-
ecmaVersion: 9,
9-
},
10-
plugins: ["jest"],
9+
plugins: ["@typescript-eslint", "jest"],
1110
extends: [
1211
"eslint:recommended",
12+
"plugin:@typescript-eslint/recommended",
1313
"plugin:jest/recommended",
14-
"plugin:prettier/recommended",
14+
"prettier",
1515
],
1616
rules: {
1717
"jest/expect-expect": ["off"],

.vscode/launch.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
// Use IntelliSense to learn about possible attributes.
3+
// Hover to view descriptions of existing attributes.
4+
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5+
"version": "0.2.0",
6+
"configurations": [
7+
{
8+
"name": "Debug Jest Tests",
9+
"type": "node",
10+
"request": "launch",
11+
"runtimeArgs": ["--inspect-brk", "${workspaceRoot}/node_modules/.bin/jest", "--runInBand"],
12+
"console": "integratedTerminal",
13+
"internalConsoleOptions": "neverOpen"
14+
}
15+
]
16+
}
Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
1-
module.exports = function CustomOperatorsPlugin(builder) {
1+
import type { Plugin } from "graphile-build";
2+
import { Build } from "postgraphile-core";
3+
import { AddConnectionFilterOperator } from "../src/PgConnectionArgFilterPlugin";
4+
5+
const CustomOperatorsPlugin: Plugin = (builder) => {
26
builder.hook("build", (_, build) => {
37
const {
48
pgSql: sql,
59
graphql: { GraphQLInt, GraphQLBoolean },
610
addConnectionFilterOperator,
7-
} = build;
11+
} = build as Build & {
12+
addConnectionFilterOperator: AddConnectionFilterOperator;
13+
};
814

915
// simple
1016
addConnectionFilterOperator(
@@ -23,7 +29,7 @@ module.exports = function CustomOperatorsPlugin(builder) {
2329
() => GraphQLInt,
2430
(i, v) => sql.fragment`${i} <> ${v}`,
2531
{
26-
resolveSqlIdentifier: i => sql.fragment`family(${i})`,
32+
resolveSqlIdentifier: (i) => sql.fragment`family(${i})`,
2733
}
2834
);
2935

@@ -35,10 +41,12 @@ module.exports = function CustomOperatorsPlugin(builder) {
3541
() => GraphQLBoolean,
3642
(i, v) => sql.fragment`family(${i}) = ${v}`,
3743
{
38-
resolveInput: input => (input === true ? 4 : 6),
44+
resolveInput: (input) => (input === true ? 4 : 6),
3945
}
4046
);
4147

4248
return _;
4349
});
4450
};
51+
52+
export default CustomOperatorsPlugin;

__tests__/helpers.js

Lines changed: 0 additions & 122 deletions
This file was deleted.

__tests__/helpers.ts

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import * as pg from "pg";
2+
import { parse, buildASTSchema, GraphQLSchema } from "graphql";
3+
import { printSchema } from "graphql/utilities";
4+
5+
export async function withPgPool<T>(
6+
cb: (pool: pg.Pool) => Promise<T>
7+
): Promise<T> {
8+
const pool = new pg.Pool({
9+
connectionString: process.env.TEST_DATABASE_URL,
10+
});
11+
try {
12+
return await cb(pool);
13+
} finally {
14+
pool.end();
15+
}
16+
}
17+
18+
export async function withPgClient<T>(
19+
cb: (client: pg.PoolClient) => Promise<T>
20+
): Promise<T> {
21+
return withPgPool(async (pool) => {
22+
const client = await pool.connect();
23+
try {
24+
return await cb(client);
25+
} finally {
26+
client.release();
27+
}
28+
});
29+
}
30+
31+
export async function withTransaction<T>(
32+
cb: (client: pg.PoolClient) => Promise<T>,
33+
closeCommand = "rollback"
34+
): Promise<T> {
35+
return withPgClient(async (client) => {
36+
await client.query("begin");
37+
try {
38+
return await cb(client);
39+
} finally {
40+
await client.query(closeCommand);
41+
}
42+
});
43+
}
44+
45+
export function printSchemaOrdered(originalSchema: GraphQLSchema): string {
46+
// Clone schema so we don't damage anything
47+
const schema = buildASTSchema(parse(printSchema(originalSchema)));
48+
49+
const typeMap = schema.getTypeMap();
50+
Object.keys(typeMap).forEach((name) => {
51+
const gqlType = typeMap[name];
52+
53+
// Object?
54+
if ("getFields" in gqlType) {
55+
const fields = gqlType.getFields();
56+
const keys = Object.keys(fields).sort();
57+
keys.forEach((key) => {
58+
const value = fields[key];
59+
60+
// Move the key to the end of the object
61+
delete fields[key];
62+
fields[key] = value;
63+
64+
// Sort args
65+
if ("args" in value) {
66+
value.args.sort((a, b) => a.name.localeCompare(b.name));
67+
}
68+
});
69+
}
70+
71+
// Enum?
72+
if ("getValues" in gqlType) {
73+
gqlType.getValues().sort((a, b) => a.name.localeCompare(b.name));
74+
}
75+
});
76+
77+
return printSchema(schema);
78+
}

__tests__/integration/__snapshots__/queries.test.js.snap renamed to __tests__/integration/__snapshots__/queries.test.ts.snap

File renamed without changes.

0 commit comments

Comments
 (0)