Skip to content

Commit

Permalink
Import draft mode context
Browse files Browse the repository at this point in the history
  • Loading branch information
Matt Gordon committed May 20, 2024
1 parent 2b7f88f commit 897b77d
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions lib/graphql/fetch/create-fetch.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { draftMode } from 'next/headers';

interface CreateFetchOptions {
spaceId: string;
accessToken: string;
Expand All @@ -9,7 +11,6 @@ interface FetchGragphQLOptions {
variables: {
[key: string]: string | object | boolean | number | undefined;
};
preview?: boolean;
tags?: Array<string>;
revalidate?: number;
}
Expand All @@ -20,15 +21,16 @@ export function createFetch({
previewToken,
}: CreateFetchOptions) {
async function fetchGraphQL(options: FetchGragphQLOptions) {
const { query, variables, preview, tags, revalidate } = options;
const { isEnabled } = draftMode();
const { query, variables, tags, revalidate } = options;

const res = await fetch(
`https://graphql.contentful.com/content/v1/spaces/${spaceId}`,
{
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${preview ? previewToken : accessToken}`,
Authorization: `Bearer ${isEnabled ? previewToken : accessToken}`,
},
body: JSON.stringify({
query,
Expand All @@ -41,7 +43,7 @@ export function createFetch({
},
}
: {}),
cache: preview ? 'no-store' : 'force-cache',
cache: isEnabled ? 'no-store' : 'force-cache',
} as RequestInit,
);

Expand Down

0 comments on commit 897b77d

Please sign in to comment.