({
query,
variables = {},
tags = [],
preview = false,
revalidate,
}: {
query: string
variables?: any
tags?: string[]
preview?: boolean
revalidate?: number
})
| 1 | export const contentGQLQuery = async <T>({ |
| 2 | query, |
| 3 | variables = {}, |
| 4 | tags = [], |
| 5 | preview = false, |
| 6 | revalidate, |
| 7 | }: { |
| 8 | query: string |
| 9 | variables?: any |
| 10 | tags?: string[] |
| 11 | preview?: boolean |
| 12 | revalidate?: number |
| 13 | }): Promise<T | undefined> => { |
| 14 | const res = await fetch( |
| 15 | `https://graphql.contentful.com/content/v1/spaces/${process.env.CONTENTFUL_SPACE_ID}`, |
| 16 | { |
| 17 | method: "POST", |
| 18 | headers: { |
| 19 | "Content-Type": "application/json", |
| 20 | Authorization: `Bearer ${ |
| 21 | preview |
| 22 | ? process.env.CONTENTFUL_PREVIEW_ACCESS_TOKEN |
| 23 | : process.env.CONTENTFUL_ACCESS_TOKEN |
| 24 | }`, |
| 25 | }, |
| 26 | body: JSON.stringify({ query, variables }), |
| 27 | next: { tags, ...{ revalidate } }, |
| 28 | } |
| 29 | ) |
| 30 | |
| 31 | const { data, errors } = await res.json() |
| 32 | if (errors) { |
| 33 | console.error(errors) |
| 34 | throw new Error("error") |
| 35 | } |
| 36 | |
| 37 | return data as T |
| 38 | } |
nothing calls this directly
no outgoing calls
no test coverage detected