Prompt
How do I use GraphQL with a headless CMS for a Next.js frontend?
Latest observation
Nov 9, 2026
OpenAI APIWeb search: off
Using GraphQL with a headless CMS in a Next.js frontend usually looks like this:
- Your CMS stores content
- GraphQL exposes that content via an API
- Your Next.js app queries the API
- You render the returned data in pages/components
Typical setup
Common headless CMS options with GraphQL support include:
- Contentful
- Sanity
- Strapi
- Hygraph
- Prismic (often via APIs/wrappers depending on setup)
Basic flow
1) Get your GraphQL endpoint and auth token
From your CMS, you’ll usually get:
- a GraphQL endpoint URL
- an API token / bearer token
- maybe a space/project ID
Example:
https://your-cms.com/graphql
2) Query the CMS from Next.js
You can use fetch, Apollo Client, urql, or GraphQL-request. For simple cases, fetch is enough.
Example with fetch
// lib/graphql.js
export async function fetchGraphQL(query, variables = {}) {
const res = await fetch(process.env.CMS_GRAPHQL_ENDPOINT, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${process.env.CMS_TOKEN}`,
},
body: JSON.stringify({ query, variables }),
});
const json = await res.json();
if (json.errors) {
throw new Error(JSON.stringify(json.errors));
}
return json.data;
}
3) Use it in a page or server component
App Router example
// app/page.js
import { fetchGraphQL } from '@/lib/graphql';
const query = `
query {
posts {
title
slug
excerpt
}
}
`;
export default async function HomePage() {
const data = await fetchGraphQL(query);
return (
<main>
<h1>Blog</h1>
<ul>
{data.posts.map((post) => (
<li key={post.slug}>
<a href={`/posts/${post.slug}`}>{post.title}</a>
<p>{post.excerpt}</p>
</li>
))}
</ul>
</main>
);
}
If you use dynamic routes
// app/posts/[slug]/page.js
import { fetchGraphQL } from '@/lib/graphql';
const query = `
query PostBySlug($slug: String!) {
post(where: { slug: $slug }) {
title
content
}
}
`;
export default async function PostPage({ params }) {
const data = await fetchGraphQL(query, { slug: params.slug });
return (
<article>
<h1>{data.post.title}</h1>
<div>{data.post.content}</div>
</article>
);
}
Recommended patterns
Use server-side fetching when possible
With Next.js, prefer fetching content on the server:
- better SEO
- keeps tokens secret
- avoids exposing CMS credentials in the browser
Use caching/revalidation
If content changes occasionally, use:
- static generation
- ISR / revalidation
- fetch cache options
Example:
fetch(url, { next: { revalidate: 60 } })
Use GraphQL Code Generator
This helps:
- generate types
- catch query mistakes early
- improve autocomplete
Keep GraphQL queries near the components
This makes it easier to see what data each page needs.
If you want client-side fetching
If a component needs live updates or user-triggered data loading, use Apollo or urql in a client component.
Best practices
- Don’t expose secret tokens in client code
- Query only the fields you need
- Use fragments for reusable query parts
- Handle loading and error states
- Consider pagination for large collections
Example with Apollo Client
If you prefer Apollo:
npm install @apollo/client graphql
Then create a client and use ApolloProvider in your app.
If you want, I can give you:
- a full Next.js App Router example
- a Strapi/Contentful/Sanity-specific setup
- a GraphQL Code Generator setup with TypeScript