Skip to content

Read with GraphQL

With the GraphQL API, you can read what a page needs in one call. You can also get entries of different content types together, such as a list of articles and the site settings. Only the fields you write in the query are returned.

The GraphQL schema is generated automatically from the content types of the space. When you add content types or fields, you can use them in queries right away.

  • An API key with the [読み取り](Read) permission. To issue one, see Create API keys and set permissions. The permissions are the same as for the REST Content API
  • The entries you read must be [公開中](Published). To read drafts, see “Read drafts” below
  • The content type ID. This is the value shown as [エンドポイント](Endpoint) on the content type screen (for example, “posts”)

In the examples below, replace these values:

  • <hostname>: the hostname of your environment. It is the part of the admin address https://<hostname>.hakocms.net
  • <API key>: your API key, which starts with hako_

The example space has the collections “posts” (articles) and “authors” (authors), and the single “site-settings” (site settings). The article fields are title (text), body (rich text), tags (select), and author (a reference to an author).

Send queries to /api/v1/graphql with POST. The body is JSON, with the query in query. Send the API key with Authorization: Bearer, the same as REST.

Count the articles and read the titles of 2 of them.

ターミナルウィンドウ
curl https://<hostname>.hakocms.net/api/v1/graphql \
-H "Authorization: Bearer <API key>" \
-H "Content-Type: application/json" \
-d '{"query": "{ postsCollection(limit: 2) { total items { title } } }"}'

You get a response like this:

{
"data": {
"postsCollection": {
"total": 3,
"items": [
{ "title": "年末年始のお休み" },
{ "title": "GraphQL で読む" }
]
}
}
}

Besides query, the body can also have variables (variables) and operationName (the name of the query to run).

Type and field names come from content type IDs and field IDs.

What you read Query field If the content type ID is “blog-posts”
A list from a collection The ID joined with its first letter lowercase, plus Collection blogPostsCollection
One entry from a collection The ID joined with its first letter lowercase blogPosts(id: "hello")
A single The ID joined with its first letter lowercase (for “site-settings”) siteSettings
  • Type names are the ID joined with its first letter uppercase (BlogPosts)
  • Field names are the field IDs as is. -, which GraphQL names cannot contain, becomes _ (old-field becomes old_field)
  • Every entry has sys, from which you can read id, contentType, createdAt, updatedAt, publishedAt, firstPublishedAt, and version

To check the exact types, get the schema of your space as described in “Get the schema” below.

In one call, read what the top page needs: the 2 newest articles (title, tags, and author name) and the site name. Home is the name of the query. You can name it anything.

query Home {
postsCollection(limit: 2) {
total
items {
sys { id publishedAt }
title
tags
author { name }
}
}
siteSettings { siteName }
}

You get a response like this:

{
"data": {
"siteSettings": { "siteName": "山田ブログ" },
"postsCollection": {
"total": 3,
"items": [
{
"sys": {
"id": "holiday",
"publishedAt": "2026-10-21T15:16:12.691Z"
},
"title": "年末年始のお休み",
"tags": ["news"],
"author": { "name": "山田 花子" }
},
{
"sys": {
"id": "graphql",
"publishedAt": "2026-10-21T15:16:11.661Z"
},
"title": "GraphQL で読む",
"tags": ["tech"],
"author": { "name": "山田 花子" }
}
]
}
}
}
  • By default, lists are newest published first, up to 20 entries. Divide them with limit (0–100) and offset
  • References (author) are followed only as far as you write in the query. You do not need REST’s include. You can follow up to 4 levels, including the top
  • If a referenced entry is not published or has been deleted, the reference field is null. For multiple references, it is removed from the array

Add where and order to the list field. You can pass values as variables. Read articles with the tag “news”, newest published first.

query News($tag: String!) {
postsCollection(
where: { tags: { contains: $tag } }
order: [sys_publishedAt_DESC]
) {
items { title }
}
}

Write the variables in variables in the body.

{ "tag": "news" }

You get a response like this:

{
"data": {
"postsCollection": {
"items": [
{ "title": "年末年始のお休み" },
{ "title": "サイトを公開しました" }
]
}
}
}
  • In where, write conditions for each field. The conditions you can use depend on the field type (for text, eq, contains, startsWith, and others; for numbers and dates, lt, gte, and others). They mean the same as REST’s where, described in Filter, sort, and paginate
  • To combine conditions, use and, or, and not. You can also filter by sys’s id and dates
  • In order, list <field ID>_ASC (smallest first) or <field ID>_DESC (largest first). You can sort by fields of type text, text area, date and time, and number, and by sys’s id and dates
  • Add q to a list field for full-text search. It means the same as in Use full-text search

