@openreachtech/hora-skills-ort-renchan 0.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +201 -0
- package/README.ja.md +135 -0
- package/README.md +135 -0
- package/dist/skills/hor-agent-loop/SKILL.md +123 -0
- package/dist/skills/hor-agent-loop/references/core.md +485 -0
- package/dist/skills/hor-agent-loop/references/graphql-integration.md +178 -0
- package/dist/skills/hor-agent-loop/references/renchan-job-integration.md +368 -0
- package/dist/skills/hor-agent-loop/references/testing.md +492 -0
- package/dist/skills/hor-ai-agent-structure/SKILL.md +403 -0
- package/dist/skills/hor-ai-agent-structure/references/loop-and-actions.md +100 -0
- package/dist/skills/hor-ai-prompt-document-store/SKILL.md +220 -0
- package/dist/skills/hor-ai-prompt-document-store/references/models-and-composition.md +156 -0
- package/dist/skills/hor-backend-testing/SKILL.md +118 -0
- package/dist/skills/hor-backend-testing/references/placement-and-ordering.md +89 -0
- package/dist/skills/hor-backend-testing/references/purity-and-mocks.md +82 -0
- package/dist/skills/hor-backend-testing/references/running-tests.md +186 -0
- package/dist/skills/hor-bank-id/SKILL.md +91 -0
- package/dist/skills/hor-build-e2e-test-environment/SKILL.md +609 -0
- package/dist/skills/hor-build-e2e-test-environment/references/compose-definition.md +368 -0
- package/dist/skills/hor-build-e2e-test-environment/references/edge-and-proxy.md +287 -0
- package/dist/skills/hor-build-e2e-test-environment/references/environment-and-ports.md +201 -0
- package/dist/skills/hor-build-e2e-test-environment/references/runner-and-lifecycle.md +330 -0
- package/dist/skills/hor-build-e2e-test-environment/references/seed-data.md +145 -0
- package/dist/skills/hor-build-e2e-test-environment/references/windows-runner.md +146 -0
- package/dist/skills/hor-constant-definition/SKILL.md +139 -0
- package/dist/skills/hor-cookie-authentication/SKILL.md +33 -0
- package/dist/skills/hor-cookie-authentication/references/auth-filter.md +55 -0
- package/dist/skills/hor-cookie-authentication/references/cookie-context.md +53 -0
- package/dist/skills/hor-cookie-authentication/references/dev-login-seeder.md +22 -0
- package/dist/skills/hor-cookie-authentication/references/environment.md +64 -0
- package/dist/skills/hor-cookie-authentication/references/error-codes.md +24 -0
- package/dist/skills/hor-cookie-authentication/references/migrations.md +18 -0
- package/dist/skills/hor-cookie-authentication/references/resolvers.md +97 -0
- package/dist/skills/hor-cookie-authentication/references/session-clerk.md +41 -0
- package/dist/skills/hor-cookie-authentication/references/token-models.md +125 -0
- package/dist/skills/hor-database-design/SKILL.md +54 -0
- package/dist/skills/hor-database-design/references/column-types.md +92 -0
- package/dist/skills/hor-database-design/references/datetimes.md +88 -0
- package/dist/skills/hor-database-design/references/master-tables.md +117 -0
- package/dist/skills/hor-database-design/references/normalization.md +40 -0
- package/dist/skills/hor-database-design/references/update-history.md +35 -0
- package/dist/skills/hor-execution-placement-pattern/SKILL.md +216 -0
- package/dist/skills/hor-external-api-client/SKILL.md +99 -0
- package/dist/skills/hor-external-api-client/references/architecture.md +127 -0
- package/dist/skills/hor-external-api-client/references/authorization.md +105 -0
- package/dist/skills/hor-external-api-client/references/capsule.md +130 -0
- package/dist/skills/hor-external-api-client/references/conventions.md +68 -0
- package/dist/skills/hor-external-api-client/references/launcher.md +138 -0
- package/dist/skills/hor-external-api-client/references/payload.md +176 -0
- package/dist/skills/hor-external-api-client/references/sdk-wrapper.md +291 -0
- package/dist/skills/hor-external-api-client/references/testing.md +130 -0
- package/dist/skills/hor-external-api-client/references/usage.md +152 -0
- package/dist/skills/hor-graphql-schema/SKILL.md +360 -0
- package/dist/skills/hor-graphql-server-engine/SKILL.md +356 -0
- package/dist/skills/hor-light-rag/SKILL.md +242 -0
- package/dist/skills/hor-light-rag/references/retrieval-internals.md +143 -0
- package/dist/skills/hor-multi-llm-provider/SKILL.md +288 -0
- package/dist/skills/hor-multi-llm-provider/references/provider-internals.md +153 -0
- package/dist/skills/hor-mutation-resolver/SKILL.md +360 -0
- package/dist/skills/hor-mutation-resolver/references/dependency-injection.md +148 -0
- package/dist/skills/hor-mutation-resolver/references/errors.md +120 -0
- package/dist/skills/hor-mutation-resolver/references/resolve-flow.md +275 -0
- package/dist/skills/hor-mutation-resolver/references/testing.md +125 -0
- package/dist/skills/hor-mutation-resolver/references/transaction.md +197 -0
- package/dist/skills/hor-post-worker/SKILL.md +320 -0
- package/dist/skills/hor-query-resolver/SKILL.md +246 -0
- package/dist/skills/hor-query-resolver/references/anatomy.md +384 -0
- package/dist/skills/hor-query-resolver/references/data-access.md +248 -0
- package/dist/skills/hor-query-resolver/references/errors-and-context.md +172 -0
- package/dist/skills/hor-query-resolver/references/testing.md +241 -0
- package/dist/skills/hor-renchan-job-bullmq/SKILL.md +178 -0
- package/dist/skills/hor-renchan-job-bullmq/references/daemon-and-scheduler.md +182 -0
- package/dist/skills/hor-renchan-job-bullmq/references/engine-and-infra.md +106 -0
- package/dist/skills/hor-renchan-job-bullmq/references/job-triple.md +189 -0
- package/dist/skills/hor-renchan-job-bullmq/references/queues.md +74 -0
- package/dist/skills/hor-renchan-job-bullmq/references/subscriptions.md +113 -0
- package/dist/skills/hor-resolver-share/SKILL.md +262 -0
- package/dist/skills/hor-resolver-validator/SKILL.md +194 -0
- package/dist/skills/hor-resolver-validator/references/inspector-api.md +115 -0
- package/dist/skills/hor-resolver-validator/references/validator-pattern.md +281 -0
- package/dist/skills/hor-restfulapi-architecture/SKILL.md +275 -0
- package/dist/skills/hor-restfulapi-architecture/references/engine-and-context.md +192 -0
- package/dist/skills/hor-restfulapi-architecture/references/renderers.md +204 -0
- package/dist/skills/hor-restfulapi-architecture/references/response-and-flusher.md +118 -0
- package/dist/skills/hor-restfulapi-architecture/references/validation.md +229 -0
- package/dist/skills/hor-security-audit/SKILL.md +110 -0
- package/dist/skills/hor-security-audit/references/application-surface.md +90 -0
- package/dist/skills/hor-security-audit/references/auth-and-transport.md +147 -0
- package/dist/skills/hor-security-audit/references/code-safety.md +74 -0
- package/dist/skills/hor-security-audit/references/dependencies.md +72 -0
- package/dist/skills/hor-security-audit/references/network-and-logging.md +56 -0
- package/dist/skills/hor-security-audit/references/secrets.md +95 -0
- package/dist/skills/hor-sequelize-migration/SKILL.md +336 -0
- package/dist/skills/hor-sequelize-migration/references/alter-table.md +147 -0
- package/dist/skills/hor-sequelize-migration/references/foreign-keys.md +107 -0
- package/dist/skills/hor-sequelize-migration/references/indexes.md +149 -0
- package/dist/skills/hor-sequelize-migration/references/notation.md +215 -0
- package/dist/skills/hor-sequelize-model/SKILL.md +69 -0
- package/dist/skills/hor-sequelize-model/references/associations.md +218 -0
- package/dist/skills/hor-sequelize-model/references/default-methods.md +244 -0
- package/dist/skills/hor-sequelize-model/references/mixins.md +247 -0
- package/dist/skills/hor-sequelize-model/references/notation.md +225 -0
- package/dist/skills/hor-sequelize-model/references/timestamps.md +74 -0
- package/dist/skills/hor-sequelize-seeder/SKILL.md +275 -0
- package/dist/skills/hor-sequelize-seeder/references/development-coverage.md +74 -0
- package/dist/skills/hor-sequelize-seeder/references/directory-structure.md +113 -0
- package/dist/skills/hor-sequelize-seeder/references/id-numbering.md +133 -0
- package/dist/skills/hor-sequelize-seeder/references/naming.md +59 -0
- package/dist/skills/hor-sequelize-seeder/references/notation.md +170 -0
- package/dist/skills/hor-sequelize-subquery/SKILL.md +369 -0
- package/dist/skills/hor-strategy-pattern/SKILL.md +371 -0
- package/dist/skills/hor-stub-api/SKILL.md +330 -0
- package/dist/skills/hor-subscription-resolver/SKILL.md +105 -0
- package/dist/skills/hor-subscription-resolver/references/authentication.md +99 -0
- package/dist/skills/hor-subscription-resolver/references/basic-notation.md +116 -0
- package/dist/skills/hor-subscription-resolver/references/design-philosophy.md +68 -0
- package/dist/skills/hor-type-interface/SKILL.md +200 -0
- package/docs/skills.ja.md +40 -0
- package/docs/skills.md +40 -0
- package/lib/equip/CommandLineArguments.js +178 -0
- package/lib/equip/HoraSkillsCli.js +590 -0
- package/lib/equip/SkillsInstaller.js +443 -0
- package/lib/equip/SkillsManifestFile.js +260 -0
- package/lib/equip/constants/CLI_COMMAND.js +8 -0
- package/lib/equip/hora-skills-ort-renchan.js +8 -0
- package/package.json +51 -0
|
@@ -0,0 +1,384 @@
|
|
|
1
|
+
# Anatomy (the full resolver template + typedefs)
|
|
2
|
+
|
|
3
|
+
The complete member-by-member shape of a query resolver, the minimal no-input variant, and the
|
|
4
|
+
trailing `@typedef` block. Referenced from §2 of [SKILL.md](../SKILL.md).
|
|
5
|
+
|
|
6
|
+
> The `users` / `User` / `Department` / `Role` names below are placeholder domain names — swap in your
|
|
7
|
+
> resource, fields, and endpoint.
|
|
8
|
+
|
|
9
|
+
## Full template (a paginated list query)
|
|
10
|
+
|
|
11
|
+
This is the canonical shape: `schema` getter → `errorCodeHash` → `resolve()` (validate → read →
|
|
12
|
+
format) → validation wiring → finders → `formatResponse()`, then the typedefs. Members appear in the
|
|
13
|
+
fixed order; each does one job.
|
|
14
|
+
|
|
15
|
+
```js
|
|
16
|
+
import {
|
|
17
|
+
Op,
|
|
18
|
+
} from 'sequelize'
|
|
19
|
+
|
|
20
|
+
import {
|
|
21
|
+
BaseQueryResolver,
|
|
22
|
+
} from '@openreachtech/renchan'
|
|
23
|
+
|
|
24
|
+
import UsersInputValidator from '../../../../../../app/tools/Validator/forResolver/admin/queries/UsersInputValidator.js'
|
|
25
|
+
|
|
26
|
+
import User from '../../../../../../sequelize/models/User.js'
|
|
27
|
+
import UserProfile from '../../../../../../sequelize/models/UserProfile.js'
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Resolve the users query.
|
|
31
|
+
*
|
|
32
|
+
* @extends {BaseQueryResolver}
|
|
33
|
+
*/
|
|
34
|
+
export default class UsersQueryResolver extends BaseQueryResolver {
|
|
35
|
+
/** @override */
|
|
36
|
+
static get schema () {
|
|
37
|
+
return 'users'
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* .get:errorCodeHash
|
|
42
|
+
*
|
|
43
|
+
* @override
|
|
44
|
+
* @returns {ErrorCodeHash}
|
|
45
|
+
*/
|
|
46
|
+
static get errorCodeHash () {
|
|
47
|
+
return {
|
|
48
|
+
...super.errorCodeHash,
|
|
49
|
+
|
|
50
|
+
// Invalid Input Errors (203 prefix)
|
|
51
|
+
InvalidKeyword: '203.Q001.001',
|
|
52
|
+
InvalidDepartmentId: '203.Q001.002',
|
|
53
|
+
InvalidPaginationLimit: '203.Q001.003',
|
|
54
|
+
InvalidPaginationOffset: '203.Q001.004',
|
|
55
|
+
InvalidPaginationSort: '203.Q001.005',
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Resolve the users query.
|
|
61
|
+
*
|
|
62
|
+
* @override
|
|
63
|
+
* @param {{
|
|
64
|
+
* variables: {
|
|
65
|
+
* input: server.graphql.admin.UsersInput
|
|
66
|
+
* }
|
|
67
|
+
* }} params
|
|
68
|
+
* @returns {Promise<server.graphql.admin.UsersResult>}
|
|
69
|
+
*/
|
|
70
|
+
async resolve ({
|
|
71
|
+
variables: {
|
|
72
|
+
input,
|
|
73
|
+
},
|
|
74
|
+
}) {
|
|
75
|
+
const validationError = this.validateInput({
|
|
76
|
+
input,
|
|
77
|
+
})
|
|
78
|
+
|
|
79
|
+
if (validationError) {
|
|
80
|
+
throw validationError
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
const {
|
|
84
|
+
departmentId,
|
|
85
|
+
keyword,
|
|
86
|
+
pagination,
|
|
87
|
+
} = input
|
|
88
|
+
|
|
89
|
+
const whereClause = this.buildWhereClause({
|
|
90
|
+
departmentId,
|
|
91
|
+
keyword,
|
|
92
|
+
})
|
|
93
|
+
|
|
94
|
+
const totalRecords = await this.countUsers({
|
|
95
|
+
whereClause,
|
|
96
|
+
})
|
|
97
|
+
|
|
98
|
+
const users = await this.findUsers({
|
|
99
|
+
whereClause,
|
|
100
|
+
pagination,
|
|
101
|
+
})
|
|
102
|
+
|
|
103
|
+
return this.formatResponse({
|
|
104
|
+
users,
|
|
105
|
+
pagination,
|
|
106
|
+
totalRecords,
|
|
107
|
+
})
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* Validate the input of the users query.
|
|
112
|
+
*
|
|
113
|
+
* @param {{
|
|
114
|
+
* input: server.graphql.admin.UsersInput
|
|
115
|
+
* }} params
|
|
116
|
+
* @returns {InstanceType<RenchanGraphqlErrorCtor> | null}
|
|
117
|
+
*/
|
|
118
|
+
validateInput ({
|
|
119
|
+
input,
|
|
120
|
+
}) {
|
|
121
|
+
const validator = this.createInputValidator({
|
|
122
|
+
input,
|
|
123
|
+
})
|
|
124
|
+
|
|
125
|
+
return validator.validateInput()
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* Create a UsersInputValidator instance.
|
|
130
|
+
*
|
|
131
|
+
* @param {{
|
|
132
|
+
* input: server.graphql.admin.UsersInput
|
|
133
|
+
* errorHash?: ErrorHash
|
|
134
|
+
* }} params
|
|
135
|
+
* @returns {UsersInputValidator}
|
|
136
|
+
*/
|
|
137
|
+
createInputValidator ({
|
|
138
|
+
input,
|
|
139
|
+
errorHash = this.errorHash,
|
|
140
|
+
}) {
|
|
141
|
+
return UsersInputValidator.create({
|
|
142
|
+
input,
|
|
143
|
+
errorHash,
|
|
144
|
+
})
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* Build the where clause for the users query.
|
|
149
|
+
*
|
|
150
|
+
* @param {{
|
|
151
|
+
* departmentId?: number
|
|
152
|
+
* keyword?: string
|
|
153
|
+
* }} params
|
|
154
|
+
* @returns {object}
|
|
155
|
+
*/
|
|
156
|
+
buildWhereClause ({
|
|
157
|
+
departmentId,
|
|
158
|
+
keyword,
|
|
159
|
+
}) {
|
|
160
|
+
const conditions = []
|
|
161
|
+
|
|
162
|
+
if (
|
|
163
|
+
keyword
|
|
164
|
+
&& keyword.trim().length > 0
|
|
165
|
+
) {
|
|
166
|
+
conditions.push({
|
|
167
|
+
name: {
|
|
168
|
+
[Op.like]: `${keyword}%`,
|
|
169
|
+
},
|
|
170
|
+
})
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
if (departmentId) {
|
|
174
|
+
conditions.push({
|
|
175
|
+
DepartmentId: departmentId,
|
|
176
|
+
})
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
if (conditions.length === 0) {
|
|
180
|
+
return {}
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
return {
|
|
184
|
+
[Op.and]: conditions,
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/**
|
|
189
|
+
* Count users.
|
|
190
|
+
*
|
|
191
|
+
* @param {{
|
|
192
|
+
* whereClause: object
|
|
193
|
+
* }} params
|
|
194
|
+
* @returns {Promise<number>}
|
|
195
|
+
*/
|
|
196
|
+
countUsers ({
|
|
197
|
+
whereClause,
|
|
198
|
+
}) {
|
|
199
|
+
return User.count({
|
|
200
|
+
where: whereClause,
|
|
201
|
+
})
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/**
|
|
205
|
+
* Find users.
|
|
206
|
+
*
|
|
207
|
+
* @param {{
|
|
208
|
+
* whereClause: object
|
|
209
|
+
* pagination: graphql.NormalPaginationInput
|
|
210
|
+
* }} params
|
|
211
|
+
* @returns {Promise<Array<model.User>>}
|
|
212
|
+
*/
|
|
213
|
+
async findUsers ({
|
|
214
|
+
whereClause,
|
|
215
|
+
pagination: {
|
|
216
|
+
limit,
|
|
217
|
+
offset,
|
|
218
|
+
sort = {
|
|
219
|
+
targetColumn: 'name',
|
|
220
|
+
orderBy: 'ASC',
|
|
221
|
+
},
|
|
222
|
+
},
|
|
223
|
+
}) {
|
|
224
|
+
return /** @type {Promise<*>} */ (
|
|
225
|
+
User.findAll({
|
|
226
|
+
where: whereClause,
|
|
227
|
+
offset,
|
|
228
|
+
limit,
|
|
229
|
+
order: [
|
|
230
|
+
[sort.targetColumn, sort.orderBy],
|
|
231
|
+
],
|
|
232
|
+
})
|
|
233
|
+
)
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
/**
|
|
237
|
+
* Format the response for the users query.
|
|
238
|
+
*
|
|
239
|
+
* @param {{
|
|
240
|
+
* users: Array<model.User>
|
|
241
|
+
* pagination: graphql.NormalPaginationInput
|
|
242
|
+
* totalRecords: number
|
|
243
|
+
* }} params
|
|
244
|
+
* @returns {server.graphql.admin.UsersResult}
|
|
245
|
+
*/
|
|
246
|
+
formatResponse ({
|
|
247
|
+
users,
|
|
248
|
+
pagination: {
|
|
249
|
+
limit,
|
|
250
|
+
offset,
|
|
251
|
+
sort = null,
|
|
252
|
+
},
|
|
253
|
+
totalRecords,
|
|
254
|
+
}) {
|
|
255
|
+
return {
|
|
256
|
+
users: users
|
|
257
|
+
.map(user => ({
|
|
258
|
+
userId: user.id,
|
|
259
|
+
name: user.name,
|
|
260
|
+
email: user.email,
|
|
261
|
+
isActive: user.isActive,
|
|
262
|
+
})),
|
|
263
|
+
pagination: {
|
|
264
|
+
limit,
|
|
265
|
+
offset,
|
|
266
|
+
sort,
|
|
267
|
+
totalRecords,
|
|
268
|
+
},
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
/**
|
|
274
|
+
* @typedef {{
|
|
275
|
+
* InvalidKeyword: string
|
|
276
|
+
* InvalidDepartmentId: string
|
|
277
|
+
* InvalidPaginationLimit: string
|
|
278
|
+
* InvalidPaginationOffset: string
|
|
279
|
+
* InvalidPaginationSort: string
|
|
280
|
+
* }} ErrorCodeHash
|
|
281
|
+
*/
|
|
282
|
+
|
|
283
|
+
/**
|
|
284
|
+
* @typedef {Record<string, RenchanGraphqlErrorCtor>} ErrorHash
|
|
285
|
+
*/
|
|
286
|
+
|
|
287
|
+
/**
|
|
288
|
+
* @typedef {typeof import('@openreachtech/renchan').RenchanGraphqlError} RenchanGraphqlErrorCtor
|
|
289
|
+
*/
|
|
290
|
+
```
|
|
291
|
+
|
|
292
|
+
## Member order (recap)
|
|
293
|
+
|
|
294
|
+
1. `static get schema ()`
|
|
295
|
+
2. `static get errorCodeHash ()`
|
|
296
|
+
3. `async resolve ()`
|
|
297
|
+
4. `validateInput ()`
|
|
298
|
+
5. `createInputValidator ()`
|
|
299
|
+
6. finders / helpers, **in call order** (`buildWhereClause` → `countX` → `findX`); unrelated helpers
|
|
300
|
+
in dictionary order
|
|
301
|
+
7. `formatResponse ()`
|
|
302
|
+
8. error creators (`createXxxNotFoundError`), when built through helpers ([errors-and-context.md](./errors-and-context.md))
|
|
303
|
+
|
|
304
|
+
- A `constructor` / `static create ()` is added **only** when the resolver needs an extra
|
|
305
|
+
dependency; the framework's `BaseResolver.create()` already builds the instance (injecting
|
|
306
|
+
`errorHash` from `errorCodeHash`) for the common case, so most resolvers declare neither.
|
|
307
|
+
|
|
308
|
+
## Minimal variant (no input, no validator)
|
|
309
|
+
|
|
310
|
+
When the field takes no `input` (or only reads auth off `context`), drop the validation wiring
|
|
311
|
+
entirely. Still declare `schema` and `errorCodeHash`:
|
|
312
|
+
|
|
313
|
+
```js
|
|
314
|
+
import {
|
|
315
|
+
BaseQueryResolver,
|
|
316
|
+
} from '@openreachtech/renchan'
|
|
317
|
+
|
|
318
|
+
import Department from '../../../../../../sequelize/models/Department.js'
|
|
319
|
+
|
|
320
|
+
export default class DepartmentsQueryResolver extends BaseQueryResolver {
|
|
321
|
+
/** @override */
|
|
322
|
+
static get schema () {
|
|
323
|
+
return 'departments'
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
/** @override */
|
|
327
|
+
static get errorCodeHash () {
|
|
328
|
+
return {
|
|
329
|
+
...super.errorCodeHash,
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
/** @override */
|
|
334
|
+
async resolve () {
|
|
335
|
+
const departmentEntries = /** @type {Array<*>} */ (
|
|
336
|
+
await Department.findAll()
|
|
337
|
+
)
|
|
338
|
+
|
|
339
|
+
const departments = departmentEntries
|
|
340
|
+
.map(({
|
|
341
|
+
id,
|
|
342
|
+
name,
|
|
343
|
+
}) => ({
|
|
344
|
+
id,
|
|
345
|
+
name,
|
|
346
|
+
}))
|
|
347
|
+
|
|
348
|
+
return {
|
|
349
|
+
departments,
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
```
|
|
354
|
+
|
|
355
|
+
## The trailing typedef block
|
|
356
|
+
|
|
357
|
+
Types tied to the DB and the GraphQL schema live in `/types` (`model.*`, `server.graphql.*`); the
|
|
358
|
+
resolver file only **aliases** them and declares its own local shapes. Keep the block at the bottom,
|
|
359
|
+
after the class. A resolver with associations typically declares:
|
|
360
|
+
|
|
361
|
+
- `ErrorCodeHash` — the keys of `errorCodeHash` (values `string`).
|
|
362
|
+
- `Input` / `Result` aliases — `@typedef {server.graphql.<endpoint>.XxxInput} XxxInput`.
|
|
363
|
+
- `ErrorHash` — `Record<string, RenchanGraphqlErrorCtor>`.
|
|
364
|
+
- `RenchanGraphqlErrorCtor` — `typeof import('@openreachtech/renchan').RenchanGraphqlError`.
|
|
365
|
+
- An **associated-entity** typedef describing the `include` tree, when a finder returns a record with
|
|
366
|
+
nested associations:
|
|
367
|
+
|
|
368
|
+
```js
|
|
369
|
+
/**
|
|
370
|
+
* @typedef {model.UserEntity & {
|
|
371
|
+
* UserProfile: model.UserProfileEntity
|
|
372
|
+
* Department: model.DepartmentEntity & {
|
|
373
|
+
* Office: model.OfficeEntity
|
|
374
|
+
* }
|
|
375
|
+
* UserRoleAssignments: Array<model.UserRoleAssignmentEntity & {
|
|
376
|
+
* Role: model.RoleEntity
|
|
377
|
+
* }>
|
|
378
|
+
* }} UserWithAssociations
|
|
379
|
+
*/
|
|
380
|
+
```
|
|
381
|
+
|
|
382
|
+
- When the resolver reads auth or injected providers off `context`, also alias the context type:
|
|
383
|
+
`@typedef {import('../../../../contexts/AdminGraphqlContext.js').default} AdminGraphqlContext`
|
|
384
|
+
([errors-and-context.md](./errors-and-context.md)).
|
|
@@ -0,0 +1,248 @@
|
|
|
1
|
+
# Data access (finders, pagination, includes, extraction)
|
|
2
|
+
|
|
3
|
+
How a query resolver reads and shapes data: where-clause builders, the count+findAll pagination
|
|
4
|
+
trio, association `include` trees, model `subquery()`, and `FieldPathValueExtractor`. Referenced from
|
|
5
|
+
§3 and §6 of [SKILL.md](../SKILL.md).
|
|
6
|
+
|
|
7
|
+
> The `User` / `UserProfile` / `Department` / `Office` / `Role` names are placeholder domain names —
|
|
8
|
+
> swap in your own models and fields.
|
|
9
|
+
|
|
10
|
+
## Finders are thin; the query shape lives in its own method
|
|
11
|
+
|
|
12
|
+
Keep `resolve()` a pipeline by giving each read its own small method. A finder wraps one Sequelize
|
|
13
|
+
call; the where clause, pagination, and includes are built by dedicated helpers so each is
|
|
14
|
+
independently testable.
|
|
15
|
+
|
|
16
|
+
- **One record:** `Model.findByPk(id, { include })` or `Model.findOne({ where, include })`, followed
|
|
17
|
+
by a not-found guard in `resolve()` that throws a domain error
|
|
18
|
+
([errors-and-context.md](./errors-and-context.md)).
|
|
19
|
+
- **A list:** `Model.findAll({ where, offset, limit, order })`.
|
|
20
|
+
- **A count:** `Model.count({ where })` — used for `totalRecords`.
|
|
21
|
+
|
|
22
|
+
```js
|
|
23
|
+
async findUser ({
|
|
24
|
+
userId,
|
|
25
|
+
activeRoleId,
|
|
26
|
+
}) {
|
|
27
|
+
return /** @type {Promise<UserWithAssociations | null>} */ (
|
|
28
|
+
User.findByPk(userId, {
|
|
29
|
+
include: [
|
|
30
|
+
{
|
|
31
|
+
model: UserRoleAssignment,
|
|
32
|
+
required: true,
|
|
33
|
+
where: {
|
|
34
|
+
RoleId: activeRoleId,
|
|
35
|
+
},
|
|
36
|
+
},
|
|
37
|
+
UserProfile,
|
|
38
|
+
],
|
|
39
|
+
})
|
|
40
|
+
)
|
|
41
|
+
}
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
- Cast the Sequelize return to your associated-entity typedef with a `/** @type {...} */ ( ... )`
|
|
45
|
+
wrapper — the model methods return loosely-typed instances.
|
|
46
|
+
|
|
47
|
+
## Pagination: the count + findAll trio
|
|
48
|
+
|
|
49
|
+
A paginated list query is always the same three steps, driven from `resolve()`:
|
|
50
|
+
|
|
51
|
+
1. `buildWhereClause()` — turn the input filters into a Sequelize `where` object.
|
|
52
|
+
2. `countX({ whereClause })` — total matching rows, **before** paging, for `totalRecords`.
|
|
53
|
+
3. `findX({ whereClause, pagination })` — the page, applying `offset` / `limit` / `order`.
|
|
54
|
+
|
|
55
|
+
```js
|
|
56
|
+
const whereClause = this.buildWhereClause({ departmentId, keyword })
|
|
57
|
+
const totalRecords = await this.countUsers({ whereClause })
|
|
58
|
+
const users = await this.findUsers({ whereClause, pagination })
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
- **Both** the count and the find use the **same** `whereClause`, so the total matches the filter.
|
|
62
|
+
- `pagination` is destructured in the finder with a **default sort** so a missing `sort` still
|
|
63
|
+
produces a deterministic order:
|
|
64
|
+
|
|
65
|
+
```js
|
|
66
|
+
pagination: {
|
|
67
|
+
limit,
|
|
68
|
+
offset,
|
|
69
|
+
sort = {
|
|
70
|
+
targetColumn: 'name',
|
|
71
|
+
orderBy: 'ASC',
|
|
72
|
+
},
|
|
73
|
+
},
|
|
74
|
+
// ...
|
|
75
|
+
order: [
|
|
76
|
+
[sort.targetColumn, sort.orderBy],
|
|
77
|
+
],
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
- `formatResponse()` **echoes** the pagination back (`limit` / `offset` / `sort` / `totalRecords`) so
|
|
81
|
+
the client can render the pager ([response formatting](#formatresponse-shape-dont-leak-models)).
|
|
82
|
+
- The allowed `sort` columns are enforced by the `*InputValidator`, not here
|
|
83
|
+
(`hor-resolver-validator`).
|
|
84
|
+
|
|
85
|
+
## buildWhereClause: accumulate conditions, combine with Op.and
|
|
86
|
+
|
|
87
|
+
Build the `where` by pushing each active filter onto a `conditions` array, then combine. Skip a
|
|
88
|
+
filter when its input is absent so an empty input means "no filter" (return `{}`).
|
|
89
|
+
|
|
90
|
+
```js
|
|
91
|
+
buildWhereClause ({
|
|
92
|
+
departmentId,
|
|
93
|
+
keyword,
|
|
94
|
+
}) {
|
|
95
|
+
const conditions = []
|
|
96
|
+
|
|
97
|
+
const keywordInspector = ValueInspector.create({
|
|
98
|
+
value: keyword,
|
|
99
|
+
})
|
|
100
|
+
|
|
101
|
+
if (
|
|
102
|
+
keywordInspector.isPresent()
|
|
103
|
+
&& keyword.trim().length > 0
|
|
104
|
+
) {
|
|
105
|
+
conditions.push({
|
|
106
|
+
[Op.or]: [
|
|
107
|
+
{
|
|
108
|
+
id: {
|
|
109
|
+
[Op.in]: UserProfile.subquery(
|
|
110
|
+
'?firstName?lastName.UserId',
|
|
111
|
+
{
|
|
112
|
+
keyword,
|
|
113
|
+
}
|
|
114
|
+
),
|
|
115
|
+
},
|
|
116
|
+
},
|
|
117
|
+
{
|
|
118
|
+
name: {
|
|
119
|
+
[Op.like]: `${keyword}%`,
|
|
120
|
+
},
|
|
121
|
+
},
|
|
122
|
+
],
|
|
123
|
+
})
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
if (departmentId) {
|
|
127
|
+
conditions.push({
|
|
128
|
+
DepartmentId: departmentId,
|
|
129
|
+
})
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
if (conditions.length === 0) {
|
|
133
|
+
return {}
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
return {
|
|
137
|
+
[Op.and]: conditions,
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
- Import `Op` from `sequelize`. Use `[Op.like]` for prefix search (`` `${keyword}%` ``), `[Op.or]`
|
|
143
|
+
for multi-column keyword match, `[Op.in]` with a subquery for a related-table filter.
|
|
144
|
+
- Import `ValueInspector` from `@openreachtech/mentsu-value-inspector`;
|
|
145
|
+
`ValueInspector.create({ value: keyword }).isPresent()` guards `null` / `undefined` before
|
|
146
|
+
`keyword.trim()`. The package has no string/blank inspector (presence + number / integer / date
|
|
147
|
+
only), so the blank `.trim().length > 0` check stays inline.
|
|
148
|
+
- `Model.subquery('<field-spec>', { keyword })` returns a subquery usable inside `[Op.in]` — it lets a
|
|
149
|
+
keyword filter reach columns on an associated table (here `UserProfile`) without a join. Match the
|
|
150
|
+
field-spec string to the model's declared subquery.
|
|
151
|
+
|
|
152
|
+
## Association includes
|
|
153
|
+
|
|
154
|
+
Read nested data with an `include` tree passed to the finder. Bare models include the whole
|
|
155
|
+
association; an object form adds `where` / `required` / `as` / nested `include`.
|
|
156
|
+
|
|
157
|
+
```js
|
|
158
|
+
include: [
|
|
159
|
+
UserProfile, // include the whole association
|
|
160
|
+
{
|
|
161
|
+
model: Office,
|
|
162
|
+
as: 'HomeOffice', // aliased association (same model, distinct role)
|
|
163
|
+
},
|
|
164
|
+
{
|
|
165
|
+
model: Office,
|
|
166
|
+
as: 'BranchOffice',
|
|
167
|
+
},
|
|
168
|
+
{
|
|
169
|
+
model: Department,
|
|
170
|
+
include: [
|
|
171
|
+
Office, // nested include
|
|
172
|
+
],
|
|
173
|
+
},
|
|
174
|
+
{
|
|
175
|
+
model: UserRoleAssignment,
|
|
176
|
+
required: true, // INNER JOIN — filters the parent
|
|
177
|
+
where: {
|
|
178
|
+
RoleId: activeRoleId,
|
|
179
|
+
},
|
|
180
|
+
},
|
|
181
|
+
]
|
|
182
|
+
```
|
|
183
|
+
|
|
184
|
+
- `required: true` makes it an inner join (the parent row is dropped when the association is absent) —
|
|
185
|
+
use it to filter by a related table. Default is a left join.
|
|
186
|
+
- Add `separate: true` to a `hasMany` include that needs its **own `order` / `limit`** — Sequelize
|
|
187
|
+
runs it as a separate query, which also avoids the N+1 a bare nested `hasMany` can cause on a large
|
|
188
|
+
parent set.
|
|
189
|
+
- Describe the resulting shape with an associated-entity `@typedef`
|
|
190
|
+
([anatomy.md](./anatomy.md#the-trailing-typedef-block)) so the extractor and `formatResponse` are
|
|
191
|
+
type-checked.
|
|
192
|
+
|
|
193
|
+
## formatResponse: shape, don't leak models
|
|
194
|
+
|
|
195
|
+
`formatResponse()` maps the model entities to the schema's field names and is the only place that
|
|
196
|
+
builds the output object. Never return a raw model instance.
|
|
197
|
+
|
|
198
|
+
- Rename columns to schema fields (`user.id` → `userId`).
|
|
199
|
+
- Map arrays to their output shape; `filter` out records whose required association is missing.
|
|
200
|
+
- Default missing scalars (`?? ''` / `?? null`) — do not leak `undefined`.
|
|
201
|
+
- **Money / decimal fields → strings.** Emit a decimal (money, rate) via `BigNumber(value).toFixed(2)`
|
|
202
|
+
(compute into a named `const` first), matching the `String!` SDL money type (`hor-graphql-schema`) —
|
|
203
|
+
never a raw JS number.
|
|
204
|
+
- Echo pagination for lists.
|
|
205
|
+
|
|
206
|
+
### Deep values via FieldPathValueExtractor
|
|
207
|
+
|
|
208
|
+
When a value sits several associations deep, pull it with `FieldPathValueExtractor` instead of
|
|
209
|
+
walking `a?.b?.c` by hand. Create one per root entity and read by dotted field path:
|
|
210
|
+
|
|
211
|
+
```js
|
|
212
|
+
const userFieldPathValueExtractor = FieldPathValueExtractor.create({
|
|
213
|
+
originEntity: user,
|
|
214
|
+
})
|
|
215
|
+
|
|
216
|
+
const bio = userFieldPathValueExtractor.extractFieldPathValue({
|
|
217
|
+
fieldPath: 'User.UserProfile.bio',
|
|
218
|
+
})
|
|
219
|
+
```
|
|
220
|
+
|
|
221
|
+
- The path starts at the **root model name** and walks association names down to the column.
|
|
222
|
+
- For an optional nested object, extract the id first and branch:
|
|
223
|
+
|
|
224
|
+
```js
|
|
225
|
+
const departmentId = extractor.extractFieldPathValue({
|
|
226
|
+
fieldPath: 'User.Department.id',
|
|
227
|
+
})
|
|
228
|
+
|
|
229
|
+
return {
|
|
230
|
+
department: departmentId
|
|
231
|
+
? {
|
|
232
|
+
departmentId,
|
|
233
|
+
name: extractor.extractFieldPathValue({ fieldPath: 'User.Department.name' }),
|
|
234
|
+
}
|
|
235
|
+
: null,
|
|
236
|
+
}
|
|
237
|
+
```
|
|
238
|
+
|
|
239
|
+
- Guard array associations with `?? []` before `map`, and drop incomplete records with `filter`:
|
|
240
|
+
|
|
241
|
+
```js
|
|
242
|
+
roles: (user.UserRoleAssignments ?? [])
|
|
243
|
+
.filter(record => record?.Role?.name)
|
|
244
|
+
.map(record => ({
|
|
245
|
+
roleId: record.Role.id,
|
|
246
|
+
name: record.Role.name,
|
|
247
|
+
})),
|
|
248
|
+
```
|