Skip to content

Commit

Permalink
Combine queries
Browse files Browse the repository at this point in the history
  • Loading branch information
chibicode committed Jun 10, 2020
1 parent 2cf6796 commit 1f29128
Showing 1 changed file with 13 additions and 25 deletions.
38 changes: 13 additions & 25 deletions examples/cms-storyblok/lib/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ export async function getAllPostsForHome(preview) {
}

export async function getPostAndMorePosts(slug, preview) {
const post = await fetchAPI(
const data = await fetchAPI(
`
query PostBySlug($slug: ID!) {
PostItem(id: $slug) {
Expand All @@ -101,20 +101,7 @@ export async function getPostAndMorePosts(slug, preview) {
}
}
}
}
`,
{
preview,
variables: {
slug: `posts/${slug}`,
},
}
)
const morePosts = post?.PostItem?.id
? await fetchAPI(
`
query PostBySlug($id: String) {
PostItems(excluding_ids: $id, per_page: 2, sort_by: "first_published_at:desc") {
PostItems(per_page: 3, sort_by: "first_published_at:desc") {
items {
slug
published_at
Expand All @@ -133,17 +120,18 @@ export async function getPostAndMorePosts(slug, preview) {
}
}
`,
{
preview,
variables: {
id: `${post.PostItem.id}`,
},
}
)
: {}
{
preview,
variables: {
slug: `posts/${slug}`,
},
}
)

return {
post: post?.PostItem,
morePosts: morePosts?.PostItems?.items || [],
post: data?.PostItem,
morePosts: (data?.PostItems?.items || [])
.filter((item) => item.slug !== slug)
.slice(0, 2),
}
}

0 comments on commit 1f29128

Please sign in to comment.