Skip to content

Commit 56b5739

Browse files
committed
Apollo cache implemenation
1 parent 0f86148 commit 56b5739

File tree

11 files changed

+272
-10
lines changed

11 files changed

+272
-10
lines changed

package-lock.json

Lines changed: 96 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
"graphql": "^15.0.0",
1515
"react": "^16.13.1",
1616
"react-dom": "^16.13.1",
17+
"react-router-dom": "^5.2.0",
1718
"react-scripts": "3.4.1",
1819
"typescript": "~3.7.2"
1920
},

src/App.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ import React from 'react';
22
import ExchangeRates from './components/ExchangeRates';
33
import logo from './logo.svg';
44
import './App.css';
5+
import {
6+
Link
7+
} from "react-router-dom";
58

69
function App() {
710
return (
@@ -19,6 +22,9 @@ function App() {
1922
>
2023
Learn React
2124
</a>
25+
<Link to="/books">Books</Link>
26+
<Link to="/todos">Todos</Link>
27+
2228
<ExchangeRates />
2329
</header>
2430
</div>

src/apollo/client.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { ApolloClient, HttpLink, InMemoryCache } from '@apollo/client';
33
const client = new ApolloClient({
44
cache: new InMemoryCache(),
55
link: new HttpLink({
6-
uri: 'https://48p1r2roz4.sse.codesandbox.io',
6+
uri: 'http://localhost:4000',
77
})
88
});
99

src/components/Books/gql.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { gql } from '@apollo/client';
2+
3+
const GET_BOOKS = gql`
4+
{
5+
books {
6+
title
7+
author
8+
}
9+
}
10+
`;
11+
12+
const ADD_BOOK = gql`
13+
mutation addBook($title: String!, $author: String!) {
14+
addBook(title: $title, author: $author) {
15+
title
16+
author
17+
}
18+
}
19+
`;
20+
21+
export { GET_BOOKS, ADD_BOOK };

src/components/Books/index.tsx

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import React, { useRef } from 'react';
2+
import { useQuery, useMutation } from '@apollo/client';
3+
import { GET_BOOKS, ADD_BOOK } from './gql';
4+
5+
export default function Books() {
6+
const bookTitle = useRef(null);
7+
const bookAuthor = useRef(null);
8+
9+
const { loading, error, data } = useQuery(GET_BOOKS);
10+
const [addBook] = useMutation(ADD_BOOK, {
11+
update(cache, { data: { addBook } }) {
12+
const { books }: any = cache.readQuery({ query: GET_BOOKS });
13+
14+
cache.writeQuery({
15+
query: GET_BOOKS,
16+
data: {
17+
books: [...books, addBook]
18+
}
19+
});
20+
}
21+
});
22+
23+
if (loading) return <p>Loading...</p>;
24+
if (error) return <p>Error :(</p>;
25+
26+
const handleSubmit = (event) => {
27+
event.preventDefault();
28+
29+
addBook({ variables: { title: event.target.title.value, author: event.target.author.value } });
30+
31+
event.target.title = '';
32+
event.target.author = '';
33+
}
34+
35+
return (
36+
<section>
37+
<ul>
38+
{
39+
data.books.map(({ title, author }) => (
40+
<li key={title}>
41+
{title}: {author}
42+
</li>
43+
))
44+
}
45+
</ul>
46+
<form onSubmit={(e) => handleSubmit(e)} style={{ display:'flex', flexDirection: 'row' }}>
47+
<label htmlFor="title">Title:</label>
48+
<input type="text" id="title" name="title" ref={bookTitle} />
49+
<label htmlFor="author">Book:</label>
50+
<input type="text" id="author" name="author" ref={bookAuthor}/>
51+
<button type="submit">Submit</button>
52+
</form>
53+
</section>
54+
);
55+
}

src/components/ExchangeRates/gql.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ import { gql } from '@apollo/client';
22

33
const EXCHANGE_RATES = gql`
44
{
5-
rates(currency: "USD") {
6-
currency
7-
rate
5+
books {
6+
title
7+
author
88
}
99
}
1010
`;

src/components/ExchangeRates/index.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
import React from 'react';
22
import { useQuery } from '@apollo/client';
3-
import EXCHANGE_RATES from './gql';
3+
import {GET_BOOKS} from '../Books/gql';
44

55
export default function ExchangeRates() {
6-
const { loading, error, data } = useQuery(EXCHANGE_RATES);
6+
const { loading, error, data } = useQuery(GET_BOOKS);
77

88
if (loading) return <p>Loading...</p>;
99
if (error) return <p>Error :(</p>;
1010

11-
return data.rates.map(({ currency, rate }) => (
12-
<div key={currency}>
11+
return data.books.map(({ title, author }) => (
12+
<div key={title}>
1313
<p>
14-
{currency}: {rate}
14+
{title}: {author}
1515
</p>
1616
</div>
1717
));

src/components/Todos/gql.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { gql } from "@apollo/client";
2+
3+
const GET_TODOS = gql`
4+
query GetTodos {
5+
todos {
6+
id
7+
type
8+
}
9+
}
10+
`;
11+
12+
const UPDATE_TODO = gql`
13+
mutation UpdateTodo($id: String!, $type: String!) {
14+
updateTodo(id: $id, type: $type) {
15+
id
16+
type
17+
}
18+
}
19+
`;
20+
21+
export { GET_TODOS, UPDATE_TODO };

src/components/Todos/index.tsx

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import React from 'react';
2+
import { useQuery, useMutation } from '@apollo/client';
3+
import { GET_TODOS, UPDATE_TODO } from './gql';
4+
5+
export default function Todos() {
6+
const { loading, error, data } = useQuery(GET_TODOS);
7+
const [updateTodo] = useMutation(UPDATE_TODO);
8+
9+
if (loading) return <p>Loading...</p>;
10+
if (error) return <p>Error :(</p>;
11+
12+
return (
13+
<div>
14+
{
15+
data.todos.map(({ id, type }) => {
16+
let input;
17+
18+
return (
19+
<div key={id}>
20+
<p>{type}</p>
21+
<form
22+
onSubmit={e => {
23+
e.preventDefault();
24+
updateTodo({ variables: { id, type: input.value } });
25+
26+
input.value = '';
27+
}}
28+
>
29+
<input
30+
ref={node => {
31+
input = node;
32+
}}
33+
/>
34+
<button type="submit">Update Todo</button>
35+
</form>
36+
</div>
37+
);
38+
})
39+
}
40+
</div>);
41+
}

0 commit comments

Comments
 (0)