-
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.
Added Boilerplate for Next 13 / Apollo / GraphQL app via apollographq…
- Loading branch information
1 parent
5955d4f
commit bb7bbe7
Showing
7 changed files
with
348 additions
and
2 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,4 @@ | ||
{ | ||
"typescript.tsdk": "node_modules/typescript/lib", | ||
"typescript.enablePromptUseWorkspaceTsdk": true | ||
} |
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,32 @@ | ||
import { ApolloClient as BaseApolloClient, InMemoryCache } from "@apollo/client"; | ||
import { ApolloClientOptions } from "@apollo/client/core/ApolloClient"; | ||
|
||
// Trying to find MVP | ||
let global:any = {}; | ||
|
||
try { | ||
global = window; | ||
} catch { | ||
|
||
} | ||
|
||
class ApolloClient<TCacheShape> extends BaseApolloClient<TCacheShape> { | ||
constructor(options: ApolloClientOptions<TCacheShape>) { | ||
super(options); | ||
} | ||
|
||
public activeQueries: ReturnType<ApolloClient<TCacheShape>["query"]>[] = []; | ||
|
||
query: BaseApolloClient<TCacheShape>["query"] = (options) => { | ||
const promise = super.query(options); | ||
this.activeQueries.push(promise); | ||
return promise; | ||
}; | ||
} | ||
|
||
export const apolloClient = new ApolloClient({ | ||
ssrMode: true, | ||
uri: "http://localhost:3000/graphql", | ||
cache: new InMemoryCache().restore(global.__APOLLO_STATE__), | ||
connectToDevTools: true, | ||
}); |
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,18 @@ | ||
import React from "react"; | ||
import { apolloClient } from './ApolloClient' | ||
|
||
const ApolloExtractCache = async () => { | ||
await Promise.all(apolloClient.activeQueries); | ||
|
||
return ( | ||
<script | ||
dangerouslySetInnerHTML={{ | ||
__html: `window.__APOLLO_STATE__=${JSON.stringify( | ||
apolloClient.extract() | ||
).replace(/</g, "\\u003c")};`, | ||
}} | ||
/> | ||
); | ||
}; | ||
|
||
export default ApolloExtractCache; |
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,8 @@ | ||
'use client'; | ||
|
||
import { ApolloProvider } from '@apollo/client'; | ||
import { apolloClient } from './ApolloClient'; | ||
|
||
export default function Provider({ children }: { children: React.ReactNode }) { | ||
return <ApolloProvider client={apolloClient}>{children}</ApolloProvider>; | ||
} |
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.