Skip to content

Commit

Permalink
Convert plugin to TypeScript (#54)
Browse files Browse the repository at this point in the history
  • Loading branch information
benjie authored Nov 23, 2024
1 parent 6afd81f commit 9abc924
Show file tree
Hide file tree
Showing 12 changed files with 188 additions and 28 deletions.
2 changes: 2 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/dist
/node_modules
12 changes: 8 additions & 4 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
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",
],
rules: {
"jest/expect-expect": ["off"],
"@typescript-eslint/no-namespace": ["off"],

// Rules to disable in V5 port
"@typescript-eslint/no-var-requires": ["off"],
},
};
8 changes: 6 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
node_modules
.idea
/.git
/.idea
/.yarn
/.yarnrc.yml
/dist
/node_modules
4 changes: 2 additions & 2 deletions __tests__/integration/queries.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ beforeAll(() => {
// need and wait for them to be created in parallel.
const [normal, columnAggregates] = await Promise.all([
createPostGraphileSchema(pgClient, ["p"], {
appendPlugins: [require("../../index.js")],
appendPlugins: [require("../../dist/index.js")],
}),
createPostGraphileSchema(pgClient, ["p"], {
appendPlugins: [require("../../index.js")],
appendPlugins: [require("../../dist/index.js")],
graphileBuildOptions: {
orderByRelatedColumnAggregates: true,
},
Expand Down
2 changes: 1 addition & 1 deletion __tests__/integration/schema/columnAggregates.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const core = require("./core");
test(
"prints a schema with the order-by-related plugin",
core.test(["p"], {
appendPlugins: [require("../../../index.js")],
appendPlugins: [require("../../../dist/index.js")],
disableDefaultMutations: true,
legacyRelations: "omit",
graphileBuildOptions: {
Expand Down
2 changes: 1 addition & 1 deletion __tests__/integration/schema/ignoreIndexes.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const core = require("./core");
test(
"prints a schema with `ignoreIndexes: false`",
core.test(["p"], {
appendPlugins: [require("../../../index.js")],
appendPlugins: [require("../../../dist/index.js")],
disableDefaultMutations: true,
legacyRelations: "omit",
ignoreIndexes: false,
Expand Down
2 changes: 1 addition & 1 deletion __tests__/integration/schema/orderByRelated.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const core = require("./core");
test(
"prints a schema with the order-by-related plugin",
core.test(["p"], {
appendPlugins: [require("../../../index.js")],
appendPlugins: [require("../../../dist/index.js")],
disableDefaultMutations: true,
legacyRelations: "omit",
})
Expand Down
3 changes: 0 additions & 3 deletions index.d.ts

This file was deleted.

18 changes: 11 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@
"name": "@graphile-contrib/pg-order-by-related",
"version": "1.0.0",
"description": "Order by related columns on PostGraphile connections",
"main": "index.js",
"types": "index.d.ts",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"scripts": {
"format": "prettier --ignore-path ./.eslintignore",
"format:all": "yarn format '**/*.{json,md,html,js,jsx,ts,tsx}'",
"format:fix": "yarn format:all --write",
"format:check": "yarn format:all --list-different",
"lint": "eslint . --ext .js,.jsx",
"test": "scripts/test"
"lint": "eslint . --ext .js,.jsx,.ts,.tsx",
"prepack": "tsc",
"test": "tsc && scripts/test"
},
"repository": {
"type": "git",
Expand All @@ -22,6 +23,9 @@
"url": "https://github.com/graphile-contrib/pg-order-by-related/issues"
},
"devDependencies": {
"@tsconfig/node18": "^18.2.4",
"@typescript-eslint/eslint-plugin": "5.59.0",
"@typescript-eslint/parser": "5.59.0",
"eslint": "8.28.0",
"eslint-config-prettier": "8.5.0",
"eslint-plugin-jest": "27.1.6",
Expand All @@ -30,13 +34,13 @@
"jest": "29.3.1",
"pg": "8.8.0",
"postgraphile-core": "4.12.3",
"prettier": "2.8.0"
"prettier": "2.8.0",
"typescript": "^5.7.2"
},
"jest": {
"testRegex": "__tests__/.*\\.test\\.js$"
},
"files": [
"index.js",
"index.d.ts"
"dist"
]
}
34 changes: 27 additions & 7 deletions index.js → src/index.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
function PgOrderByRelatedPlugin(builder, { orderByRelatedColumnAggregates }) {
import type { Inflection, Plugin } from "graphile-build";

const PgOrderByRelatedPlugin: Plugin = (
builder,
{ orderByRelatedColumnAggregates }
) => {
builder.hook("build", (build) => {
const pkg = require("./package.json");
const pkg = require("../package.json");

// Check dependencies
if (!build.versions) {
throw new Error(
`Plugin ${pkg.name}@${pkg.version} requires graphile-build@^4.1.0 in order to check dependencies (current version: ${build.graphileBuildVersion})`
);
}
const depends = (name, range) => {
const depends = (name: string, range: string) => {
if (!build.hasVersion(name, range)) {
throw new Error(
`Plugin ${pkg.name}@${pkg.version} requires ${name}@${range} (${
Expand All @@ -29,14 +34,21 @@ function PgOrderByRelatedPlugin(builder, { orderByRelatedColumnAggregates }) {

builder.hook("inflection", (inflection) => {
return Object.assign(inflection, {
orderByRelatedColumnEnum(attr, ascending, foreignTable, keyAttributes) {
orderByRelatedColumnEnum(
this: Inflection,
attr,
ascending,
foreignTable,
keyAttributes
) {
return `${this.constantCase(
`${this._singularizedTableName(foreignTable)}-by-${keyAttributes
.map((keyAttr) => this._columnName(keyAttr))
.join("-and-")}`
)}__${this.orderByColumnEnum(attr, ascending)}`;
},
orderByRelatedComputedColumnEnum(
this: Inflection,
pseudoColumnName,
proc,
ascending,
Expand All @@ -54,7 +66,12 @@ function PgOrderByRelatedPlugin(builder, { orderByRelatedColumnAggregates }) {
ascending
)}`;
},
orderByRelatedCountEnum(ascending, foreignTable, keyAttributes) {
orderByRelatedCountEnum(
this: Inflection,
ascending,
foreignTable,
keyAttributes
) {
return `${this.constantCase(
`${this.pluralize(
this._singularizedTableName(foreignTable)
Expand All @@ -64,6 +81,7 @@ function PgOrderByRelatedPlugin(builder, { orderByRelatedColumnAggregates }) {
)}__${this.constantCase(`count-${ascending ? "asc" : "desc"}`)}`;
},
orderByRelatedColumnAggregateEnum(
this: Inflection,
attr,
ascending,
foreignTable,
Expand Down Expand Up @@ -514,9 +532,11 @@ function PgOrderByRelatedPlugin(builder, { orderByRelatedColumnAggregates }) {
const pseudoColumnName = proc.name.substr(table.name.length + 1);
return { argTypes, pseudoColumnName };
}
}
};

export default PgOrderByRelatedPlugin;

// HACK: for TypeScript/Babel import
module.exports = PgOrderByRelatedPlugin;
// Hacks for TypeScript/Babel import
module.exports.default = PgOrderByRelatedPlugin;
Object.defineProperty(module.exports, "__esModule", { value: true });
13 changes: 13 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"extends": "@tsconfig/node18/tsconfig.json",
"compilerOptions": {
"noImplicitAny": false,
"rootDir": "./src",
"outDir": "./dist",
"declarationDir": "dist",
"declaration": true,
"sourceMap": true
},
"include": ["src"],
"exclude": ["node_modules"]
}
Loading

0 comments on commit 9abc924

Please sign in to comment.