GraphQL
Turn on the GraphQL endpoint to query and mutate bucket data with a schema generated from your buckets.
Spica can generate a GraphQL schema from your buckets. Every bucket gets query and mutation fields, and the schema is rebuilt when buckets change.
Enable GraphQL
GraphQL is off by default. Start the API with:
--bucket-graphql=trueThe endpoint is POST /graphql under your public URL, for example https://api.example.com/api/graphql. Opening it in a browser shows GraphiQL.
Generated types
Each bucket becomes a type named Bucket_<bucketId> with these operations:
| Operation | Field |
|---|---|
| List | FindBucket_<id>(limit: Int, skip: Int, sort: JSON, language: String, query: JSON): Bucket_<id>FindResponse |
| Get | FindByBucket_<id>Id(_id: ObjectId!, language: String): Bucket_<id> |
| Insert | insertBucket_<id>(input: Bucket_<id>Input): Bucket_<id> |
| Replace | replaceBucket_<id>(_id: ObjectId!, input: Bucket_<id>Input): Bucket_<id> |
| Patch | patchBucket_<id>(_id: ObjectId!, input: JSON): Bucket_<id> |
| Delete | deleteBucket_<id>(_id: ObjectId!): String |
List results have the shape {meta {total} data {...}}. Relations are exposed as nested types, so you can select fields of related documents in one query.
Example
query RecentPosts {
FindBucket_6b0b1968a5611972c29c25f7(limit: 5, sort: {created_at: -1}, language: "en_US") {
meta {
total
}
data {
_id
title
slug
author {
name
}
}
}
}curl -X POST https://api.example.com/api/graphql \
-H "Authorization: APIKEY <key>" \
-H "Content-Type: application/json" \
-d '{"query": "{ FindBucket_6b0b1968a5611972c29c25f7(limit: 5) { data { _id title } } }"}'Authorization
The caller needs the bucket:index action to load the schema, plus the data actions of each bucket it touches, like the REST API.
When to use it
- Use GraphQL when clients need to fetch nested data with precise field selection.
- Use the REST API when you need expression filters that work on every engine, or for the simplest integration.
- Property names that aren't valid GraphQL names are adjusted, and the endpoint lists such adjustments in a
Warningresponse header.