This repository has been archived by the owner on Feb 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: switch to apollo client (#15)
Co-authored-by: Samer Buna <[email protected]>
- Loading branch information
Showing
22 changed files
with
243 additions
and
133 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
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 was deleted.
Oops, something went wrong.
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,16 +1,25 @@ | ||
import "@testing-library/jest-dom/extend-expect" | ||
|
||
import { render } from "@testing-library/react" | ||
import { MockedProvider } from "@apollo/client/testing" | ||
|
||
import Root from "./root" | ||
|
||
describe("Root", () => { | ||
it("renders Home and matches snapshot", () => { | ||
const { asFragment } = render(<Root initialState={{ path: "/" }} />) | ||
const { asFragment } = render( | ||
<MockedProvider> | ||
<Root initialState={{ path: "/" }} /> | ||
</MockedProvider>, | ||
) | ||
expect(asFragment()).toMatchSnapshot() | ||
}) | ||
it("renders Login and matches snapshot", () => { | ||
const { asFragment } = render(<Root initialState={{ path: "/login" }} />) | ||
const { asFragment } = render( | ||
<MockedProvider> | ||
<Root initialState={{ path: "/login" }} /> | ||
</MockedProvider>, | ||
) | ||
expect(asFragment()).toMatchSnapshot() | ||
}) | ||
}) |
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,16 +1,20 @@ | ||
import * as ReactDOM from "react-dom" | ||
|
||
import Root from "components/root" | ||
import { ApolloProvider } from "@apollo/client" | ||
|
||
import "../styles/index.css" | ||
import { ssr } from "store" | ||
|
||
import Root from "components/root" | ||
import client from "store/client" | ||
|
||
const container = document.getElementById("root") | ||
|
||
if (!container) { | ||
throw new Error("HTML_ROOT_ELEMENT_IS_MISSING") | ||
} | ||
|
||
ssr.restoreData(window.__G_DATA.ssrData) | ||
|
||
ReactDOM.hydrateRoot(container, <Root initialState={window.__G_DATA.initialState} />) | ||
ReactDOM.hydrateRoot( | ||
container, | ||
<ApolloProvider client={client}> | ||
<Root initialState={window.__G_DATA.initialState} /> | ||
</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
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,17 +1,20 @@ | ||
import { createClient } from "@urql/core" | ||
import { Request } from "express" | ||
import { ApolloClient, createHttpLink, InMemoryCache } from "@apollo/client" | ||
|
||
import config from "./config" | ||
|
||
const client = (req: Request) => | ||
createClient({ | ||
url: config.graphqlUri, | ||
fetchOptions: () => { | ||
const token = req.session?.authToken | ||
return { | ||
headers: { authorization: token ? `Bearer ${token}` : "" }, | ||
} | ||
}, | ||
const client = (req: Request) => { | ||
const authToken = req.session?.authToken | ||
return new ApolloClient({ | ||
ssrMode: true, | ||
link: createHttpLink({ | ||
uri: config.graphqlUri, | ||
headers: { | ||
authorization: authToken ? `Bearer ${authToken}` : "", | ||
}, | ||
}), | ||
cache: new InMemoryCache(), | ||
}) | ||
} | ||
|
||
export default client |
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,30 @@ | ||
import { ApolloClient, InMemoryCache, ApolloLink, from, HttpLink } from "@apollo/client" | ||
|
||
import config from "server/config" | ||
import { getCachedAuthToken, setCachedAuthToken } from "./use-auth-token" | ||
|
||
export const cache = new InMemoryCache().restore(window.__G_DATA.ssrData) | ||
|
||
const authLink = new ApolloLink((operation, forward) => { | ||
const user = getCachedAuthToken(cache) | ||
operation.setContext(({ headers }: { headers: Record<string, string> }) => ({ | ||
headers: { | ||
authorization: user ? `Bearer ${user.authToken}` : "", | ||
...headers, | ||
}, | ||
})) | ||
return forward(operation) | ||
}) | ||
|
||
const httpLink = new HttpLink({ uri: config.graphqlUri }) | ||
|
||
const client = new ApolloClient({ | ||
cache, | ||
link: from([authLink, httpLink]), | ||
}) | ||
|
||
if (window.__G_DATA.initialState.authToken) { | ||
setCachedAuthToken(client)(window.__G_DATA.initialState.authToken) | ||
} | ||
|
||
export default client |
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.