Make GraphQL queries more compact

This commit is contained in:
2022-08-16 21:17:14 +02:00
parent 52e0407700
commit 0e6aaaa4e2
14 changed files with 435 additions and 471 deletions

View File

@@ -6,44 +6,40 @@ import { getRandomItems } from '$utils/functions'
export const load: PageServerLoad = async () => {
try {
// Get total of published photos
const totalRes = await fetchAPI(`
query {
photo (
filter: {
favorite: { _eq: true },
status: { _eq: "published" },
},
limit: -1,
) {
id
}
const totalRes = await fetchAPI(`query {
photo (
filter: {
favorite: { _eq: true },
status: { _eq: "published" },
},
limit: -1,
) {
id
}
`)
}`)
const { data: { photo: photosIds }} = totalRes
// Get random photos
const randomPhotosIds = [...getRandomItems(photosIds, 11)].map(({ id }) => id)
// Query these random photos from IDs
const photosRes = await fetchAPI(`
query {
photo (filter: { id: { _in: [${randomPhotosIds}] }}) {
const photosRes = await fetchAPI(`query {
photo (filter: { id: { _in: [${randomPhotosIds}] }}) {
slug
title
city
location {
name
slug
title
city
location {
name
country {
slug
country {
slug
name
flag { id }
}
name
flag { id }
}
image { id }
}
image { id }
}
`)
}`)
const { data: { photo: photos }} = photosRes
if (photos) {