-
Notifications
You must be signed in to change notification settings - Fork 0
/
gql-client.tsx
32 lines (27 loc) · 1.5 KB
/
gql-client.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import {GraphQLClient} from "graphql-request"
import {getSdk} from "@lib/gql/__generated__/queries"
export const graphqlClient = (requestConfig: Omit<RequestInit, "method"> = {}, isPreviewMode?: boolean) => {
requestConfig.headers = buildHeaders(requestConfig.headers as HeadersInit, isPreviewMode)
const client = new GraphQLClient(process.env.NEXT_PUBLIC_DRUPAL_BASE_URL + "/graphql", {
...requestConfig,
next: requestConfig.cache ? {...requestConfig.next} : {revalidate: 60 * 60 * 24 * 365, ...requestConfig.next},
// Use fetch function so Next.js will be able to cache it normally.
fetch: async (input: URL | RequestInfo, init?: RequestInit) => fetch(input, init),
})
return getSdk(client)
}
export const buildHeaders = (headers?: HeadersInit, isPreviewMode?: boolean): Headers => {
const requestHeaders = new Headers(headers)
// If viewing while in preview mode, use the admin credentials if they are available. Fall back to the basic credentials.
const authCreds = (
isPreviewMode ? process.env.DRUPAL_BASIC_AUTH_ADMIN || process.env.DRUPAL_BASIC_AUTH : process.env.DRUPAL_BASIC_AUTH
) as string
if (process.env.DRUPAL_REQUEST_HEADERS) {
const envRequestHeaders: Record<string, string> = JSON.parse(process.env.DRUPAL_REQUEST_HEADERS)
Object.keys(envRequestHeaders).map(key => {
requestHeaders.set(key, envRequestHeaders[key])
})
}
if (authCreds) requestHeaders.set("Authorization", "Basic " + Buffer.from(authCreds).toString("base64"))
return requestHeaders
}