Skip to content

Commit 9abc924

Browse files
authored
Convert plugin to TypeScript (#54)
1 parent 6afd81f commit 9abc924

File tree

12 files changed

+188
-28
lines changed

12 files changed

+188
-28
lines changed

.eslintignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/dist
2+
/node_modules

.eslintrc.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,23 @@
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",
1414
"plugin:prettier/recommended",
1515
],
1616
rules: {
1717
"jest/expect-expect": ["off"],
18+
"@typescript-eslint/no-namespace": ["off"],
19+
20+
// Rules to disable in V5 port
21+
"@typescript-eslint/no-var-requires": ["off"],
1822
},
1923
};

.gitignore

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,6 @@
1-
node_modules
2-
.idea
1+
/.git
2+
/.idea
3+
/.yarn
4+
/.yarnrc.yml
5+
/dist
6+
/node_modules

__tests__/integration/queries.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@ beforeAll(() => {
2929
// need and wait for them to be created in parallel.
3030
const [normal, columnAggregates] = await Promise.all([
3131
createPostGraphileSchema(pgClient, ["p"], {
32-
appendPlugins: [require("../../index.js")],
32+
appendPlugins: [require("../../dist/index.js")],
3333
}),
3434
createPostGraphileSchema(pgClient, ["p"], {
35-
appendPlugins: [require("../../index.js")],
35+
appendPlugins: [require("../../dist/index.js")],
3636
graphileBuildOptions: {
3737
orderByRelatedColumnAggregates: true,
3838
},

__tests__/integration/schema/columnAggregates.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ const core = require("./core");
33
test(
44
"prints a schema with the order-by-related plugin",
55
core.test(["p"], {
6-
appendPlugins: [require("../../../index.js")],
6+
appendPlugins: [require("../../../dist/index.js")],
77
disableDefaultMutations: true,
88
legacyRelations: "omit",
99
graphileBuildOptions: {

__tests__/integration/schema/ignoreIndexes.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ const core = require("./core");
33
test(
44
"prints a schema with `ignoreIndexes: false`",
55
core.test(["p"], {
6-
appendPlugins: [require("../../../index.js")],
6+
appendPlugins: [require("../../../dist/index.js")],
77
disableDefaultMutations: true,
88
legacyRelations: "omit",
99
ignoreIndexes: false,

__tests__/integration/schema/orderByRelated.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ const core = require("./core");
33
test(
44
"prints a schema with the order-by-related plugin",
55
core.test(["p"], {
6-
appendPlugins: [require("../../../index.js")],
6+
appendPlugins: [require("../../../dist/index.js")],
77
disableDefaultMutations: true,
88
legacyRelations: "omit",
99
})

index.d.ts

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

package.json

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,16 @@
22
"name": "@graphile-contrib/pg-order-by-related",
33
"version": "1.0.0",
44
"description": "Order by related columns on PostGraphile connections",
5-
"main": "index.js",
6-
"types": "index.d.ts",
5+
"main": "dist/index.js",
6+
"types": "dist/index.d.ts",
77
"scripts": {
88
"format": "prettier --ignore-path ./.eslintignore",
99
"format:all": "yarn format '**/*.{json,md,html,js,jsx,ts,tsx}'",
1010
"format:fix": "yarn format:all --write",
1111
"format:check": "yarn format:all --list-different",
12-
"lint": "eslint . --ext .js,.jsx",
13-
"test": "scripts/test"
12+
"lint": "eslint . --ext .js,.jsx,.ts,.tsx",
13+
"prepack": "tsc",
14+
"test": "tsc && scripts/test"
1415
},
1516
"repository": {
1617
"type": "git",
@@ -22,6 +23,9 @@
2223
"url": "https://github.com/graphile-contrib/pg-order-by-related/issues"
2324
},
2425
"devDependencies": {
26+
"@tsconfig/node18": "^18.2.4",
27+
"@typescript-eslint/eslint-plugin": "5.59.0",
28+
"@typescript-eslint/parser": "5.59.0",
2529
"eslint": "8.28.0",
2630
"eslint-config-prettier": "8.5.0",
2731
"eslint-plugin-jest": "27.1.6",
@@ -30,13 +34,13 @@
3034
"jest": "29.3.1",
3135
"pg": "8.8.0",
3236
"postgraphile-core": "4.12.3",
33-
"prettier": "2.8.0"
37+
"prettier": "2.8.0",
38+
"typescript": "^5.7.2"
3439
},
3540
"jest": {
3641
"testRegex": "__tests__/.*\\.test\\.js$"
3742
},
3843
"files": [
39-
"index.js",
40-
"index.d.ts"
44+
"dist"
4145
]
4246
}

index.js renamed to src/index.ts

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
1-
function PgOrderByRelatedPlugin(builder, { orderByRelatedColumnAggregates }) {
1+
import type { Inflection, Plugin } from "graphile-build";
2+
3+
const PgOrderByRelatedPlugin: Plugin = (
4+
builder,
5+
{ orderByRelatedColumnAggregates }
6+
) => {
27
builder.hook("build", (build) => {
3-
const pkg = require("./package.json");
8+
const pkg = require("../package.json");
49

510
// Check dependencies
611
if (!build.versions) {
712
throw new Error(
813
`Plugin ${pkg.name}@${pkg.version} requires graphile-build@^4.1.0 in order to check dependencies (current version: ${build.graphileBuildVersion})`
914
);
1015
}
11-
const depends = (name, range) => {
16+
const depends = (name: string, range: string) => {
1217
if (!build.hasVersion(name, range)) {
1318
throw new Error(
1419
`Plugin ${pkg.name}@${pkg.version} requires ${name}@${range} (${
@@ -29,14 +34,21 @@ function PgOrderByRelatedPlugin(builder, { orderByRelatedColumnAggregates }) {
2934

3035
builder.hook("inflection", (inflection) => {
3136
return Object.assign(inflection, {
32-
orderByRelatedColumnEnum(attr, ascending, foreignTable, keyAttributes) {
37+
orderByRelatedColumnEnum(
38+
this: Inflection,
39+
attr,
40+
ascending,
41+
foreignTable,
42+
keyAttributes
43+
) {
3344
return `${this.constantCase(
3445
`${this._singularizedTableName(foreignTable)}-by-${keyAttributes
3546
.map((keyAttr) => this._columnName(keyAttr))
3647
.join("-and-")}`
3748
)}__${this.orderByColumnEnum(attr, ascending)}`;
3849
},
3950
orderByRelatedComputedColumnEnum(
51+
this: Inflection,
4052
pseudoColumnName,
4153
proc,
4254
ascending,
@@ -54,7 +66,12 @@ function PgOrderByRelatedPlugin(builder, { orderByRelatedColumnAggregates }) {
5466
ascending
5567
)}`;
5668
},
57-
orderByRelatedCountEnum(ascending, foreignTable, keyAttributes) {
69+
orderByRelatedCountEnum(
70+
this: Inflection,
71+
ascending,
72+
foreignTable,
73+
keyAttributes
74+
) {
5875
return `${this.constantCase(
5976
`${this.pluralize(
6077
this._singularizedTableName(foreignTable)
@@ -64,6 +81,7 @@ function PgOrderByRelatedPlugin(builder, { orderByRelatedColumnAggregates }) {
6481
)}__${this.constantCase(`count-${ascending ? "asc" : "desc"}`)}`;
6582
},
6683
orderByRelatedColumnAggregateEnum(
84+
this: Inflection,
6785
attr,
6886
ascending,
6987
foreignTable,
@@ -514,9 +532,11 @@ function PgOrderByRelatedPlugin(builder, { orderByRelatedColumnAggregates }) {
514532
const pseudoColumnName = proc.name.substr(table.name.length + 1);
515533
return { argTypes, pseudoColumnName };
516534
}
517-
}
535+
};
536+
537+
export default PgOrderByRelatedPlugin;
518538

539+
// HACK: for TypeScript/Babel import
519540
module.exports = PgOrderByRelatedPlugin;
520-
// Hacks for TypeScript/Babel import
521541
module.exports.default = PgOrderByRelatedPlugin;
522542
Object.defineProperty(module.exports, "__esModule", { value: true });

tsconfig.json

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"extends": "@tsconfig/node18/tsconfig.json",
3+
"compilerOptions": {
4+
"noImplicitAny": false,
5+
"rootDir": "./src",
6+
"outDir": "./dist",
7+
"declarationDir": "dist",
8+
"declaration": true,
9+
"sourceMap": true
10+
},
11+
"include": ["src"],
12+
"exclude": ["node_modules"]
13+
}

0 commit comments

Comments
 (0)