@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,262 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: hor-resolver-share
|
|
3
|
+
description: >
|
|
4
|
+
Implement the Share class — the per-process container of shared singletons (subscription broker,
|
|
5
|
+
worker/job dispatcher, authorization service, shared external clients, environment access) that
|
|
6
|
+
the framework builds once at server boot and hands to every GraphQL resolver as context.share.
|
|
7
|
+
Use this skill whenever the user asks to add or change what is shared across an API's resolvers,
|
|
8
|
+
or to decide whether something belongs in Share (per process) or Context (per request).
|
|
9
|
+
---
|
|
10
|
+
|
|
11
|
+
# Resolver Share
|
|
12
|
+
|
|
13
|
+
A skill for the **Share class**: the object that holds the instances **shared across every resolver of
|
|
14
|
+
an API** and is reachable from a resolver as `context.share`. The framework builds one Share **once
|
|
15
|
+
per server process** at boot and passes it into each request's Context, so resolvers read shared
|
|
16
|
+
dependencies from it instead of constructing their own.
|
|
17
|
+
|
|
18
|
+
> This skill states a **general, project-independent rule** — a refined best practice, not a
|
|
19
|
+
> description of any one project's code, so it need not match a given repo's existing notation. The
|
|
20
|
+
> class and property names in it (`CustomerGraphqlShare`, `workerDispatcher`, `permissionService`, …)
|
|
21
|
+
> are **illustrative fakes**; use your project's own names and never hard-code its file paths or domain
|
|
22
|
+
> concepts into the rule. The framework base class is shown as `BaseGraphqlShare`; treat it as "your
|
|
23
|
+
> framework's Share base".
|
|
24
|
+
|
|
25
|
+
## Grand principle: Share is a per-process container of shared singletons — no request state, no logic
|
|
26
|
+
|
|
27
|
+
Share holds the instances that are **built once and reused by every request/resolver** of an API:
|
|
28
|
+
the subscription broker, the worker (background-job) dispatcher, the authorization/permission
|
|
29
|
+
service, shared external clients, environment access. The framework constructs it at boot and passes
|
|
30
|
+
it — via the Context — to every resolver as `context.share`. Share's only job is **to hold and expose
|
|
31
|
+
these shared instances**; it runs no business logic and stores no per-request data.
|
|
32
|
+
|
|
33
|
+
- **Why per-process, built once.** These instances hold connections or are expensive to create
|
|
34
|
+
(a PubSub broker, a job-queue connection, a permission service). Building them once at boot and
|
|
35
|
+
sharing them keeps connection counts bounded and startup deterministic — a resolver that built its
|
|
36
|
+
own broker per call would exhaust connections.
|
|
37
|
+
- **Why no request state.** One Share instance is shared by **all concurrent requests**. Anything
|
|
38
|
+
request-scoped or per-user (the current principal, the request input, a per-request transaction)
|
|
39
|
+
belongs in the **Context** (built fresh per request), never on Share — writing it to Share creates
|
|
40
|
+
data races between concurrent requests.
|
|
41
|
+
- **Why no business logic.** Share is a wiring container. Domain decisions live in resolvers and the
|
|
42
|
+
services they call; Share only *provides* those services. Keeping it a pure container makes the set
|
|
43
|
+
of shared dependencies auditable in one place.
|
|
44
|
+
|
|
45
|
+
**Comment language**: the `js` examples here use English comments. In the Share files you generate,
|
|
46
|
+
keep structural comments in English and write any domain notes in whatever language the project uses
|
|
47
|
+
for domain prose — match the surrounding files. Sample code follows the project's lint style
|
|
48
|
+
(no semicolons, 2-space indent, a space before the parameter parenthesis, trailing commas).
|
|
49
|
+
|
|
50
|
+
## 1. Share (per process) vs Context (per request)
|
|
51
|
+
|
|
52
|
+
The two containers split by **lifetime and sharing**:
|
|
53
|
+
|
|
54
|
+
| | Share | Context |
|
|
55
|
+
| --- | --- | --- |
|
|
56
|
+
| Built | once, at server boot (`createAsync`) | fresh, per request |
|
|
57
|
+
| Shared by | **all** concurrent requests of the process | one request |
|
|
58
|
+
| Holds | shared singletons (broker, dispatcher, permission service, clients, env) | the request principal, per-request state, and a handle to `share` |
|
|
59
|
+
| Reached from a resolver | `context.share.<name>` | `context.<name>` |
|
|
60
|
+
|
|
61
|
+
- **Decision rule**: is the thing the **same for every request** and safe to reuse concurrently?
|
|
62
|
+
→ Share. Does it **depend on who is calling or on this request**? → Context.
|
|
63
|
+
- The Context carries a reference to the Share, which is why a resolver reaches a shared instance
|
|
64
|
+
through `context.share`. Share does not know about Context or resolvers — the dependency is
|
|
65
|
+
one-way (Context → Share).
|
|
66
|
+
|
|
67
|
+
```js
|
|
68
|
+
// Good: request-scoped principal lives on the Context; shared dispatcher lives on Share
|
|
69
|
+
async resolve ({
|
|
70
|
+
variables,
|
|
71
|
+
context,
|
|
72
|
+
}) {
|
|
73
|
+
const caller = context.principal // per-request → Context
|
|
74
|
+
|
|
75
|
+
await context.share.workerDispatcher.dispatchJob({ // shared singleton → Share
|
|
76
|
+
body: {
|
|
77
|
+
requestedBy: caller.id,
|
|
78
|
+
},
|
|
79
|
+
})
|
|
80
|
+
|
|
81
|
+
return {
|
|
82
|
+
accepted: true,
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
```js
|
|
88
|
+
// Avoid: stashing request-scoped state on the shared object
|
|
89
|
+
this.currentPrincipal = authenticatedPrincipal // shared across all concurrent requests → data race
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
## 2. What belongs in Share (the admission test)
|
|
93
|
+
|
|
94
|
+
A candidate goes on Share only when it passes **all three**:
|
|
95
|
+
|
|
96
|
+
1. **Shared** — used across multiple resolvers (often across multiple APIs).
|
|
97
|
+
2. **Concurrency-safe** — stateless, or internally synchronized; holding no per-request data.
|
|
98
|
+
3. **Build-once-worthy** — holds a connection or is expensive to create, so building it per request
|
|
99
|
+
would be wasteful or wrong.
|
|
100
|
+
|
|
101
|
+
Typical members (role names, not fixed to any project):
|
|
102
|
+
|
|
103
|
+
| Member | What it is |
|
|
104
|
+
| --- | --- |
|
|
105
|
+
| `subscriptionBroker` | The PubSub broker that publishes/subscribes GraphQL subscription events across processes. |
|
|
106
|
+
| `workerDispatcher` | The enqueuer that dispatches background jobs to workers (reuses one queue connection). |
|
|
107
|
+
| `permissionService` | The shared authorization/permission service resolvers consult to gate access. |
|
|
108
|
+
| shared external clients | Long-lived clients for external services (reused, connection-pooled). |
|
|
109
|
+
| `env` | Environment/configuration access shared process-wide. |
|
|
110
|
+
|
|
111
|
+
Do **not** put on Share: request inputs or the current principal (→ Context), resolver/business
|
|
112
|
+
logic, GraphQL entities or query results, or a mutable cache keyed by request.
|
|
113
|
+
|
|
114
|
+
## 3. The class shape (constructor DI + `create` + `createAsync`)
|
|
115
|
+
|
|
116
|
+
A Share class extends the framework Share base and follows the standard member order:
|
|
117
|
+
**constructor → `create` (sync factory) → `createAsync` (async boot factory)**. Dependencies are
|
|
118
|
+
**injected through the constructor** and held as read-only properties.
|
|
119
|
+
|
|
120
|
+
```js
|
|
121
|
+
import {
|
|
122
|
+
BaseGraphqlShare,
|
|
123
|
+
} from '@openreachtech/renchan'
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* Shared object for the customer GraphQL API.
|
|
127
|
+
* Holds process-lifetime instances shared by every resolver of this endpoint.
|
|
128
|
+
*
|
|
129
|
+
* @extends {BaseGraphqlShare}
|
|
130
|
+
*/
|
|
131
|
+
export default class CustomerGraphqlShare extends BaseGraphqlShare {
|
|
132
|
+
/**
|
|
133
|
+
* Constructor.
|
|
134
|
+
*
|
|
135
|
+
* @param {{
|
|
136
|
+
* subscriptionBroker: object
|
|
137
|
+
* workerDispatcher: object
|
|
138
|
+
* permissionService: object
|
|
139
|
+
* }} params - Own dependencies; base params are forwarded via rest.
|
|
140
|
+
*/
|
|
141
|
+
constructor ({
|
|
142
|
+
subscriptionBroker,
|
|
143
|
+
workerDispatcher,
|
|
144
|
+
permissionService,
|
|
145
|
+
...restArgs
|
|
146
|
+
}) {
|
|
147
|
+
super(restArgs)
|
|
148
|
+
|
|
149
|
+
this.subscriptionBroker = subscriptionBroker
|
|
150
|
+
this.workerDispatcher = workerDispatcher
|
|
151
|
+
this.permissionService = permissionService
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* Factory method.
|
|
156
|
+
*
|
|
157
|
+
* @param {*} params - Same shape as the constructor.
|
|
158
|
+
* @returns {CustomerGraphqlShare} Instance of this class.
|
|
159
|
+
*/
|
|
160
|
+
static create ({
|
|
161
|
+
subscriptionBroker,
|
|
162
|
+
workerDispatcher,
|
|
163
|
+
permissionService,
|
|
164
|
+
...restArgs
|
|
165
|
+
}) {
|
|
166
|
+
return new this({
|
|
167
|
+
subscriptionBroker,
|
|
168
|
+
workerDispatcher,
|
|
169
|
+
permissionService,
|
|
170
|
+
...restArgs,
|
|
171
|
+
})
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/**
|
|
175
|
+
* Async factory method. Assembles the shared instances once, at server boot.
|
|
176
|
+
*
|
|
177
|
+
* @param {{
|
|
178
|
+
* config: object
|
|
179
|
+
* }} params - Boot configuration.
|
|
180
|
+
* @returns {Promise<CustomerGraphqlShare>} Instance of this class.
|
|
181
|
+
*/
|
|
182
|
+
static async createAsync ({
|
|
183
|
+
config,
|
|
184
|
+
}) {
|
|
185
|
+
const subscriptionBroker = this.createBroker({
|
|
186
|
+
config,
|
|
187
|
+
})
|
|
188
|
+
|
|
189
|
+
const workerDispatcher = await WorkerDispatcher.createAsync({
|
|
190
|
+
config,
|
|
191
|
+
})
|
|
192
|
+
|
|
193
|
+
const permissionService = PermissionService.create({
|
|
194
|
+
config,
|
|
195
|
+
})
|
|
196
|
+
|
|
197
|
+
return this.create({
|
|
198
|
+
env: this.generateEnv(),
|
|
199
|
+
subscriptionBroker,
|
|
200
|
+
workerDispatcher,
|
|
201
|
+
permissionService,
|
|
202
|
+
})
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
```
|
|
206
|
+
|
|
207
|
+
- **Constructor = pure assignment** (`super(restArgs)` then assign own deps). No I/O, no building —
|
|
208
|
+
everything is already built and handed in. This is what makes Share testable ([§6](#6-testing)).
|
|
209
|
+
- **`createAsync` is the single place the shared instances are assembled.** Async because some deps
|
|
210
|
+
connect (broker, queue). Build them, then call `create`. The framework base commonly supplies
|
|
211
|
+
helpers for the cross-cutting ones (a broker factory, an env accessor); use them rather than
|
|
212
|
+
re-implementing.
|
|
213
|
+
- **Properties are read-only after construction** and named for their role (arrays plural, single
|
|
214
|
+
instances singular). Resolvers read them; nothing reassigns them.
|
|
215
|
+
|
|
216
|
+
## 4. Lifecycle & wiring (boot → Context → resolver)
|
|
217
|
+
|
|
218
|
+
The flow is one-directional and happens once at boot, then per request:
|
|
219
|
+
|
|
220
|
+
1. **Boot**: the API's engine names its Share class; the framework calls `Share.createAsync({ config })`
|
|
221
|
+
**once**, producing the single per-process Share.
|
|
222
|
+
2. **Per request**: the framework builds a Context for the request and gives it a reference to that
|
|
223
|
+
Share.
|
|
224
|
+
3. **In the resolver**: code reads shared instances via `context.share.<name>`.
|
|
225
|
+
|
|
226
|
+
- **Share never imports resolvers or the Context** — the arrows point Context → Share, resolver →
|
|
227
|
+
`context.share`. Keeping the dependency one-way stops Share from becoming a hub that knows about
|
|
228
|
+
request handling.
|
|
229
|
+
- **One assembly point.** All shared instances are wired in `createAsync`; a resolver never
|
|
230
|
+
constructs a broker/dispatcher/client of its own. If a resolver needs a new shared instance, add it
|
|
231
|
+
to Share (in `createAsync` + a property), don't construct it ad hoc.
|
|
232
|
+
|
|
233
|
+
## 5. Naming
|
|
234
|
+
|
|
235
|
+
- **One Share per API/endpoint**, named `<Role>GraphqlShare` (e.g. `CustomerGraphqlShare`,
|
|
236
|
+
`AdminGraphqlShare`). When several endpoints share the same set of dependencies, factor a common
|
|
237
|
+
app-level Share base and let each endpoint's Share extend it (add only its extras).
|
|
238
|
+
- **Properties are named for the instance's role** — `subscriptionBroker`, `workerDispatcher`,
|
|
239
|
+
`permissionService`. Arrays are plural; single instances singular. Avoid the denied vague
|
|
240
|
+
identifiers (`data` / `info` / `list` / `manager` …).
|
|
241
|
+
|
|
242
|
+
## 6. Testing
|
|
243
|
+
|
|
244
|
+
Because every dependency is **constructor-injected**, a Share is trivial to unit-test and, more
|
|
245
|
+
importantly, resolvers become testable: a test builds a Share (or a plain `context.share` stand-in)
|
|
246
|
+
with **fakes**, and asserts the resolver used them.
|
|
247
|
+
|
|
248
|
+
- Construct the Share via `create({ ... })` with **fake** deps (a fake dispatcher, a fake broker) —
|
|
249
|
+
never build a real broker/queue/client in a test.
|
|
250
|
+
- Assert the resolver reads/uses the shared instance it should (e.g. the fake `workerDispatcher`
|
|
251
|
+
received the expected dispatch).
|
|
252
|
+
- Do not exercise `createAsync` against real infrastructure in unit tests; that is integration
|
|
253
|
+
territory.
|
|
254
|
+
|
|
255
|
+
## Finishing checklist
|
|
256
|
+
|
|
257
|
+
- [ ] Everything on Share is **shared, concurrency-safe, and build-once** — it passes the admission test ([§2](#2-what-belongs-in-share-the-admission-test)); nothing request-scoped or per-user is on it.
|
|
258
|
+
- [ ] No business logic on Share — it only **holds and exposes** shared instances.
|
|
259
|
+
- [ ] Class shape is constructor (pure assignment) → `create` → `createAsync`, with all deps **injected via the constructor** ([§3](#3-the-class-shape-constructor-di--create--createasync)).
|
|
260
|
+
- [ ] The shared instances are assembled in **one place** (`createAsync`), built once at boot; resolvers reach them via `context.share.<name>` and never construct their own ([§4](#4-lifecycle--wiring-boot--context--resolver)).
|
|
261
|
+
- [ ] Dependency direction is one-way: Context → Share; Share imports no resolver/Context.
|
|
262
|
+
- [ ] Tests inject **fakes** through the constructor; no real broker/queue/client is built in a unit test ([§6](#6-testing)).
|
|
@@ -0,0 +1,194 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: hor-resolver-validator
|
|
3
|
+
description: >
|
|
4
|
+
Implement input-validation classes for GraphQL resolvers under
|
|
5
|
+
app/validator/forResolver/<endpoint>/{queries,mutations}/, extending BaseInputValidator and
|
|
6
|
+
delegating value checks to @openreachtech/mentsu-value-inspector. Use this skill whenever the
|
|
7
|
+
user asks to validate a resolver's input — presence, number, integer, pagination, enum/keyword
|
|
8
|
+
checks — via a `*InputValidator` class whose failing predicate raises the matching InvalidXxx
|
|
9
|
+
error.
|
|
10
|
+
---
|
|
11
|
+
|
|
12
|
+
# Resolver Validator
|
|
13
|
+
|
|
14
|
+
A skill for writing the **`*InputValidator` classes** that GraphQL resolvers use to reject bad
|
|
15
|
+
input. A validator **extends `BaseInputValidator`** and declares a list of
|
|
16
|
+
`[predicate, ErrorConstructor]` entries; the base runs them and, for the first predicate that
|
|
17
|
+
returns `false`, raises that entry's error. Per-value checks (presence / number / integer /
|
|
18
|
+
positive-integer …) are delegated to **`@openreachtech/mentsu-value-inspector`**, imported directly
|
|
19
|
+
(it is a published module — use it from the package, not a local wrapper).
|
|
20
|
+
|
|
21
|
+
> Directory layout differs from the current repo on purpose — this is the target convention. The
|
|
22
|
+
> support classes (`BaseInputValidator`, `NormalPaginationInputValidator`) come from the renchan
|
|
23
|
+
> framework / shared layer; the inspectors come from the `@openreachtech/mentsu-value-inspector`
|
|
24
|
+
> module.
|
|
25
|
+
|
|
26
|
+
## Grand principle: declare `[predicate, error]` entries, let the base run them
|
|
27
|
+
|
|
28
|
+
A validator is a `BaseInputValidator` subclass whose only required override is
|
|
29
|
+
**`generateValidationEntries()`** — it returns `Array<[() => boolean, RenchanGraphqlErrorCtor]>`.
|
|
30
|
+
Each entry pairs a **predicate** (returns `true` when that rule passes) with the **error
|
|
31
|
+
constructor** to raise when it fails. The base holds `this.input` and `this.errorHash`, iterates the
|
|
32
|
+
entries, and throws the error of the first failing predicate.
|
|
33
|
+
|
|
34
|
+
- **Predicates are small `isValidX()` methods** that read `this.input` and return a boolean. They
|
|
35
|
+
use the inspector classes and/or composed sub-validators — they never throw.
|
|
36
|
+
- **Errors come from `this.errorHash`** (the resolver's error hash, injected into the validator), so
|
|
37
|
+
each `InvalidXxx` maps to the resolver's `errorCodeHash` code.
|
|
38
|
+
- **Reuse shared validators** (e.g. pagination) by composing them inside entries, not by
|
|
39
|
+
re-implementing limit/offset/sort.
|
|
40
|
+
|
|
41
|
+
```js
|
|
42
|
+
import {
|
|
43
|
+
IntegerValueInspector,
|
|
44
|
+
} from '@openreachtech/mentsu-value-inspector'
|
|
45
|
+
|
|
46
|
+
import BaseInputValidator from '../../BaseInputValidator.js'
|
|
47
|
+
import PaginationInputValidator from '../../NormalPaginationInputValidator.js'
|
|
48
|
+
|
|
49
|
+
const VALID_SORT_COLUMNS = [
|
|
50
|
+
'labelName',
|
|
51
|
+
'fieldPath',
|
|
52
|
+
]
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Class for validating input of the emailInsertableVariables query
|
|
56
|
+
*
|
|
57
|
+
* @extends {BaseInputValidator<ErrorHash, EmailInsertableVariablesInput>}
|
|
58
|
+
*/
|
|
59
|
+
export default class EmailInsertableVariablesInputValidator extends BaseInputValidator {
|
|
60
|
+
/**
|
|
61
|
+
* Generate validation entries
|
|
62
|
+
*
|
|
63
|
+
* @override
|
|
64
|
+
* @returns {Array<[() => boolean, RenchanGraphqlErrorCtor]>}
|
|
65
|
+
*/
|
|
66
|
+
generateValidationEntries () {
|
|
67
|
+
const paginationInputValidator = this.createPaginationInputValidator()
|
|
68
|
+
|
|
69
|
+
return [
|
|
70
|
+
[
|
|
71
|
+
() => paginationInputValidator.isValidLimit(),
|
|
72
|
+
this.errorHash.InvalidPaginationLimit,
|
|
73
|
+
],
|
|
74
|
+
[
|
|
75
|
+
() => paginationInputValidator.isValidOffset(),
|
|
76
|
+
this.errorHash.InvalidPaginationOffset,
|
|
77
|
+
],
|
|
78
|
+
[
|
|
79
|
+
() => paginationInputValidator.isValidSort(),
|
|
80
|
+
this.errorHash.InvalidPaginationSort,
|
|
81
|
+
],
|
|
82
|
+
[
|
|
83
|
+
() => this.isValidKeyword(),
|
|
84
|
+
this.errorHash.InvalidKeyword,
|
|
85
|
+
],
|
|
86
|
+
[
|
|
87
|
+
() => this.isValidOriginObjectCategoryId(),
|
|
88
|
+
this.errorHash.InvalidOriginObjectCategoryId,
|
|
89
|
+
],
|
|
90
|
+
]
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
// ... isValidX() predicates + create*Inspector helpers (see validator-pattern.md) ...
|
|
94
|
+
}
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
## Directory & naming
|
|
98
|
+
|
|
99
|
+
- **Validators:** `app/validator/forResolver/<endpoint>/<operation-kind>/<Operation>InputValidator.js`
|
|
100
|
+
- `<endpoint>` tracks the GraphQL endpoint the resolver belongs to (`user`, `customer`, `admin`,
|
|
101
|
+
…) — it changes per endpoint.
|
|
102
|
+
- `<operation-kind>` is **`queries`** or **`mutations`**.
|
|
103
|
+
- e.g. `app/validator/forResolver/user/queries/EmailInsertableVariablesInputValidator.js`,
|
|
104
|
+
`app/validator/forResolver/user/mutations/CreateFooInputValidator.js`.
|
|
105
|
+
- **Shared under `app/validator/forResolver/`:** `BaseInputValidator.js` and reusable validators such
|
|
106
|
+
as `NormalPaginationInputValidator.js` (default-exported, commonly imported as
|
|
107
|
+
`PaginationInputValidator`).
|
|
108
|
+
- **Inspectors:** imported directly from `@openreachtech/mentsu-value-inspector` (`ValueInspector` /
|
|
109
|
+
`NumberValueInspector` / `IntegerValueInspector`)
|
|
110
|
+
([inspector-api.md](./references/inspector-api.md)).
|
|
111
|
+
- Class name = `<Operation>InputValidator` (PascalCase, matching the resolver's `schema`).
|
|
112
|
+
|
|
113
|
+
## Predicates & the inspector
|
|
114
|
+
|
|
115
|
+
Each rule is an `isValidX()` method. For value-shape checks, build an inspector via a small
|
|
116
|
+
`create*ValueInspector({ value })` helper and call its methods:
|
|
117
|
+
|
|
118
|
+
```js
|
|
119
|
+
isValidOriginObjectCategoryId () {
|
|
120
|
+
const inspector = this.createIntegerValueInspector({
|
|
121
|
+
value: this.input.originObjectCategoryId,
|
|
122
|
+
})
|
|
123
|
+
|
|
124
|
+
// the module has no isPositiveInteger — compose it (string-tolerant *Like)
|
|
125
|
+
return inspector.isIntegerLike()
|
|
126
|
+
&& inspector.isPositiveNumberLike()
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
createIntegerValueInspector ({
|
|
130
|
+
value,
|
|
131
|
+
}) {
|
|
132
|
+
return IntegerValueInspector.create({
|
|
133
|
+
value,
|
|
134
|
+
})
|
|
135
|
+
}
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
- **Optional field idiom:** return `true` early when the field is absent, then check the shape —
|
|
139
|
+
e.g. `if (!keyword) { return true }` before `return typeof keyword === 'string'`.
|
|
140
|
+
- The `*Like` methods accept numeric **strings**, which is what you want at the GraphQL boundary;
|
|
141
|
+
there is no `isPositiveInteger`, so compose it ([inspector-api.md](./references/inspector-api.md)).
|
|
142
|
+
|
|
143
|
+
## Error hash & resolver wiring
|
|
144
|
+
|
|
145
|
+
The resolver declares the codes and passes its `errorHash` into the validator:
|
|
146
|
+
|
|
147
|
+
```js
|
|
148
|
+
/** @override */
|
|
149
|
+
static get errorCodeHash () {
|
|
150
|
+
return {
|
|
151
|
+
...super.errorCodeHash,
|
|
152
|
+
|
|
153
|
+
InvalidKeyword: '400.C001.001',
|
|
154
|
+
InvalidOriginObjectCategoryId: '400.C001.002',
|
|
155
|
+
InvalidPaginationLimit: '400.C001.003',
|
|
156
|
+
InvalidPaginationOffset: '400.C001.004',
|
|
157
|
+
InvalidPaginationSort: '400.C001.005',
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/** @override */
|
|
162
|
+
async resolve ({
|
|
163
|
+
variables: {
|
|
164
|
+
input,
|
|
165
|
+
},
|
|
166
|
+
context,
|
|
167
|
+
}) {
|
|
168
|
+
EmailInsertableVariablesInputValidator
|
|
169
|
+
.create({
|
|
170
|
+
input,
|
|
171
|
+
errorHash: this.errorHash,
|
|
172
|
+
})
|
|
173
|
+
.validate()
|
|
174
|
+
|
|
175
|
+
// ... input is now trusted ...
|
|
176
|
+
}
|
|
177
|
+
```
|
|
178
|
+
|
|
179
|
+
`create({ input, errorHash })` and the run entrypoint (shown as `validate()`) are provided by
|
|
180
|
+
`BaseInputValidator` — match the actual base API in your framework.
|
|
181
|
+
|
|
182
|
+
## Testing
|
|
183
|
+
|
|
184
|
+
Predicates are pure booleans, so unit-test them (and `generateValidationEntries`) with the
|
|
185
|
+
`hoc-jest` skill: feed inputs, assert `isValidX()` results and that invalid input
|
|
186
|
+
raises the expected `InvalidXxx`.
|
|
187
|
+
|
|
188
|
+
## Detail files
|
|
189
|
+
|
|
190
|
+
- [inspector-api.md](./references/inspector-api.md) — the `@openreachtech/mentsu-value-inspector`
|
|
191
|
+
API (strict vs `*Like`, presence / number / integer, composing positive-integer, cheat sheet)
|
|
192
|
+
- [validator-pattern.md](./references/validator-pattern.md) — the full validator template
|
|
193
|
+
(`generateValidationEntries`, predicates, inspector helpers, composed pagination, typedefs), the
|
|
194
|
+
`BaseInputValidator` contract, resolver wiring, and testing
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
# mentsu-value-inspector API
|
|
2
|
+
|
|
3
|
+
The value-inspection module validators use for per-value checks. Referenced from
|
|
4
|
+
[SKILL.md](../SKILL.md). Full docs: the package README on npm.
|
|
5
|
+
|
|
6
|
+
Validators **import directly from the module** — it is published, so use it from the package (do not
|
|
7
|
+
hand-roll a local wrapper):
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm install @openreachtech/mentsu-value-inspector@1.1.0
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
```js
|
|
14
|
+
import {
|
|
15
|
+
IntegerValueInspector,
|
|
16
|
+
} from '@openreachtech/mentsu-value-inspector'
|
|
17
|
+
|
|
18
|
+
const inspector = IntegerValueInspector.create({
|
|
19
|
+
value: this.input.originObjectCategoryId,
|
|
20
|
+
})
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
## Classes & hierarchy
|
|
24
|
+
|
|
25
|
+
Three classes, each a named export of the package root; each subclass inherits all its parents'
|
|
26
|
+
methods, so instantiate the most specific one you need:
|
|
27
|
+
|
|
28
|
+
```
|
|
29
|
+
ValueInspector // presence checks
|
|
30
|
+
└── NumberValueInspector // + number checks + normalizeValue()
|
|
31
|
+
└── IntegerValueInspector // + integer / safe-integer checks
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
## strict vs `*Like`
|
|
35
|
+
|
|
36
|
+
Every number/integer check comes in two flavors:
|
|
37
|
+
|
|
38
|
+
| Flavor | Reads | Accepts numeric strings | Example |
|
|
39
|
+
| :-- | :-- | :-- | :-- |
|
|
40
|
+
| **strict** | the raw value | No | `isNumber('3.14')` → `false` |
|
|
41
|
+
| **`*Like`** | the normalized value | Yes | `isNumberLike('3.14')` → `true` |
|
|
42
|
+
|
|
43
|
+
`*Like` normalizes first (number/string accepted): `'100'` → `100`, while `true` / `1000n` / `{}` /
|
|
44
|
+
`'abc'` / `null` / `undefined` → `null` and every `*Like` returns `false`. **`''` normalizes to
|
|
45
|
+
`0`** (so `isIntegerLike('')` → `true`). At the GraphQL/REST boundary prefer the string-tolerant
|
|
46
|
+
(`*Like`) checks, since inputs often arrive as strings.
|
|
47
|
+
|
|
48
|
+
## Presence (`ValueInspector`)
|
|
49
|
+
|
|
50
|
+
| Method | Returns |
|
|
51
|
+
| :-- | :-- |
|
|
52
|
+
| `isNull()` | `value === null` |
|
|
53
|
+
| `isUndefined()` | `value === undefined` |
|
|
54
|
+
| `isNullish()` | `null` or `undefined` |
|
|
55
|
+
| `isDefined()` | not `undefined` |
|
|
56
|
+
| `isPresent()` | neither `null` nor `undefined` |
|
|
57
|
+
|
|
58
|
+
## Numbers (`NumberValueInspector`)
|
|
59
|
+
|
|
60
|
+
| Method | Reads | Meaning |
|
|
61
|
+
| :-- | :-- | :-- |
|
|
62
|
+
| `isNumber()` / `isNumberLike()` | raw / norm | finite number |
|
|
63
|
+
| `isPositiveNumber()` / `isPositiveNumberLike()` | raw / norm | finite `> 0` |
|
|
64
|
+
| `isNegativeNumber()` / `isNegativeNumberLike()` | raw / norm | finite `< 0` |
|
|
65
|
+
| `isZero()` | raw | `value === 0` (incl. `-0`) |
|
|
66
|
+
| `isNaN()` / `isFinite()` / `isInfinite()` | raw | NaN / finite / ±Infinity |
|
|
67
|
+
| `normalizeValue()` | — | normalized `number`, or `null` (lazy + memoized) |
|
|
68
|
+
|
|
69
|
+
## Integers (`IntegerValueInspector`)
|
|
70
|
+
|
|
71
|
+
| Method | Reads | Meaning |
|
|
72
|
+
| :-- | :-- | :-- |
|
|
73
|
+
| `isInteger()` / `isIntegerLike()` | raw / norm | integer |
|
|
74
|
+
| `isSafeInteger()` / `isSafeIntegerLike()` | raw / norm | safe integer (`±(2^53 − 1)`) |
|
|
75
|
+
|
|
76
|
+
## There is no `isPositiveInteger` — compose it
|
|
77
|
+
|
|
78
|
+
The module has no positive-integer method; combine an integer check with a positive check on the same
|
|
79
|
+
inspector (string-tolerant `*Like` at the boundary):
|
|
80
|
+
|
|
81
|
+
```js
|
|
82
|
+
const inspector = IntegerValueInspector.create({
|
|
83
|
+
value: this.input.originObjectCategoryId,
|
|
84
|
+
})
|
|
85
|
+
|
|
86
|
+
// "positive integer" (accepts '42')
|
|
87
|
+
inspector.isIntegerLike()
|
|
88
|
+
&& inspector.isPositiveNumberLike()
|
|
89
|
+
|
|
90
|
+
// for an id, also require it to be safe:
|
|
91
|
+
inspector.isSafeIntegerLike()
|
|
92
|
+
&& inspector.isPositiveNumberLike()
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
## Behavior cheat sheet
|
|
96
|
+
|
|
97
|
+
| Input | `isNumber` | `isNumberLike` | `isInteger` | `isIntegerLike` | `isSafeIntegerLike` |
|
|
98
|
+
| :-- | :-: | :-: | :-: | :-: | :-: |
|
|
99
|
+
| `42` | ✅ | ✅ | ✅ | ✅ | ✅ |
|
|
100
|
+
| `'42'` | ❌ | ✅ | ❌ | ✅ | ✅ |
|
|
101
|
+
| `3.14` | ✅ | ✅ | ❌ | ❌ | ❌ |
|
|
102
|
+
| `'3.14'` | ❌ | ✅ | ❌ | ❌ | ❌ |
|
|
103
|
+
| `Infinity` / `NaN` | ❌ | ❌ | ❌ | ❌ | ❌ |
|
|
104
|
+
| `9007199254740993` | ✅ | ✅ | ✅ | ✅ | ❌ |
|
|
105
|
+
| `1000n` / `true` | ❌ | ❌ | ❌ | ❌ | ❌ |
|
|
106
|
+
| `null` / `undefined` | ❌ | ❌ | ❌ | ❌ | ❌ |
|
|
107
|
+
|
|
108
|
+
## Recipes for predicates
|
|
109
|
+
|
|
110
|
+
- **Positive integer (id):** `inspector.isIntegerLike() && inspector.isPositiveNumberLike()`
|
|
111
|
+
(or `isSafeIntegerLike()` for ids).
|
|
112
|
+
- **Optional field:** return `true` early when absent (`if (!value) { return true }`), then check.
|
|
113
|
+
- **Required present:** `inspector.isPresent()`.
|
|
114
|
+
- **Read the coerced number after validating:** `inspector.normalizeValue()` — don't re-parse.
|
|
115
|
+
- **Guard empty string** for required numeric fields, since `''` normalizes to `0`.
|