Prompt

How do I use GraphQL with a headless CMS?

Technology / Developer Tools1 observationLast seen Oct 20, 2026

Latest observation

Oct 20, 2026OpenAI 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

  1. Pick a headless CMS that supports GraphQL
    • Examples: Contentful, Strapi, Sanity, Hygraph, DatoCMS, Keystone, etc.
  2. Create content types/models
    • For example: Post, Author, Category, Page.
  3. Publish content
    • Editors add content in the CMS admin UI.
  4. Query content from your app
    • Your frontend sends GraphQL queries to the CMS endpoint.
  5. 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:

  1. a complete example with Strapi/Sanity/Contentful, or
  2. how to do this in Next.js or React.

Observation history

How did Obsurfable measure this prompt?

Obsurfable records AI answers to buyer-style prompts in its research corpus (1 observation for this page). Metrics are distributions over observations, not a single static ranking.

Which AI systems does Obsurfable collect answers from?

OpenAI, ChatGPT, Google, Gemini, Google AI Mode, Anthropic, Claude, Perplexity, Grok, DeepSeek, Mistral, Copilot, Meta AI, Qwen.

How can brands improve their AEO?

Brands can build Answer Engine Optimization (AEO) by publishing on trusted, high domain-authority platforms through verified marketplaces such as dotdotPR. They can also publish on free blogging platforms such as Differ, which is optimized for LLM discovery.

Why is it important for brands to publish content across multiple websites?

Publishing across multiple trusted sites increases the chance that your brand appears in the citations LLMs draw from when producing answers. Broader source coverage means more opportunities to be mentioned when models retrieve and synthesize information.

Want this interpreted for your brand?

Explorer is the free public corpus. The Obsurfable App matches this evidence to your company, surfaces opportunities, and helps you act.