-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: refactor Remix example Contentful client to play nice with serve…
…r vs client
- Loading branch information
1 parent
0bad8c2
commit 34fa562
Showing
5 changed files
with
98 additions
and
86 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,43 @@ | ||
import { GraphQLClient } from 'graphql-request'; | ||
import type { QueryResponse, Variables } from '../types' | ||
|
||
const SPACE = process.env.CONTENTFUL_SPACE_ID; | ||
const fetchData = async (spaceId: string, accessToken: string, query: string, variables: Variables, preview: boolean) => { | ||
const client = new GraphQLClient(`https://graphql.contentful.com/content/v1/spaces/${spaceId}`); | ||
const data = (await client.request( | ||
query, | ||
variables, | ||
{ authorization: `Bearer ${accessToken}` } | ||
)) as QueryResponse; | ||
|
||
const endpoint = `https://graphql.contentful.com/content/v1/spaces/${SPACE}`; | ||
return data; | ||
} | ||
|
||
export const contentful = new GraphQLClient(endpoint); | ||
// The `spaceId` and `accessToken` are passed as arguments from a `loader` function | ||
// to these utility functions. This approach avoids browser errors that occur when | ||
// trying to access `process.env` from this file, even though it is not executed in | ||
// the browser. This is likely a Remix bundling issue. | ||
export const getEntryBySlug = async ({ | ||
spaceId, | ||
accessToken, | ||
query, | ||
slug, | ||
preview, | ||
}: { | ||
spaceId: string, | ||
accessToken: string, | ||
query: string, | ||
slug: string, | ||
preview: boolean, | ||
}) => fetchData(spaceId, accessToken, query, { slug, preview }, preview); | ||
|
||
export const getEntries = async ({ | ||
spaceId, | ||
accessToken, | ||
query, | ||
preview, | ||
}: { | ||
spaceId: string, | ||
accessToken: string, | ||
query: string, | ||
preview: boolean, | ||
}) => fetchData(spaceId, accessToken, query, { preview }, preview); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
export type Post = { | ||
title: string; | ||
description: string; | ||
slug: string; | ||
sys: { | ||
id: string; | ||
}; | ||
}; | ||
|
||
export type LoaderData = { | ||
post: Post; | ||
posts: Post[]; | ||
preview: boolean; | ||
}; | ||
|
||
export type QueryResponse = { | ||
postCollection: { | ||
items: Post[]; | ||
}; | ||
}; | ||
|
||
export type Variables = { | ||
[key: string]: string | boolean; | ||
}; |