|
1 | | -import { DrupalClient } from "next-drupal" |
| 1 | +import { NextDrupalGraphQL } from "./next-drupal-graphql" |
2 | 2 |
|
3 | | -const baseUrl: string = process.env.NEXT_PUBLIC_DRUPAL_BASE_URL || "" |
4 | | -const clientId = process.env.DRUPAL_CLIENT_ID || "" |
5 | | -const clientSecret = process.env.DRUPAL_CLIENT_SECRET || "" |
| 3 | +const baseUrl = process.env.NEXT_PUBLIC_DRUPAL_BASE_URL as string |
| 4 | +const clientId = process.env.DRUPAL_CLIENT_ID as string |
| 5 | +const clientSecret = process.env.DRUPAL_CLIENT_SECRET as string |
6 | 6 |
|
7 | | -export const drupal = new DrupalClient(baseUrl, { |
| 7 | +export const drupal = new NextDrupalGraphQL(baseUrl, { |
8 | 8 | auth: { |
9 | 9 | clientId, |
10 | 10 | clientSecret, |
11 | 11 | }, |
| 12 | + // debug: true, |
12 | 13 | }) |
13 | | - |
14 | | -export const graphqlEndpoint = drupal.buildUrl("/graphql") |
15 | | - |
16 | | -type QueryPayload = { |
17 | | - query: string |
18 | | - variables?: Record<string, string> |
19 | | -} |
20 | | - |
21 | | -type QueryJsonResponse<DataType> = { |
22 | | - data?: DataType |
23 | | - errors?: { message: string }[] |
24 | | -} |
25 | | - |
26 | | -// This is a wrapper around drupal.fetch. |
27 | | -// Acts as a query helper. |
28 | | -export async function query<DataType>(payload: QueryPayload) { |
29 | | - const response = await drupal.fetch(graphqlEndpoint.toString(), { |
30 | | - method: "POST", |
31 | | - body: JSON.stringify(payload), |
32 | | - withAuth: true, // Make authenticated requests using OAuth. |
33 | | - headers: { |
34 | | - "Content-Type": "application/json", |
35 | | - Accept: "application/json", |
36 | | - }, |
37 | | - }) |
38 | | - |
39 | | - if (!response?.ok) { |
40 | | - throw new Error(response.statusText) |
41 | | - } |
42 | | - |
43 | | - const { data, errors }: QueryJsonResponse<DataType> = await response.json() |
44 | | - |
45 | | - if (errors) { |
46 | | - console.log(errors) |
47 | | - throw new Error(errors?.map((e) => e.message).join("\n") ?? "unknown") |
48 | | - } |
49 | | - |
50 | | - return data |
51 | | -} |
0 commit comments