Get one entry and receive rich text as HTML

Section titled “Get one entry and receive rich text as HTML”

Pass the entry ID in id. For a rich text field, select html to get HTML rendered on the server, or json to get the JSON format.

query Post($id: ID!) {
posts(id: $id) {
title
body { html }
}
}

If you pass { "id": "hello" } as the variables, you get a response like this:

{
"data": {
"posts": {
"title": "サイトを公開しました",
"body": { "html": "<p>今日からサイトを公開します。</p>" }
}
}
}

If the entry does not exist or is not published, posts is null. The JSON format is described in Rich text format (Hako Rich Text v1).

In JavaScript (browser, Node.js), send it with fetch. The following code sends the query from “Filter and sort” above and prints the titles.

const res = await fetch(
"https://<hostname>.hakocms.net/api/v1/graphql",
{
method: "POST",
headers: {
Authorization: "Bearer <API key>",
"Content-Type": "application/json",
},
body: JSON.stringify({
query: `query News($tag: String!) {
postsCollection(where: { tags: { contains: $tag } }) {
items { title }
}
}`,
variables: { tag: "news" },
}),
},
);
const { data, errors } = await res.json();
if (errors) throw new Error(errors[0].message);
console.log(data.postsCollection.items.map((p) => p.title));

You can call it from any origin (CORS). Give the API key you pass to the browser only the [読み取り](Read) permission.

Add preview: true to a top-level field of the query to read drafts before they are published. Referenced entries you follow under it also return draft values.

{
posts(id: "hello", preview: true) { title }
}

Only an API key with the [プレビュー](Preview) permission can read drafts. Use this key only on your site’s server, and do not give it to the browser. For details, see Read drafts (preview).

You can get the GraphQL schema (SDL) of the space as follows. The API also answers introspection, so you can connect GraphQL tools (editor completion, GraphQL Code Generator, and others) as is.

ターミナルウィンドウ
curl https://<hostname>.hakocms.net/api/v1/schema/graphql \
-H "Authorization: Bearer <API key>"

You get text that starts like this (excerpt). The names of content types and fields are included as descriptions.

"""
著者(authors)
"""
type Authors {
sys: Sys!
"""
名前
"""
name: String
}

The types for each field type are as follows.

Field type GraphQL type When there is no value
Text, text area String null
Rich text RichText (json, html) null
Image Image (url, width, height, alt) null
Multiple images [Image!]! []
File File (url, fileSize) null
Date and time DateTime (an RFC 3339 string) null
Boolean Boolean! false
Number Float null
Select [String!]! (the option values) []
Reference The type of the referenced entry null
Multiple references An array of the type of the referenced entries []
Custom A type named by joining the content type name and the custom field name null
Repeater An array of the custom type. If there are 2 or more custom fields, an array of a union []

So that heavy queries do not slow your site down, the following limits apply.

Item Limit
Entries in one list (limit) 0–100. The default is 20
Total entries returned in one call (including referenced entries) 1,000
Following references 4 levels, including the top
Query depth 10
Query complexity (the number of fields you write) 2,000
Body size 64 KB. If it is larger, 413

If the fields you wrote are returned in data, the read works. If something goes wrong, look at errors in the response. GraphQL errors are returned with HTTP 200 and errors.

{
"data": null,
"errors": [
{
"message": "limit must be an integer from 0 to 100",
"locations": [{ "line": 2, "column": 3 }],
"extensions": { "code": "BAD_REQUEST" }
}
]
}

Of the fields written in one query, only those with errors are removed from data. The other fields are returned.

What you got Cause Fix
401 unauthorized The API key is missing, wrong, or revoked Check that you put the key starting with hako_ as is after Authorization: Bearer
400 bad_request The body is not in the form of a GraphQL request (JSON with query) Put the body in the form {"query": "…"} and check that it is valid JSON
code in errors is FORBIDDEN The key does not have permission to read that content type, or does not have the preview permission for preview: true On the [API キー](API keys) screen, check the key’s permissions
code in errors is BAD_REQUEST An argument value is not valid (limit is out of range, a where condition does not fit, and so on) message shows the reason
Unknown field in errors You wrote a field that is not in the schema Check the name against “Naming rules.” Get the schema to find the exact names