caddygraphqlsqlite

package module
v1.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 26, 2026 License: Apache-2.0 Imports: 3 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

GraphQL over a caddy-sqlite database

ci

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. graphql names a database and borrows its pool. Nothing is opened twice, so a readonly schema 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 expose is invisible, in introspection and at runtime.
  • Types come from the database. Columns and types come from PRAGMA table_info; relationships come from PRAGMA 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 }
  • relationships declares what the foreign key graph cannot infer. table is the remote, mapping is local column to remote column, and more than one pair is a composite link joined with AND. many: true makes it a list.
  • fields adds a computed column. Its SQL binds every column of the row by name, so :body and :author_id are the row's own values.
  • role is per table. A role with no entry sees nothing. select limits the columns; insert, update and delete are the writes, each with its own columns and filter.
  • 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_notes is schema_table; the type is AppNotes.
  • A view is exposed read-only: it has no mutation fields, whatever writes says.
  • where is 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

  1. 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.
  2. 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.
  3. Subscriptions are SSE only. There is no WebSocket transport.
  4. The role is whatever the placeholder resolves to. Put an auth handler in front; the plugin trusts the header it is told to read.

Documentation

Overview

Package caddygraphqlsqlite registers the graphql handler, which serves a GraphQL endpoint over databases declared by a database plugin (caddy-sqlite, caddy-pgsql or caddy-mysql).

Directories

Path Synopsis
pkg

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL