GraphQL
Each database has a GraphQL schema generated from its tables. GraphQL is available on the Starter plan and above.
VaultDB accepts a small subset of GraphQL: one flat list of table queries or mutations per request. It does not support variables, fragments, aliases, nested selections, directives or subscriptions. For anything those would need, use the REST API or raw SQL.
Read the schema
curl https://api.vaultdb.io/api/databases/shop/graphql \
-H "Authorization: Bearer $VAULTDB_API_KEY"
Needs the Databases permission. Returns:
sdl: the schema in GraphQL schema language,types: one entry per table, with its type name, table name and field count,skipped: tables left out because their names are not valid GraphQL names (for example a table namedorder-items).
Each table becomes a type named in PascalCase (order_items becomes OrderItems). Column types map like this:
| SQLite type contains | GraphQL type |
|---|---|
INT |
Int |
REAL, FLOAT, DOUBLE or NUMERIC |
Float |
BOOL |
Boolean |
| anything else | String |
Run a query or mutation
Send the document as query with POST. This needs the Databases.edit permission, for queries as well as mutations.
curl -X POST https://api.vaultdb.io/api/databases/shop/graphql \
-H "Authorization: Bearer $VAULTDB_API_KEY" \
-H "Content-Type: application/json" \
-d '{ "query": "{ orders(limit: 10) { id customer total } }" }'
{ "data": { "orders": [{ "id": 7, "customer": "ada", "total": 120.5 }] } }
Queries
For every table the schema has two fields:
| Field | Returns |
|---|---|
<table>(limit: Int, offset: Int) |
A list of rows. limit defaults to 100 and is capped at 1,000. A list also stops at 16 MB of row data (at least one row), so with large rows it can hold fewer than limit; continue with offset set to the number of rows you have read. |
<table>_by_id(id: Int!) |
The row whose id column equals id, or null. |
Several fields can go in one request:
{
orders(limit: 5) { id total }
customers_by_id(id: 3) { id name }
}
Mutations
A table gets mutations only if it has a column named id:
| Mutation | Does |
|---|---|
insert_<table>(col: value, ...) |
Inserts a row and returns it. If the table's select row-level policies hide the new row, it returns only the values you sent. |
update_<table>(id: Int!, col: value, ...) |
Sets the given columns on the row with that id and returns it. If the table's select policies hide the row, it returns null, whether or not the update changed it. |
delete_<table>(id: Int!) |
Deletes the row with that id. Returns true if a row was deleted. |
Start the document with mutation, and give every field a selection set in braces, even a delete:
curl -X POST https://api.vaultdb.io/api/databases/shop/graphql \
-H "Authorization: Bearer $VAULTDB_API_KEY" \
-H "Content-Type: application/json" \
-d '{ "query": "mutation { insert_orders(customer: \"grace\", total: 42) { id customer total } }" }'
Argument values can be strings in double quotes, numbers, true, false or null. A string value cannot contain a double quote.
Mutations follow the same rules as the REST API: plan limits, row-level policies, and webhook and realtime events.
Limits and errors
- A document can hold at most 20 fields. Fields past the 20th are not run, and an error is added.
- A document can be at most 16 KB. A larger one gets
413. - If one field fails, for example because it names a table that does not exist, its value in
dataisnulland the response has anerrorsarray. The other fields still run. - A refusal from a plan limit or a row-level policy is returned as an HTTP error (
403) for the whole request, not insideerrors.