Skip to content

Commit 49b354e

Browse files
committed
✨ feat: adapt to SOFA standards
1 parent 09e4908 commit 49b354e

File tree

3 files changed

+43
-32
lines changed

3 files changed

+43
-32
lines changed

bun.lock

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,15 @@
99
"@pothos/plugin-smart-subscriptions": "^4.1.4",
1010
"graphql-scalars": "^1.24.2",
1111
"graphql-yoga": "^5.15.1",
12+
"pluralize": "^8.0.0",
1213
"sofa-api": "^0.18.8",
1314
},
1415
"devDependencies": {
1516
"@biomejs/biome": "2.1.1",
1617
"@graphql-tools/executor-http": "^2.0.3",
1718
"@types/bun": "latest",
1819
"@types/pg": "^8.15.4",
20+
"@types/pluralize": "^0.0.33",
1921
"drizzle-kit": "1.0.0-beta.1-c0277c0",
2022
"drizzle-orm": "1.0.0-beta.1-c0277c0",
2123
"drizzle-seed": "0.3.1-c0277c0",
@@ -210,6 +212,8 @@
210212

211213
"@types/pg": ["@types/[email protected]", "", { "dependencies": { "@types/node": "*", "pg-protocol": "*", "pg-types": "^2.2.0" } }, "sha512-I6UNVBAoYbvuWkkU3oosC8yxqH21f4/Jc4DK71JLG3dT2mdlGe1z+ep/LQGXaKaOgcvUrsQoPRqfgtMcvZiJhg=="],
212214

215+
"@types/pluralize": ["@types/[email protected]", "", {}, "sha512-JOqsl+ZoCpP4e8TDke9W79FDcSgPAR0l6pixx2JHkhnRjvShyYiAYw2LVsnA7K08Y6DeOnaU6ujmENO4os/cYg=="],
216+
213217
"@types/react": ["@types/[email protected]", "", { "dependencies": { "csstype": "^3.0.2" } }, "sha512-AwAfQ2Wa5bCx9WP8nZL2uMZWod7J7/JSplxbTmBQ5ms6QpqNYm672H0Vu9ZVKVngQ+ii4R/byguVEUZQyeg44g=="],
214218

215219
"@whatwg-node/cookie-store": ["@whatwg-node/[email protected]", "", { "dependencies": { "@whatwg-node/promise-helpers": "^1.0.0", "tslib": "^2.6.3" } }, "sha512-LPDv38Hv+RrVA8o7x4YOjQx4qhqOs3Lm8aPTsPrQCUF3MxXEUBEYcUqD5wRTaTxzQEd7ZXbDEnXBLDECFoOfjg=="],
@@ -438,6 +442,8 @@
438442

439443
"pkg-types": ["[email protected]", "", { "dependencies": { "confbox": "^0.1.8", "mlly": "^1.7.4", "pathe": "^2.0.1" } }, "sha512-/Jm5M4RvtBFVkKWRu2BLUTNP8/M2a+UwuAX+ae4770q1qVGtfjG+WTCupoZixokjmHiry8uI+dlY8KXYV5HVVQ=="],
440444

445+
"pluralize": ["[email protected]", "", {}, "sha512-Nc3IT5yHzflTfbjgqWcCPpo7DaKy4FnpB0l/zCAW0Tc7jxAiuqSxHasntB3D7887LSrA93kDJ9IXovxJYxyLCA=="],
446+
441447
"postcss-load-config": ["[email protected]", "", { "dependencies": { "lilconfig": "^3.1.1" }, "peerDependencies": { "jiti": ">=1.21.0", "postcss": ">=8.0.9", "tsx": "^4.8.1", "yaml": "^2.4.2" }, "optionalPeers": ["jiti", "postcss", "tsx", "yaml"] }, "sha512-oPtTM4oerL+UXmx+93ytZVN82RrlY/wPUV8IeDxFrzIjXOLF1pN+EmKPLbubvKHT2HC20xXsCAH2Z+CKV6Oz/g=="],
442448

443449
"postgres-array": ["[email protected]", "", {}, "sha512-VpZrUqU5A69eQyW2c5CA1jtLecCsN2U/bD6VilrFDWq5+5UIEVO7nazS3TEcHf1zuPYO/sqGvUvW62g86RXZuA=="],

lib/query.ts

Lines changed: 34 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
1+
import { plural, singular } from "pluralize";
12
import { capitalizeFirstLetter } from "./helpers/capitalize";
23
import { assertFindFirstExists } from "./helpers/helper";
3-
import type { TableIdentifierTSName } from "./helpers/tableHelpers";
4+
import {
5+
type TableIdentifierTSName,
6+
tableHelper,
7+
} from "./helpers/tableHelpers";
48
import type { OrderArgImplementerType } from "./orderArg";
59
import type { MakePubSubInstanceType } from "./pubsub";
610
import type { SchemaBuilderType } from "./schemaBuilder";
@@ -85,12 +89,17 @@ export const createQueryImplementer = <
8589
const OrderArg = orderArgImplementer({
8690
table: table,
8791
});
92+
const tableSchema = tableHelper({
93+
db,
94+
tsName: table!,
95+
});
96+
const primaryKeyField = Object.values(tableSchema.primaryColumns)[0];
8897

8998
const { registerOnInstance } = makePubSubInstance({ table: table });
9099

91100
return schemaBuilder.queryFields((t) => {
92101
return {
93-
[`findMany${capitalizeFirstLetter(table.toString())}`]: t.drizzleField({
102+
[singular(table.toString())]: t.drizzleField({
94103
type: [table],
95104
nullable: false,
96105
smartSubscription: true,
@@ -143,39 +152,33 @@ export const createQueryImplementer = <
143152
return db.query[table as any].findMany(queryInstance);
144153
},
145154
}),
146-
[`findFirst${capitalizeFirstLetter(table.toString())}`]: t.drizzleField(
147-
{
148-
type: table,
149-
nullable: false,
150-
smartSubscription: true,
151-
args: {
152-
where: t.arg({ type: WhereArg, required: false }),
153-
},
154-
resolve: (query, root, args, ctx, info) => {
155-
// transform null prototyped object
156-
// biome-ignore lint/style/noParameterAssign: Its really not a problem here
157-
args = JSON.parse(JSON.stringify(args));
158-
const filter = ctx.abilities[table as any].filter(
159-
readAction,
160-
args.where
161-
? {
162-
inject: { where: args.where },
163-
}
164-
: undefined,
165-
).query.single;
155+
[plural(table.toString())]: t.drizzleField({
156+
type: table,
157+
nullable: false,
158+
smartSubscription: true,
159+
args: {
160+
// where: t.arg({ type: WhereArg, required: false }),
161+
id: t.arg.id({ required: true }),
162+
},
163+
resolve: (query, root, args, ctx, info) => {
164+
// transform null prototyped object
165+
args = JSON.parse(JSON.stringify(args));
166166

167-
const queryInstance = query(filter as any);
167+
const filter = ctx.abilities[table as any].filter(readAction, {
168+
inject: { where: { [primaryKeyField.name]: args.id } },
169+
}).query.single;
168170

169-
if (filter.columns) {
170-
queryInstance.columns = filter.columns;
171-
}
171+
const queryInstance = query(filter as any);
172172

173-
return db.query[table as any]
174-
.findFirst(queryInstance)
175-
.then(assertFindFirstExists);
176-
},
173+
if (filter.columns) {
174+
queryInstance.columns = filter.columns;
175+
}
176+
177+
return db.query[table as any]
178+
.findFirst(queryInstance)
179+
.then(assertFindFirstExists);
177180
},
178-
),
181+
}),
179182
};
180183
});
181184
};

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,10 @@
1717
"@graphql-tools/executor-http": "^2.0.3",
1818
"@types/bun": "latest",
1919
"@types/pg": "^8.15.4",
20+
"@types/pluralize": "^0.0.33",
2021
"drizzle-kit": "1.0.0-beta.1-c0277c0",
21-
"drizzle-seed": "0.3.1-c0277c0",
2222
"drizzle-orm": "1.0.0-beta.1-c0277c0",
23+
"drizzle-seed": "0.3.1-c0277c0",
2324
"lefthook": "^1.12.2",
2425
"pg": "^8.16.3",
2526
"tsup": "^8.5.0"
@@ -32,6 +33,7 @@
3233
"@pothos/core": "^4.7.2",
3334
"@pothos/plugin-drizzle": "^0.11.0",
3435
"@pothos/plugin-smart-subscriptions": "^4.1.4",
36+
"pluralize": "^8.0.0",
3537
"graphql-scalars": "^1.24.2",
3638
"graphql-yoga": "^5.15.1",
3739
"sofa-api": "^0.18.8"

0 commit comments

Comments
 (0)