Query Resolver
A skill for writing the *QueryResolver classes that answer a GraphQL Query field. A query
resolver extends BaseQueryResolver (from @openreachtech/renchan), is read-only (it never
opens a transaction — that is the mutation side's job), and always follows the same shape: a schema
getter that names the GraphQL field, an errorCodeHash, and a resolve() that validates the
input, reads data, throws domain errors, and formats the response.
Examples use a made-up
usersdomain (aUsermodel with aUserProfile/Department/Rolearound it) on theadminendpoint. They are placeholders — swap in the resource, fields, and endpoint of the query you are writing.
This skill covers the resolver class itself. Two related skills own the pieces it delegates to:
- The
*InputValidatorit calls belongs tohor-resolver-validator— this skill only shows the wiring, not the validator internals. - The unit tests belong to
hoc-jest— see testing.md for the resolver-specific shape.
This is the Query side. A Mutation resolver is a different template (it wraps a transaction callback and returns save results); do not copy this skeleton for a write.
Core principle: a query resolver is a filled-in template
Every query resolver has the same skeleton — schema getter → errorCodeHash → resolve() →
validation wiring → finders → formatResponse() → error creators → a trailing block of @typedefs.
Keep the skeleton identical so review attention goes straight to the query-specific differences
(what it reads and how it shapes the result). Do not write it cleverly; lean toward the layout of
the existing resolvers.
resolve() reads as a fixed pipeline:
- Validate the input (
validateInput()returns an error ornull;throwit when present). - Read the data (finders:
findX()/countX()), passing already-trusted input. - Throw domain errors for not-found / empty results, via
this.errorHash.Xxx.create(). - Format the result to the GraphQL schema shape (
formatResponse()), andreturnit.
js/** @override */ async resolve ({ variables: { input, }, }) { const validationError = this.validateInput({ input, }) if (validationError) { throw validationError } const user = await this.findUser({ userId: input.userId, }) if (!user) { throw this.errorHash.UserNotFound.create() } return this.formatResponse({ user, }) }
- No transaction. Queries only read. Reach for
.findOne/.findByPk/.findAll/.count; neverdb.transaction(...)here. resolve()orchestrates; the small methods do the work. EachfindX/buildWhereClause/formatResponsehas one job and is unit-testable in isolation. Do not inline a 60-line find or a giant response object intoresolve().- The full member-by-member template with JSDoc and the typedef block is in anatomy.md.
1. Directory, class name, and schema linkage
One resolver = one file. Placement encodes the endpoint and whether it is real or a stub:
server/graphql/resolvers/<endpoint>/<actual|stub>/queries/<Operation>QueryResolver.js
-
<endpoint>is the GraphQL endpoint the resolver serves:user,customer,admin,portal,jobs. It maps to a*GraphqlServerEngine(e.g.server/graphql/AdminGraphqlServerEngine.js). -
<actual|stub>—actualholds the real business logic;stubreturns fixed fake data for the frontend to develop against (errors-and-context.md). -
Class name =
<Operation>QueryResolver(PascalCase), default-exported, one class per file. -
Auto-loaded — no manual registration. The engine loads every file under
.../queries/by directory (actualResolversPathin the engine config). Adding the file is enough; there is no index to edit. -
schemagetter = the GraphQLQueryfield name (camelCase). It must match a field declared inserver/graphql/schemas/<endpoint>/*.graphql, whoseInput!/Result!types become the resolver'sinputand return shape:graphql# server/graphql/schemas/admin/003-user.graphql type Query { users(input: UsersInput!): UsersResult! }js/** @override */ static get schema () { return 'users' }
2. Member order
Declare members in this fixed order (matches the existing resolvers and the backend guideline):
static get schema ()— the GraphQL field name.@override.static get errorCodeHash ()— spread...super.errorCodeHash, then this resolver's codes (§4).async resolve ({ variables, context })— the entrypoint.@override.validateInput ({ input })— runs the validator, returns the error ornull.createInputValidator ({ input, errorHash })— factory for the*InputValidator.- Finders and helpers, in call order (
findX/countX/buildWhereClause/create*). Place each helper right after its caller; unrelated helpers go in dictionary order. formatResponse ({ ... })— shape the GraphQL result.- Error creators (
createXxxNotFoundError), when domain errors are built through helpers.
After the class, a block of @typedefs (ErrorCodeHash, the Input / Result aliases, ErrorHash,
RenchanGraphqlErrorCtor, and any associated-entity types). See anatomy.md.
3. Reading data
Finders are thin wrappers over the Sequelize models; keep query-shape logic (where clauses,
includes, pagination) in their own small methods so resolve() stays a pipeline.
- Single record:
Model.findByPk(id, { include })/Model.findOne({ where, include }), then a not-found guard that throws a domain error. - List with pagination: the canonical trio is
buildWhereClause()→countX({ whereClause })(fortotalRecords) →findX({ whereClause, pagination })(withoffset/limit/order). - Associations: pass an
includetree of models; deep values are pulled out informatResponsewithFieldPathValueExtractor.
The where-clause / pagination / include / extractor patterns are in data-access.md.
4. errorCodeHash and domain errors
errorCodeHash maps each error name to a string code; .create() turns those into the
this.errorHash.<Name> constructors the resolver throws. Spread the parent first:
js/** @override */ static get errorCodeHash () { return { ...super.errorCodeHash, // Invalid Input Errors (203 prefix) InvalidUserId: '203.Q002.001', // Database Errors (204 prefix) UserNotFound: '204.Q002.001', } }
- The code's shape and its categories are settled by
hoc-error-codes. What a query resolver decides is which category each of its own errors takes:203for what the validator rejects,204for what the database does not hold. The framework's own codes are declared by the engine below, and are never redeclared here. - Input-shape errors (
InvalidXxx) are raised by the validator via the sharederrorHash, so their names must exist here. Domain errors (XxxNotFound,NoXxxFound) are thrown fromresolve()withthis.errorHash.Xxx.create()(optionally through acreateXxxError()helper).
The families, the validator↔errorHash contract, and error-creator helpers are in errors-and-context.md.
5. Input validation wiring
The resolver does not validate inline — it delegates to a *InputValidator
(hor-resolver-validator) through two small methods:
jsvalidateInput ({ input, }) { const validator = this.createInputValidator({ input, }) return validator.validateInput() } createInputValidator ({ input, errorHash = this.errorHash, }) { return UsersInputValidator.create({ input, errorHash, }) }
validateInput()returns the error ornull;resolve()throws it when present (do not throw from insidevalidateInput). The validator is fed the resolver's ownerrorHash, so itsInvalidXxxnames resolve to the codes declared in §4.- A resolver that takes no input (or only auth from
context) has no validator — see the minimaldepartments/userSummaryresolvers in anatomy.md.
6. Formatting the response
formatResponse() is the only place that builds the GraphQL output object. It maps model
entities to the schema's field names — never returns raw model instances.
- Rename model columns to schema fields (
user.id→userId), map arrays to their output shape, and echo pagination (limit/offset/sort/totalRecords) for lists. - On PostgreSQL,
BIGINTids arrive as strings, so wrap each one inNumber()before it fills an SDLIntfield (a nullable one is branched first, becauseNumber(null)is0). - Read deep association values with
FieldPathValueExtractor; guard optional relations and default missing scalars (?? ''/?? null) rather than leakingundefined(data-access.md).
7. Testing
Each public member is unit-tested with the hoc-jest skill: a top-level describe per
member, test.each cases, and Resolver.create() with no args. #resolve() gets both the
happy path and a throws-case per error code; .get:schema asserts the field name. The
resolver-specific structure (mocking the validator, DB-backed finder cases, formatResponse with
built model instances) is in testing.md.
Detail files
- anatomy.md — the full canonical resolver (every member with JSDoc, the
minimal no-input variant, and the trailing
@typedefblock) (§2) - data-access.md — finders,
buildWhereClause, the count+findAll pagination trio, associationincludetrees, modelsubquery(), andFieldPathValueExtractor(§3, §6) - errors-and-context.md — the errorCodeHash prefix families, the
validator↔errorHash contract, domain-error creators, reading auth/providers off
context, and actual-vs-stub resolvers (§1, §4) - testing.md — the member-by-member test layout for a query resolver, building on the jest skill (§7)


