-
Notifications
You must be signed in to change notification settings - Fork 162
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Handle client termination and recreation (#1303)
- Loading branch information
1 parent
d96a9fd
commit df0d242
Showing
6 changed files
with
129 additions
and
65 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,5 @@ | ||
--- | ||
"apollo-client-devtools": patch | ||
--- | ||
|
||
Fix issue where terminating the client by calling `.stop` would not disconnect it from devtools making it difficult to track newly created clients. |
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,37 +1,109 @@ | ||
import React from "react"; | ||
import { BrowserRouter as Router, Routes, Route, Link } from "react-router-dom"; | ||
import { useQuery } from "@apollo/client"; | ||
import React, { useState } from "react"; | ||
import { BrowserRouter, Routes, Route, Link } from "react-router-dom"; | ||
import { | ||
ApolloClient, | ||
ApolloProvider, | ||
InMemoryCache, | ||
makeReference, | ||
useQuery, | ||
} from "@apollo/client"; | ||
import ColorSchemeGenerator from "./ColorSchemeGenerator"; | ||
import Favorites from "./Favorites"; | ||
import ColorLookup from "./ColorLookup"; | ||
import { GET_SAVED_COLORS } from "./queries"; | ||
import "./App.css"; | ||
|
||
function App() { | ||
useQuery(GET_SAVED_COLORS); | ||
const [client, setClient] = useState(() => createClient()); | ||
|
||
return ( | ||
<Router> | ||
<div className="App"> | ||
<header> | ||
<Link to="/"> | ||
<h1>Colors</h1> | ||
</Link> | ||
<nav> | ||
<Link to="/favorites">Favorites</Link> | ||
<Link to="/lookup">Lookup</Link> | ||
</nav> | ||
</header> | ||
<main> | ||
<Routes> | ||
<Route path="/favorites" element={<Favorites />} /> | ||
<Route path="/lookup" element={<ColorLookup />} /> | ||
<Route path="/" element={<ColorSchemeGenerator />} /> | ||
</Routes> | ||
</main> | ||
if (!client) { | ||
return ( | ||
<div style={{ textAlign: "center" }}> | ||
<h1>Client was terminated</h1> | ||
<button onClick={() => setClient(createClient())}> | ||
Recreate client | ||
</button> | ||
</div> | ||
</Router> | ||
); | ||
} | ||
|
||
return ( | ||
<ApolloProvider client={client}> | ||
<BrowserRouter> | ||
<Layout onChangeClient={(client) => setClient(client)} /> | ||
</BrowserRouter> | ||
</ApolloProvider> | ||
); | ||
} | ||
|
||
function Layout({ onChangeClient }) { | ||
const { client } = useQuery(GET_SAVED_COLORS); | ||
|
||
return ( | ||
<div className="App"> | ||
<header style={{ display: "flex" }}> | ||
<Link to="/"> | ||
<h1>Colors</h1> | ||
</Link> | ||
<nav style={{ flex: 1 }}> | ||
<Link to="/favorites">Favorites</Link> | ||
<Link to="/lookup">Lookup</Link> | ||
</nav> | ||
<div style={{ display: "flex", gap: "1rem" }}> | ||
<button | ||
onClick={() => { | ||
client.stop(); | ||
onChangeClient(createClient()); | ||
}} | ||
> | ||
Recreate client | ||
</button> | ||
<button | ||
onClick={() => { | ||
client.stop(); | ||
onChangeClient(null); | ||
}} | ||
> | ||
Terminate client | ||
</button> | ||
</div> | ||
</header> | ||
<main> | ||
<Routes> | ||
<Route path="/favorites" element={<Favorites />} /> | ||
<Route path="/lookup" element={<ColorLookup />} /> | ||
<Route path="/" element={<ColorSchemeGenerator />} /> | ||
</Routes> | ||
</main> | ||
</div> | ||
); | ||
} | ||
|
||
function createClient() { | ||
return new ApolloClient({ | ||
cache: new InMemoryCache({ | ||
typePolicies: { | ||
Color: { | ||
keyFields: ["hex"], | ||
fields: { | ||
saved: { | ||
read(_, { readField }) { | ||
const hex = readField("hex"); | ||
const favoritedColors = readField( | ||
"favoritedColors", | ||
makeReference("ROOT_QUERY") | ||
); | ||
return favoritedColors.some((colorRef) => { | ||
return hex === readField("hex", colorRef); | ||
}); | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}), | ||
uri: "http://localhost:4000", | ||
}); | ||
} | ||
|
||
export default App; |
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