README
¶
GraphQL over a caddy-sqlite database
A Caddy v2 plugin that serves a GraphQL endpoint over a database this process
already declares with caddy-sqlite. It opens no file of its own: reads,
writes and subscriptions run through the sqlite plugin's cluster, so batching,
the query cache, migrations, readonly and the in-memory scratch are the ones
you already configured.
Cross-database
The same handler serves SQLite, PostgreSQL and MySQL unchanged. A backend
directive picks the engine; the SQL dialect (placeholders, identifier quoting,
JSON aggregation, introspection) is the only thing that differs, and it is
selected for you. A backend plugin registers itself with RegisterBackend, so
caddy-pgsql and caddy-mysql plug in without touching this code.
{
pgsql {
dsn postgres://user:pass@host:5432/db
database main { schema app }
}
}
example.com {
handle /graphql {
graphql {
backend pgsql # sqlite (default) | pgsql | mysql
database main
expose app.notes { reads anonymous user }
}
}
}
See SPEC-cross-db.md, SPEC-caddy-pgsql.md and SPEC-caddy-mysql.md for the
shared contract and the per-engine deltas.
Merging several backends
One endpoint can span engines: repeat a backend <name> { database <db> } block
and the tables merge into a single schema. Each root field is routed to the
backend it came from, with that backend's dialect.
example.com {
handle /graphql {
graphql {
backend sqlite { database main }
backend pgsql { database analytics }
role {http.request.header.X-Hasura-Role}
expose main.notes { reads anonymous user }
expose analytics.events { reads user }
}
}
}
Relationships are compiled into a single statement, so a relationship must stay within one backend; cross-backend links are omitted from the schema rather than emitted as unresolvable fields.
Scope
- One endpoint, one cluster.
graphqlnames adatabaseand borrows its pool. Nothing is opened twice, so areadonlyschema stays read-only and a write is batched with every other write to that schema. - Exposed tables are the whole policy. A table you do not
exposeis invisible, in introspection and at runtime. - Types come from the database. Columns and types come from
PRAGMA table_info; relationships come fromPRAGMA foreign_key_list, both directions, plus whatever the policy declares. - One statement per query. A nested selection compiles to a single
json_group_array(json_object(...))with correlated subqueries. No N+1. - Subscriptions are SSE. A subscription re-runs when the cluster's
generation for that schema moves, and streams each result as
text/event-stream.
Naming
graphql is the handler directive. database is the caddy-sqlite database it
serves, expose is a table, and role is the placeholder that names the
caller.
{
sqlite {
database main {
attach app /var/lib/caddy/app.db {
migrate /srv/www/migrations
}
}
}
}
example.com {
handle /graphql {
graphql {
database main
role {http.request.header.X-Hasura-Role}
policy /srv/www/policy
expose app.notes {
reads anonymous user
writes user
}
expose app.tags
}
}
}
Directives
graphql
| Key | Meaning |
|---|---|
backend <name> |
the database engine: sqlite (default), pgsql or mysql. |
database <name> |
the caddy-sqlite database to serve. Required. |
role <placeholder> |
resolves to the caller's role. Defaults to anonymous. |
policy <dir> |
directory of per-table policy files. Optional. |
expose <schema.table> |
a table to expose. Repeatable. |
expose
| Key | Meaning |
|---|---|
reads <role...> |
roles that may query and subscribe. |
writes <role...> |
roles that may insert, update and delete. |
A bare expose app.tags is read-only for every role. Naming writes opens
the table for insert, update and delete. In both keys an empty list means every
role, so writes with no roles is writes for everyone, and reads with no
roles is reads for everyone.
Policy
One file per exposed table, named after it with dots replaced by underscores:
app.notes is notes.yaml when the table is app_notes, or app_notes.yaml
for a cross-schema name. A missing file is full access.
relationships:
author:
table: app.authors
mapping:
author_id: id
comments:
table: app.comments
mapping:
id: note_id
many: true
fields:
body_len:
sql: "SELECT length(:body) FROM app.notes WHERE id = :id"
role:
anonymous:
select: [id, body]
user:
select: [id, body, author_id]
insert:
columns: [body]
update:
columns: [body]
filter:
author_id: { _eq: x-hasura-user-id }
status: { _eq: draft }
delete:
filter:
author_id: { _eq: x-hasura-user-id }
relationshipsdeclares what the foreign key graph cannot infer.tableis the remote,mappingis local column to remote column, and more than one pair is a composite link joined withAND.many: truemakes it a list.fieldsadds a computed column. Its SQL binds every column of the row by name, so:bodyand:author_idare the row's own values.roleis per table. A role with no entry sees nothing.selectlimits the columns;insert,updateanddeleteare the writes, each with its owncolumnsandfilter.- A bare
x-hasura-*word in a filter resolves from the request's claims, so a filter is per request.
Every table, column and schema a policy names is checked against the live
cluster at provision, so a typo fails caddy validate.
The API
query {
app_notes(limit: 10) {
id
body
app_authors { name }
comments { text }
}
}
mutation {
insert_app_notes_one(object: { body: "hi" }) { id body }
update_app_notes(where: { id: { _eq: 1 } }, _set: { body: "x" })
delete_app_notes(where: { id: { _eq: 1 } })
}
subscription {
app_notes { id body }
}
app_notesisschema_table; the type isAppNotes.- A view is exposed read-only: it has no mutation fields, whatever
writessays. whereis one comparison per column, flat:_eq,_neq,_gt,_gte,_lt,_lte,_like,_in.
Build
This module is nested in the caddy-sqlite repository and replaces it with the parent directory, so both build from the repository root:
sh gql/test/unit.sh # gofmt, vet, unit tests
sh gql/build.sh # image caddy-graphql:test
sh gql/test/e2e.sh # end-to-end against that image
Known caveats
- A view's dependencies are not tracked. A subscription on a view watches the view's own schema, so it does not move when the underlying table changes.
- A computed field costs a statement per row. It is the one place a selection can produce N+1, and only when you ask for the field.
- Subscriptions are SSE only. There is no WebSocket transport.
- The role is whatever the placeholder resolves to. Put an auth handler in front; the plugin trusts the header it is told to read.