Skip to content

feat: GraphQL backend over the kits, one endpoint - #1

Merged
BackStacked merged 2 commits into
mainfrom
claude/graphql-backend-fa2c1c
Oct 14, 2026
Merged

BackStacked merged 2 commits into
mainfrom
claude/graphql-backend-fa2c1c

Conversation

@BackStacked

@BackStacked BackStacked commented Oct 14, 2026 •

Copy link
Copy Markdown
Contributor

What and why

Before: there was no backend. Each kit was a library, report-kit and invoice-kit had their own HTTP handlers, and db-kit held a partial GraphQL layer waiting to move.

After: this repo is the backend. One endpoint, POST /graphql (GraphQL Yoga on Fastify, plus GET /health), serves 137 queries and mutations across:

  • auth and users
  • organisation
  • catalog
  • lab: patients, bookings, samples, reports
  • billing
  • books
  • finance: GST, receivables, settlements, cash and bank, locks, exports
  • documents: report and invoice PDFs as base64, or HTML

All seven kits are used as internal libraries behind controllers and services.

How:

  • Layers. src/schema (SDL per module) → src/controllers (authorize, map) → src/services (kit calls with (actor, input)) → kits. src/container.ts builds db-kit with finance-kit's posting hook, auth-kit on db-kit's stores (Redis when configured), and the services.
  • Safety. orgId and actorId come only from the session. Every id is checked against the organisation, and single records are authorised against their own branch. Domain errors map to stable codes and unknown errors are masked. Queries are limited to depth 12 and lists to 200 items; nested fields use per-request batch loaders.
  • Docs. docs/ covers everything from A to Z with Mermaid diagrams (architecture, request sequence, state diagrams, ER diagram), plus runnable examples and an API reference generated from the schema and controllers (npm run docs:reference, checked in CI).
  • Local-agent brief. docs/LOCAL-AGENT-BRIEF.md lists the steps that need an org owner or a local machine: releases, package access, repo protection, the db-kit 2.0.0 cleanup, deployment and the CA review.
  • Kit installation. CI builds the kits from their public repos at the refs in kits.json (npm run kits), because not every kit is on the registry yet.

Type

  • Bug fix
  • Feature
  • Test data (ranges, units, prices)
  • Docs
  • Refactor / tooling

Screenshots

Nothing visual changes: documents are rendered by report-kit and invoice-kit, unchanged.

Checklist

  • On a branch (not main)
  • npm run typecheck passes
  • The repo's tests pass: 68 passed, 0 failed on PostgreSQL locally. MongoDB binaries could not be downloaded in the cloud session, so CI is the first run on Mongo.
  • Docs updated (README, docs/, AGENTS.md, llms.txt, llms-full.txt)
  • Contract: the client's GraphQL operations are in feat: GraphQL client for mgpl-diagnostics/api client#6 (all 138 validated against this schema)
  • No real patient data, contact details or signatures: fixtures use fictional names

🤖 Generated with Claude Code

https://claude.ai/code/session_01BA5Vx77ihDsLiZpyg5duJV

GraphQL Yoga on Fastify at POST /graphql (plus GET /health) with
schema → controller → service layers over auth-kit, db-kit, catalog-kit,
report-kit, invoice-kit, tally-kit and finance-kit as internal libraries:
137 queries and mutations for auth, organisation, catalog, lab, billing,
books, finance and documents. Branch-aware permissions, tenant isolation,
error codes, loaders, depth limit. End-to-end tests over HTTP on
PostgreSQL and MongoDB, A-to-Z docs with Mermaid diagrams, examples, a
generated API reference and a brief for the local agent.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01BA5Vx77ihDsLiZpyg5duJV
BackStacked added a commit to mgpl-diagnostics/client that referenced this pull request Oct 14, 2026
## What and why

Before: the client could only call the report and invoice HTTP API
(`createMgplClient`). Nothing in it could reach patients, bookings,
billing, books or finance.

After: `createMgplGraphQLClient({ url: "/graphql" })` has a typed method
for every query and mutation of the new backend's single GraphQL
endpoint (mgpl-diagnostics/api#1), 138 operations in all. They are
grouped as `auth`, `users`, `organization`, `catalog`, `patients`,
`doctors`, `bookings`, `samples`, `reports`, `invoices`, `books`, `gst`,
`receivables`, `settlements`, `cash`, `bank`, `periods`, `exports` and
`documents`.
- `auth.login` keeps the session token.
- Errors throw `MgplGraphQLError` with a stable `code`.
- `documentBlob` / `csvBlob` turn rendered PDFs and CSV exports into
Blobs.

The package still has zero dependencies. The operations are plain
strings, and I validated all 138 against the api's schema: 0 invalid,
and every root field is covered.

How:
- `src/graphql/` holds the transport, the operations with their
fragments, the client and the types.
- There is a new `"/graphql"` entry with the types flat. The main entry
re-exports the client, with the types under the `Gql` namespace, because
names like `Patient` already exist for the HTTP API.
- `docs/graphql.md` gains a Mermaid sequence diagram and a table mapping
each method to its operation.
- README, AGENTS.md and llms files are updated, and the version goes to
1.1.0.
- The existing HTTP client is unchanged.

## Type

- [ ] Bug fix
- [x] Feature
- [ ] Test data (ranges, units, prices)
- [x] Docs
- [ ] Refactor / tooling

## Screenshots

No UI (headless SDK).

## Checklist

- [x] On a branch (not `main`)
- [x] `npm run typecheck` passes
- [x] `npm run build` passes (dist/index.js, dist/graphql.js,
dist/react.js)
- [x] Docs updated (README, `docs/graphql.md`, `docs/api.md`, AGENTS.md,
llms.txt)
- [x] Contract: mirrors mgpl-diagnostics/api#1's schema. The api's test
suite will validate these operations once it pins this version.
- [x] **No real patient data, contact details or signatures**

🤖 Generated with [Claude Code](https://claude.com/claude-code)

https://claude.ai/code/session_01BA5Vx77ihDsLiZpyg5duJV

Co-authored-by: Claude <noreply@anthropic.com>
Pins @mgpl-diagnostics/client at the merge of client#6 (1.1.0).

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01BA5Vx77ihDsLiZpyg5duJV
BackStacked added a commit to mgpl-diagnostics/.github that referenced this pull request Oct 14, 2026
## What and why

Before: `protect-repos.mjs` did not know the new `api` repo, so running
it would not require `ci` there. The org profile still listed api as
"coming soon" and said PDFs use REST.

After: `CHECKS` includes `api: ["ci"]`. The profile links the api
(mgpl-diagnostics/api#1) and says everything goes through the one
GraphQL endpoint, PDFs included. `llms-full.txt` is regenerated.

Running `node scripts/protect-repos.mjs api` needs an org owner; it is
step 1 of the api's `docs/LOCAL-AGENT-BRIEF.md`.

## Type

- [ ] Bug fix
- [ ] Feature
- [ ] Test data (ranges, units, prices)
- [x] Docs
- [x] Refactor / tooling

## Checklist

- [x] On a branch (not `main`)
- [x] Docs updated (profile, llms-full.txt)
- [x] **No real patient data, contact details or signatures**

🤖 Generated with [Claude Code](https://claude.com/claude-code)

https://claude.ai/code/session_01BA5Vx77ihDsLiZpyg5duJV

---
_Generated by [Claude
Code](https://claude.ai/code/session_01BA5Vx77ihDsLiZpyg5duJV)_

Co-authored-by: Claude <noreply@anthropic.com>
@BackStacked
BackStacked merged commit 85ef249 into main Oct 14, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants