Unverified Commit 3f4872aa authored by Ante Sepic's avatar Ante Sepic Committed by GitHub

[Examples] Switch contentful example to graphql (#15223)

Now that all Contentful spaces support GraphQL, we are looking into switching the example from using a JS library to a simple fetch with GraphQL.

@stefanjudis ☝️ 
parent c8fa2848
...@@ -5,7 +5,7 @@ export default function PostBody({ content }) { ...@@ -5,7 +5,7 @@ export default function PostBody({ content }) {
return ( return (
<div className="max-w-2xl mx-auto"> <div className="max-w-2xl mx-auto">
<div className={markdownStyles['markdown']}> <div className={markdownStyles['markdown']}>
{documentToReactComponents(content)} {documentToReactComponents(content.json)}
</div> </div>
</div> </div>
) )
......
import { createClient } from 'contentful' const POST_GRAPHQL_FIELDS = `
slug
const client = createClient({ title
space: process.env.CONTENTFUL_SPACE_ID, coverImage {
accessToken: process.env.CONTENTFUL_ACCESS_TOKEN, url
}) }
date
const previewClient = createClient({ author {
space: process.env.CONTENTFUL_SPACE_ID, name
accessToken: process.env.CONTENTFUL_PREVIEW_ACCESS_TOKEN, picture {
host: 'preview.contentful.com', url
})
const getClient = (preview) => (preview ? previewClient : client)
function parseAuthor({ fields }) {
return {
name: fields.name,
picture: fields.picture.fields.file,
} }
} }
excerpt
content {
json
}
`
function parsePost({ fields }) { async function fetchGraphQL(query, preview = false) {
return { return fetch(
title: fields.title, `https://graphql.contentful.com/content/v1/spaces/${process.env.CONTENTFUL_SPACE_ID}`,
slug: fields.slug, {
date: fields.date, method: 'POST',
content: fields.content, headers: {
excerpt: fields.excerpt, 'Content-Type': 'application/json',
coverImage: fields.coverImage.fields.file, Authorization: `Bearer ${
author: parseAuthor(fields.author), preview
} ? process.env.CONTENTFUL_PREVIEW_ACCESS_TOKEN
: process.env.CONTENTFUL_ACCESS_TOKEN
}`,
},
body: JSON.stringify({ query }),
}
).then((response) => response.json())
} }
function parsePostEntries(entries, cb = parsePost) { function extractPost(fetchResponse) {
return entries?.items?.map(cb) return fetchResponse?.data?.postCollection?.items?.[0]
}
function extractPostEntries(fetchResponse) {
return fetchResponse?.data?.postCollection?.items
} }
export async function getPreviewPostBySlug(slug) { export async function getPreviewPostBySlug(slug) {
const entries = await getClient(true).getEntries({ const entry = await fetchGraphQL(
content_type: 'post', `query {
limit: 1, postCollection(where: { slug: "${slug}" }, preview: true, limit: 1) {
'fields.slug[in]': slug, items {
}) ${POST_GRAPHQL_FIELDS}
return parsePostEntries(entries)[0] }
}
}`,
true
)
return extractPost(entry)
} }
export async function getAllPostsWithSlug() { export async function getAllPostsWithSlug() {
const entries = await client.getEntries({ const entries = await fetchGraphQL(
content_type: 'post', `query {
select: 'fields.slug', postCollection(where: { slug_exists: true }, order: date_DESC) {
}) items {
return parsePostEntries(entries, (post) => post.fields) ${POST_GRAPHQL_FIELDS}
}
}
}`
)
return extractPostEntries(entries)
} }
export async function getAllPostsForHome(preview) { export async function getAllPostsForHome(preview) {
const entries = await getClient(preview).getEntries({ const entries = await fetchGraphQL(
content_type: 'post', `query {
order: '-fields.date', postCollection(order: date_DESC, preview: ${preview ? 'true' : 'false'}) {
}) items {
return parsePostEntries(entries) ${POST_GRAPHQL_FIELDS}
}
}
}`,
preview
)
return extractPostEntries(entries)
} }
export async function getPostAndMorePosts(slug, preview) { export async function getPostAndMorePosts(slug, preview) {
const entry = await getClient(preview).getEntries({ const entry = await fetchGraphQL(
content_type: 'post', `query {
limit: 1, postCollection(where: { slug: "${slug}" }, preview: ${
'fields.slug[in]': slug, preview ? 'true' : 'false'
}) }, limit: 1) {
const entries = await getClient(preview).getEntries({ items {
content_type: 'post', ${POST_GRAPHQL_FIELDS}
limit: 2, }
order: '-fields.date', }
'fields.slug[nin]': slug, }`,
}) preview
)
const entries = await fetchGraphQL(
`query {
postCollection(where: { slug_not_in: "${slug}" }, order: date_DESC, preview: ${
preview ? 'true' : 'false'
}, limit: 2) {
items {
${POST_GRAPHQL_FIELDS}
}
}
}`,
preview
)
return { return {
post: parsePostEntries(entry)[0], post: extractPost(entry),
morePosts: parsePostEntries(entries), morePosts: extractPostEntries(entries),
} }
} }
...@@ -8,19 +8,18 @@ ...@@ -8,19 +8,18 @@
"setup": "node ./contentful/setup.js $CONTENTFUL_SPACE_ID $CONTENTFUL_MANAGEMENT_TOKEN" "setup": "node ./contentful/setup.js $CONTENTFUL_SPACE_ID $CONTENTFUL_MANAGEMENT_TOKEN"
}, },
"dependencies": { "dependencies": {
"@contentful/rich-text-react-renderer": "13.4.0", "@contentful/rich-text-react-renderer": "14.1.1",
"classnames": "2.2.6", "classnames": "2.2.6",
"contentful": "7.14.4", "date-fns": "2.14.0",
"date-fns": "2.10.0",
"next": "latest", "next": "latest",
"react": "^16.13.0", "react": "^16.13.1",
"react-dom": "^16.13.0" "react-dom": "^16.13.1"
}, },
"devDependencies": { "devDependencies": {
"@fullhuman/postcss-purgecss": "^2.1.0", "@fullhuman/postcss-purgecss": "^2.3.0",
"contentful-import": "^7.7.12", "contentful-import": "^7.8.7",
"postcss-preset-env": "^6.7.0", "postcss-preset-env": "^6.7.0",
"tailwindcss": "^1.2.0" "tailwindcss": "^1.5.1"
}, },
"license": "MIT" "license": "MIT"
} }
module.exports = { module.exports = {
purge: ['./components/**/*.js', './components/**/*.css'],
theme: { theme: {
extend: { extend: {
colors: { colors: {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment