|
| 1 | +import { plural, singular } from "pluralize"; |
1 | 2 | import { capitalizeFirstLetter } from "./helpers/capitalize"; |
2 | 3 | import { assertFindFirstExists } from "./helpers/helper"; |
3 | | -import type { TableIdentifierTSName } from "./helpers/tableHelpers"; |
| 4 | +import { |
| 5 | + type TableIdentifierTSName, |
| 6 | + tableHelper, |
| 7 | +} from "./helpers/tableHelpers"; |
4 | 8 | import type { OrderArgImplementerType } from "./orderArg"; |
5 | 9 | import type { MakePubSubInstanceType } from "./pubsub"; |
6 | 10 | import type { SchemaBuilderType } from "./schemaBuilder"; |
@@ -85,12 +89,17 @@ export const createQueryImplementer = < |
85 | 89 | const OrderArg = orderArgImplementer({ |
86 | 90 | table: table, |
87 | 91 | }); |
| 92 | + const tableSchema = tableHelper({ |
| 93 | + db, |
| 94 | + tsName: table!, |
| 95 | + }); |
| 96 | + const primaryKeyField = Object.values(tableSchema.primaryColumns)[0]; |
88 | 97 |
|
89 | 98 | const { registerOnInstance } = makePubSubInstance({ table: table }); |
90 | 99 |
|
91 | 100 | return schemaBuilder.queryFields((t) => { |
92 | 101 | return { |
93 | | - [`findMany${capitalizeFirstLetter(table.toString())}`]: t.drizzleField({ |
| 102 | + [singular(table.toString())]: t.drizzleField({ |
94 | 103 | type: [table], |
95 | 104 | nullable: false, |
96 | 105 | smartSubscription: true, |
@@ -143,39 +152,33 @@ export const createQueryImplementer = < |
143 | 152 | return db.query[table as any].findMany(queryInstance); |
144 | 153 | }, |
145 | 154 | }), |
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)); |
166 | 166 |
|
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; |
168 | 170 |
|
169 | | - if (filter.columns) { |
170 | | - queryInstance.columns = filter.columns; |
171 | | - } |
| 171 | + const queryInstance = query(filter as any); |
172 | 172 |
|
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); |
177 | 180 | }, |
178 | | - ), |
| 181 | + }), |
179 | 182 | }; |
180 | 183 | }); |
181 | 184 | }; |
|
0 commit comments