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:

Terminal
--bucket-graphql=true

The 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:

OperationField
ListFindBucket_<id>(limit: Int, skip: Int, sort: JSON, language: String, query: JSON): Bucket_<id>FindResponse
GetFindByBucket_<id>Id(_id: ObjectId!, language: String): Bucket_<id>
InsertinsertBucket_<id>(input: Bucket_<id>Input): Bucket_<id>
ReplacereplaceBucket_<id>(_id: ObjectId!, input: Bucket_<id>Input): Bucket_<id>
PatchpatchBucket_<id>(_id: ObjectId!, input: JSON): Bucket_<id>
DeletedeleteBucket_<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

GraphQL
query RecentPosts {
  FindBucket_6b0b1968a5611972c29c25f7(limit: 5, sort: {created_at: -1}, language: "en_US") {
    meta {
      total
    }
    data {
      _id
      title
      slug
      author {
        name
      }
    }
  }
}
Terminal
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 Warning response header.