Skip to content

Commit e216426

Browse files
authored
Merge pull request #19 from javascripteverywhere/apollo3
Migrate examples to Apollo Client 3
2 parents 23bdb9f + 3071358 commit e216426

46 files changed

Lines changed: 208 additions & 400 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

final/App.js

Lines changed: 30 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,50 @@
11
import React from 'react';
22
import ReactDOM from 'react-dom';
3-
import { ApolloProvider } from '@apollo/react-hooks';
4-
import ApolloClient, { InMemoryCache } from 'apollo-boost';
3+
import {
4+
ApolloClient,
5+
ApolloProvider,
6+
createHttpLink,
7+
InMemoryCache
8+
} from '@apollo/client';
9+
import { setContext } from 'apollo-link-context';
510

611
// import global styles
712
import GlobalStyle from '/components/GlobalStyle';
813
// import our routes
914
import Pages from '/pages';
1015

16+
// configure our API URI & cache
1117
const uri = process.env.API_URI;
18+
const httpLink = createHttpLink({ uri });
1219
const cache = new InMemoryCache();
1320

14-
// configure Apollo Client
21+
// return the headers to the context
22+
const authLink = setContext((_, { headers }) => {
23+
return {
24+
headers: {
25+
...headers,
26+
authorization: localStorage.getItem('token') || ''
27+
}
28+
};
29+
});
30+
31+
// create the Apollo client
1532
const client = new ApolloClient({
33+
link: authLink.concat(httpLink),
1634
cache,
17-
uri,
18-
clientState: {
19-
resolvers: {},
20-
connectToDevTools: true
21-
},
22-
request: operation => {
23-
operation.setContext({
24-
headers: {
25-
authorization: localStorage.getItem('token') || ''
26-
}
27-
});
28-
}
35+
resolvers: {},
36+
connectToDevTools: true
2937
});
3038

31-
const initialCache = new Object({
32-
data: {
33-
isLoggedIn: !!localStorage.getItem('token')
34-
}
35-
});
39+
// check for a local token
40+
const data = {
41+
isLoggedIn: !!localStorage.getItem('token')
42+
};
3643

3744
// write the cache data on initial load
38-
cache.writeData(initialCache);
39-
// write the cache data after the stored cache is reset
40-
client.onResetStore(() => cache.writeData(initialCache));
45+
cache.writeData({ data });
46+
// write the cache data after cache is reset
47+
client.onResetStore(() => cache.writeData({ data }));
4148

4249
const App = () => {
4350
return (

final/components/DeleteNote.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React from 'react';
2-
import { useMutation } from '@apollo/react-hooks';
2+
import { useMutation } from '@apollo/client';
33
import { withRouter } from 'react-router-dom';
44

55
import ButtonAsLink from './ButtonAsLink';

final/components/FavoriteNote.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React, { useState } from 'react';
2-
import { useMutation } from '@apollo/react-hooks';
2+
import { useMutation } from '@apollo/client';
33

44
import ButtonAsLink from './ButtonAsLink';
55
import { TOGGLE_FAVORITE } from '../gql/mutation';

final/components/Header.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React from 'react';
22
import styled from 'styled-components';
33
import logo from '../img/logo.svg';
4-
import { useQuery } from '@apollo/react-hooks';
4+
import { useQuery } from '@apollo/client';
55
import { Link, withRouter } from 'react-router-dom';
66

77
import ButtonAsLink from './ButtonAsLink';

final/components/Note.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@ import React from 'react';
22
import ReactMarkdown from 'react-markdown';
33
import { format } from 'date-fns';
44
import styled from 'styled-components';
5-
import { useQuery } from '@apollo/react-hooks';
6-
import { gql } from 'apollo-boost';
5+
import { useQuery } from '@apollo/client';
76

87
import NoteUser from './NoteUser';
98
import { IS_LOGGED_IN } from '../gql/query';

final/components/NoteUser.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import React from 'react';
2-
import { useQuery } from '@apollo/react-hooks';
3-
import { gql } from 'apollo-boost';
2+
import { useQuery } from '@apollo/client';
43
import { Link } from 'react-router-dom';
54

65
import DeleteNote from './DeleteNote';

final/gql/mutation.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { gql } from 'apollo-boost';
1+
import { gql } from '@apollo/client';
22

33
const NEW_NOTE = gql`
44
mutation newNote($content: String!) {

final/gql/query.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { gql } from 'apollo-boost';
1+
import { gql } from '@apollo/client';
22

33
const GET_NOTES = gql`
44
query noteFeed($cursor: String) {
@@ -33,9 +33,6 @@ const GET_NOTE = gql`
3333
avatar
3434
}
3535
}
36-
me {
37-
id
38-
}
3936
}
4037
`;
4138

final/pages/edit.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
import React from 'react';
2-
import { useMutation, useQuery } from '@apollo/react-hooks';
2+
import { useMutation, useQuery } from '@apollo/client';
33

44
// import the NoteForm component
55
import NoteForm from '../components/NoteForm';
6-
import { GET_NOTE } from '../gql/query';
6+
import { GET_NOTE, GET_ME } from '../gql/query';
77
import { EDIT_NOTE } from '../gql/mutation';
88

99
const EditNote = props => {
1010
// store the id found in the url as a variable
1111
const id = props.match.params.id;
1212
// define our note query
1313
const { loading, error, data } = useQuery(GET_NOTE, { variables: { id } });
14+
const { data: userdata } = useQuery(GET_ME);
1415
// define our mutation
1516
const [editNote] = useMutation(EDIT_NOTE, {
1617
variables: {
@@ -26,7 +27,7 @@ const EditNote = props => {
2627
// if there is an error fetching the data, display an error message
2728
if (error) return <p>Error!</p>;
2829
// if the current user and the author of the note do not match
29-
if (data.me.id !== data.note.author.id) {
30+
if (userdata.me.id !== data.note.author.id) {
3031
return <p>You do not have access to edit this note</p>;
3132
}
3233

final/pages/favorites.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React, { useEffect } from 'react';
2-
import { useQuery } from '@apollo/react-hooks';
2+
import { useQuery } from '@apollo/client';
33

44
import NoteFeed from '../components/NoteFeed';
55
import { GET_MY_FAVORITES } from '../gql/query';

0 commit comments

Comments
 (0)