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,8 +6,7 @@ import { PUBLIC_PREVIEW_COUNT } from '$env/static/public'
export const load: PageServerLoad = async () => {
try {
const res = await fetchAPI(`
query {
const res = await fetchAPI(`query {
locations: location (filter: { status: { _eq: "published" }}) {
id
name
@@ -84,8 +83,7 @@ export const load: PageServerLoad = async () => {
countCountries: country_aggregated (filter: { status: { _eq: "published" }}) {
count { id }
}
}
`)
}`)
if (res) {
const { data: { countPhotos, countLocations, countCountries, ...rest }} = res

View File

@@ -6,8 +6,7 @@ import { getRandomItems } from '$utils/functions'
export const load: PageServerLoad = async () => {
try {
// Get total of published photos
const totalRes = await fetchAPI(`
query {
const totalRes = await fetchAPI(`query {
photo (
filter: {
favorite: { _eq: true },
@@ -17,16 +16,14 @@ export const load: PageServerLoad = async () => {
) {
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 {
const photosRes = await fetchAPI(`query {
photo (filter: { id: { _in: [${randomPhotosIds}] }}) {
slug
title
@@ -42,8 +39,7 @@ export const load: PageServerLoad = async () => {
}
image { id }
}
}
`)
}`)
const { data: { photo: photos }} = photosRes
if (photos) {

View File

@@ -21,8 +21,7 @@ export const load: PageServerLoad = async ({ params }) => {
const { location: slug } = params
// Query
const res = await fetchAPI(`
query {
const res = await fetchAPI(`query {
location (
filter: {
slug: { _eq: "${slug}" },
@@ -76,8 +75,7 @@ export const load: PageServerLoad = async ({ params }) => {
}
}
}
}
`)
}`)
const { data: { location: location, photos, total_published, product }} = res

View File

@@ -75,8 +75,7 @@
const loadPhotos = async (page?: number) => {
const res = await fetch('/api/data', {
method: 'POST',
body: `
query {
body: `query {
photos: photo (
filter: {
location: { slug: { _eq: "${params.location}" }},
@@ -88,8 +87,7 @@
) {
${photoFields}
}
}
`,
}`,
})
const { data: { photos }} = await res.json()

View File

@@ -5,18 +5,15 @@ import { fetchAPI } from '$utils/api'
export const load: PageServerLoad = async ({ params }) => {
try {
// Get the first photo ID
const firstPhoto = await fetchAPI(`
query {
const firstPhoto = await fetchAPI(`query {
photo (search: "${params.photo}") {
id
}
}
`)
}`)
const firstPhotoId = firstPhoto?.data?.photo[0]?.id
// TODO: use same request for both queries (photo.id)
const photosBeforeFirst = await fetchAPI(`
query {
const photosBeforeFirst = await fetchAPI(`query {
count: photo_aggregated (
filter: {
id: { _gt: ${firstPhotoId} },
@@ -29,15 +26,13 @@ export const load: PageServerLoad = async ({ params }) => {
id
}
}
}
`)
}`)
// Define offset from the current count
const offset = Math.max(photosBeforeFirst?.data?.count[0]?.count.id - 5, 0)
const limit = 10
const res = await fetchAPI(`
query {
const res = await fetchAPI(`query {
photos: photo (
filter: {
location: { slug: { _eq: "${params.location}" }}
@@ -72,8 +67,7 @@ export const load: PageServerLoad = async ({ params }) => {
total_published: photo_aggregated (filter: { location: { slug: { _eq: "${params.location}" }}}) {
count { location }
}
}
`)
}`)
const { data } = res

View File

@@ -152,8 +152,7 @@
const isPrev = direction === directions.PREV
const res = await fetch('/api/data', {
method: 'POST',
body: `
query {
body: `query {
photos: photo (
filter: {
location: { slug: { _eq: "${location.slug}" }},
@@ -173,8 +172,7 @@
}
city
}
}
`,
}`,
})
const { data: { photos: newPhotos }} = await res.json()

View File

@@ -6,8 +6,7 @@ import { getRandomItems } from '$utils/functions'
export const load: PageServerLoad = async () => {
try {
// Get data and total of published photos
const res = await fetchAPI(`
query {
const res = await fetchAPI(`query {
photos: photo (
filter: {
favorite: { _eq: true },
@@ -55,16 +54,14 @@ export const load: PageServerLoad = async () => {
contact_title
contact_blocks
}
}
`)
}`)
const { data: { about, photos: photosIds }} = res
// Get random photos
const randomPhotosIds = [...getRandomItems(photosIds, 42)].map(({ id }) => id)
// Query these random photos from IDs
const photosRes = await fetchAPI(`
query {
const photosRes = await fetchAPI(`query {
photo (filter: { id: { _in: [${randomPhotosIds}] }}) {
id
title
@@ -74,8 +71,7 @@ export const load: PageServerLoad = async () => {
title
}
}
}
`)
}`)
if (photosRes) {
const { data: { photo: photos }} = photosRes

View File

@@ -4,8 +4,7 @@ import { fetchAPI } from '$utils/api'
export const load: PageServerLoad = async () => {
try {
const res = await fetchAPI(`
query {
const res = await fetchAPI(`query {
credits {
text
list
@@ -27,8 +26,7 @@ export const load: PageServerLoad = async () => {
}
}
}
}
`)
}`)
const { data } = res

View File

@@ -11,8 +11,7 @@ export const load: PageServerLoad = async ({ url }) => {
const querySort = url.searchParams.get('sort') || PUBLIC_FILTERS_DEFAULT_SORT
// Query
const res = await fetchAPI(`
query {
const res = await fetchAPI(`query {
photos: photo (
filter: {
${queryCountry !== 'all' ? `location: { country: { slug: { _eq: "${queryCountry}" }}},` : ''}
@@ -56,8 +55,7 @@ export const load: PageServerLoad = async ({ url }) => {
total_published: photo_aggregated ${queryCountry !== 'all' ? `(filter: { location: { country: { slug: { _eq: "${queryCountry}" }}}})` : ''} {
count { id }
}
}
`)
}`)
const { data } = res

View File

@@ -159,8 +159,7 @@
const loadPhotos = async (page: number) => {
const res = await fetch('/api/data', {
method: 'POST',
body: `
query {
body: `query {
photos: photo (
filter: {
${filterCountry !== 'all' ? `location: { country: { slug: { _eq: "${filterCountry}" }} },` : ''}
@@ -189,8 +188,7 @@
}
city
}
}
`,
}`,
})
const { data: { photos }} = await res.json()

View File

@@ -6,8 +6,7 @@ import { getProducts } from '$utils/functions/swell'
export const load: PageServerLoad = async () => {
try {
// Get content from API
const res = await fetchAPI(`
query {
const res = await fetchAPI(`query {
shop {
page_heroimage { id }
}
@@ -48,8 +47,7 @@ export const load: PageServerLoad = async () => {
}
}
}
}
`)
}`)
const { data: { shop, location, posters }} = res

View File

@@ -7,8 +7,7 @@ import { getRandomItem } from '$utils/functions'
export const load: PageServerLoad = async ({}) => {
try {
// Get content from API
const data = await fetchAPI(`
query {
const data = await fetchAPI(`query {
posters: product (
filter: { status: { _eq: "published" }}
) {
@@ -34,8 +33,7 @@ export const load: PageServerLoad = async ({}) => {
}
}
}
}
`)
}`)
if (data) {
const randomPoster = getRandomItem(data.data.posters)

View File

@@ -6,8 +6,7 @@ import { getProduct } from '$utils/functions/swell'
export const load: PageServerLoad = async ({ params }) => {
try {
// Get content from API
const data = await fetchAPI(`
query {
const data = await fetchAPI(`query {
poster: product (search: "${params.name}") {
name
type
@@ -31,8 +30,7 @@ export const load: PageServerLoad = async ({ params }) => {
}
}
}
}
`)
}`)
if (data) {
const poster = data.data.poster[0]

View File

@@ -4,8 +4,7 @@ import { fetchAPI } from '$utils/api'
export const load: PageServerLoad = async () => {
try {
const res = await fetchAPI(`
query {
const res = await fetchAPI(`query {
settings {
newsletter_page_text
}
@@ -21,8 +20,7 @@ export const load: PageServerLoad = async () => {
link
thumbnail { id }
}
}
`)
}`)
const { data } = res