Skip to content

Commit

Permalink
Added Boilerplate for Next 13 / Apollo / GraphQL app via apollographq…
Browse files Browse the repository at this point in the history
  • Loading branch information
lukemiller01 committed Mar 6, 2023
1 parent 5955d4f commit bb7bbe7
Show file tree
Hide file tree
Showing 7 changed files with 348 additions and 2 deletions.
4 changes: 4 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"typescript.tsdk": "node_modules/typescript/lib",
"typescript.enablePromptUseWorkspaceTsdk": true
}
32 changes: 32 additions & 0 deletions app/ApolloClient.ts
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,
});
18 changes: 18 additions & 0 deletions app/ApolloExtractCache.tsx
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;
8 changes: 8 additions & 0 deletions app/ApolloProviderWrapper.tsx
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>;
}
12 changes: 10 additions & 2 deletions app/layout.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import './globals.css'
import './globals.css';
import ApolloExtractCache from "./ApolloExtractCache";
import Provider from './ApolloProviderWrapper';

export const metadata = {
title: 'Create Next App',
Expand All @@ -12,7 +14,13 @@ export default function RootLayout({
}) {
return (
<html lang="en">
<body>{children}</body>
<body>
<Provider>
{children}
{/* @ts-expect-error Server Component */}
<ApolloExtractCache />
</Provider>
</body>
</html>
)
}
Loading

0 comments on commit bb7bbe7

Please sign in to comment.