-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add ApolloWrapper as Client Component to fetch GraphQL data wit…
…h using apollo-client.
- Loading branch information
1 parent
19bad6b
commit 6e18235
Showing
6 changed files
with
313 additions
and
154 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
'use client' | ||
|
||
import { HttpLink, ApolloLink } from '@apollo/client' | ||
import { | ||
NextSSRApolloClient, | ||
ApolloNextAppProvider, | ||
NextSSRInMemoryCache, | ||
SSRMultipartLink | ||
} from '@apollo/experimental-nextjs-app-support/ssr' | ||
|
||
function apiEndpoint(): { uri: string; headers?: Record<string, string> } { | ||
if (process.env.NODE_ENV === 'development') { | ||
return { uri: 'http://127.0.0.1:54321/graphql/v1' } | ||
} else { | ||
return { | ||
uri: process.env.GRAPHQL_ENDPOINT as string, | ||
headers: { | ||
apiKey: process.env.API_KEY as string | ||
} | ||
} | ||
} | ||
} | ||
|
||
function makeClient() { | ||
const httpLink = new HttpLink(apiEndpoint()) | ||
|
||
return new NextSSRApolloClient({ | ||
cache: new NextSSRInMemoryCache(), | ||
link: | ||
typeof window === 'undefined' | ||
? ApolloLink.from([ | ||
new SSRMultipartLink({ | ||
stripDefer: true | ||
}), | ||
httpLink | ||
]) | ||
: httpLink | ||
}) | ||
} | ||
|
||
export function ApolloWrapper({ children }: React.PropsWithChildren) { | ||
return ( | ||
<ApolloNextAppProvider makeClient={makeClient}> | ||
{children} | ||
</ApolloNextAppProvider> | ||
) | ||
} |
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
Oops, something went wrong.