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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@@ -4,8 +4,7 @@ import { fetchAPI } from '$utils/api'
export const load: PageServerLoad = async () => { export const load: PageServerLoad = async () => {
try { try {
const res = await fetchAPI(` const res = await fetchAPI(`query {
query {
credits { credits {
text text
list list
@@ -27,8 +26,7 @@ export const load: PageServerLoad = async () => {
} }
} }
} }
} }`)
`)
const { data } = res 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 const querySort = url.searchParams.get('sort') || PUBLIC_FILTERS_DEFAULT_SORT
// Query // Query
const res = await fetchAPI(` const res = await fetchAPI(`query {
query {
photos: photo ( photos: photo (
filter: { filter: {
${queryCountry !== 'all' ? `location: { country: { slug: { _eq: "${queryCountry}" }}},` : ''} ${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}" }}}})` : ''} { total_published: photo_aggregated ${queryCountry !== 'all' ? `(filter: { location: { country: { slug: { _eq: "${queryCountry}" }}}})` : ''} {
count { id } count { id }
} }
} }`)
`)
const { data } = res const { data } = res

View File

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

View File

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

View File

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

View File

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

View File

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