Prompt
How do I use GraphQL with a headless CMS?
Latest observation
Oct 20, 2026
OpenAI APIWeb search: off
Using GraphQL with a headless CMS is usually straightforward: the CMS exposes content through a GraphQL API, and your app queries only the fields it needs.
Basic flow
- Pick a headless CMS that supports GraphQL
- Examples: Contentful, Strapi, Sanity, Hygraph, DatoCMS, Keystone, etc.
- Create content types/models
- For example:
Post,Author,Category,Page.
- For example:
- Publish content
- Editors add content in the CMS admin UI.
- Query content from your app
- Your frontend sends GraphQL queries to the CMS endpoint.
- Render the data
- Use the returned JSON in React, Next.js, Vue, mobile apps, etc.
Example GraphQL query
If your CMS has a blog post model, you might query:
query GetPost($slug: String!) {
post(where: { slug: $slug }) {
title
slug
excerpt
body
publishedAt
author {
name
avatarUrl
}
}
}
Variables:
{
"slug": "hello-world"
}
Typical frontend usage
JavaScript with fetch
const query = `
query GetPosts {
posts {
title
slug
excerpt
}
}
`;
const res = await fetch("https://your-cms.com/graphql", {
method: "POST",
headers: {
"Content-Type": "application/json",
"Authorization": `Bearer ${process.env.CMS_TOKEN}`
},
body: JSON.stringify({ query })
});
const data = await res.json();
console.log(data.data.posts);
Benefits of GraphQL in a headless CMS
- Fetch only what you need: reduces over-fetching
- One endpoint: easier than multiple REST endpoints
- Strong schema: easier to discover available content fields
- Good for relationships: nested content like posts + authors + tags
- Works well for multiple clients: web, mobile, desktop, etc.
Common setup considerations
Authentication
Some CMS GraphQL APIs are public for read access, while others require:
- API tokens
- role-based permissions
- preview/draft tokens for unpublished content
Drafts and preview mode
If you need preview content:
- use a preview token
- query draft/published states depending on the CMS
Pagination
Instead of loading everything at once, use pagination:
query GetPosts($first: Int!, $after: String) {
posts(first: $first, after: $after) {
edges {
node {
title
slug
}
}
pageInfo {
hasNextPage
endCursor
}
}
}
Caching
GraphQL queries can be cached in:
- your frontend app
- CDN/edge layers
- server-side rendering (SSR) frameworks like Next.js
Good practice
- Keep queries close to where data is used
- Use fragments for repeated field sets
- Avoid very deep nested queries if your CMS/API has performance limits
- Use generated TypeScript types if your CMS supports schema introspection
- Test queries in a GraphQL playground or explorer
Example fragment:
fragment PostCardFields on Post {
title
slug
excerpt
}
query GetPosts {
posts {
...PostCardFields
}
}
In a framework like Next.js
You’d typically:
- fetch data in
getStaticProps,getServerSideProps, or server components - build pages from CMS content
- use revalidation/webhooks so content updates automatically
If you want, I can show you:
- a complete example with Strapi/Sanity/Contentful, or
- how to do this in Next.js or React.