Files
speckle-server/packages/frontend-2/lib/common/composables/graphql.ts
T
Kristaps Fabians Geikins 86b535d751 chore(fe2): project page load speed optimization (#1974)
* merging queries for faster load

* preload plugin

* latest threads/models query optimization
2024-01-18 11:00:48 +02:00

31 lines
883 B
TypeScript

import type { QueryOptions } from '@apollo/client/core'
import { convertThrowIntoFetchResult } from '~/lib/common/helpers/graphql'
export const useApolloClientIfAvailable = () => {
const nuxt = useNuxtApp()
const getClient = () => (nuxt.$apollo?.default ? nuxt.$apollo.default : undefined)
return getClient
}
export const useApolloClientFromNuxt = () => {
const getClient = useApolloClientIfAvailable()
const client = getClient()
if (!client) {
throw new Error("Apollo Client can't be resolved from NuxtApp yet")
}
return client
}
export const usePreloadApolloQueries = () => {
const client = useApolloClientFromNuxt()
return async (params: { queries: QueryOptions[] }) => {
const { queries } = params
const promises = queries.map((q) =>
client.query(q).catch(convertThrowIntoFetchResult)
)
return await Promise.all(promises)
}
}