-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: convert to TypeScript (#149)
- Loading branch information
Showing
55 changed files
with
2,608 additions
and
2,098 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
File renamed without changes.
Oops, something went wrong.