Skip to content

Commit af10902

Browse files
authored
Merge pull request #43 from naveteam/feat/add-apollo-client
feat(providers): add apollo client
2 parents ebfc228 + 496756c commit af10902

6 files changed

Lines changed: 90 additions & 9 deletions

File tree

package.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,19 @@
66
"version": "0.1.0",
77
"private": true,
88
"dependencies": {
9+
"@apollo/client": "^3.3.7",
910
"@naveteam/dispatcher": "^1.1.0",
1011
"@naveteam/pandora-frontend": "^1.0.2",
1112
"@sentry/browser": "5.14",
1213
"@styled-system/prop-types": "^5.1.5",
1314
"@welldone-software/why-did-you-render": "^6.0.0-rc.1",
15+
"apollo-cache-inmemory": "^1.6.6",
1416
"axios": "^0.19.0",
17+
"graphql": "^15.5.0",
18+
"graphql-anywhere": "^4.2.7",
19+
"graphql-tag": "^2.11.0",
1520
"react": "^16.9.0",
21+
"react-apollo": "^3.1.5",
1622
"react-app-polyfill": "^1.0.6",
1723
"react-dom": "^16.9.0",
1824
"react-helmet": "^5.2.1",

src/App.js

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@ import React, { useEffect, Suspense, lazy } from 'react'
22
import { BrowserRouter as Router } from 'react-router-dom'
33
import { ReactQueryDevtools } from 'react-query-devtools'
44
import { createGlobalStyle } from 'styled-components'
5+
import { ApolloProvider } from '@apollo/client'
56
import Helmet from 'react-helmet'
67

78
import Loader from 'components/Loader'
89

910
import { useUser } from 'context/user-context'
11+
import { apolloClient } from 'providers'
1012

1113
import Theme from 'theme'
1214

@@ -41,15 +43,17 @@ const App = () => {
4143
}, [])
4244

4345
return (
44-
<Theme>
45-
<Helmet titleTemplate='Nave.rs | %s' />
46-
<GlobalStyle />
47-
<Suspense fallback={<Loader />}>
48-
{isLoading && <Loader />}
49-
<Router>{user ? <AuthenticatedApp /> : <UnauthenticatedApp />}</Router>
50-
</Suspense>
51-
<ReactQueryDevtools initialIsOpen={false} />
52-
</Theme>
46+
<ApolloProvider client={apolloClient}>
47+
<Theme>
48+
<Helmet titleTemplate='Nave.rs | %s' />
49+
<GlobalStyle />
50+
<Suspense fallback={<Loader />}>
51+
{isLoading && <Loader />}
52+
<Router>{user ? <AuthenticatedApp /> : <UnauthenticatedApp />}</Router>
53+
</Suspense>
54+
<ReactQueryDevtools initialIsOpen={false} />
55+
</Theme>
56+
</ApolloProvider>
5357
)
5458
}
5559

src/providers/apolloClient.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import { ApolloClient, InMemoryCache, HttpLink, ApolloLink } from '@apollo/client'
2+
import { onError } from '@apollo/client/link/error'
3+
4+
import { getToken } from 'helpers'
5+
6+
const httpLink = new HttpLink({ uri: process.env.REACT_APP_API_URL })
7+
8+
const authMiddleware = new ApolloLink((operation, forward) => {
9+
const token = getToken()
10+
11+
operation.setContext(({ headers = {} }) => ({
12+
headers: {
13+
...headers,
14+
...(token && { authorization: `Bearer: ${token}` })
15+
}
16+
}))
17+
18+
return forward(operation)
19+
})
20+
21+
const errorMiddleware = onError(({ graphQLErrors, networkError }) => {
22+
if (graphQLErrors)
23+
graphQLErrors.forEach(error => {
24+
return Promise.reject(error)
25+
})
26+
27+
if (
28+
networkError.statusCode === 401 &&
29+
!['/login', '/redefine-password', '/sign-up'].includes(window.location.pathname)
30+
) {
31+
window.location.href = '/login'
32+
}
33+
})
34+
35+
const client = new ApolloClient({
36+
cache: new InMemoryCache(),
37+
link: ApolloLink.from([authMiddleware, errorMiddleware, httpLink])
38+
})
39+
40+
export default client

src/providers/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export { default as apolloClient } from './apolloClient'
2+
export { default as client } from './fetchClient'

src/routes/Home/Home.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
import React, { Fragment } from 'react'
22
import { Link } from 'react-router-dom'
3+
import { useQuery } from '@apollo/client'
34

45
import Button from 'components/Button'
56

67
import { useUser } from 'context/user-context'
8+
import { GET_USER } from 'services/auth'
79

810
const Home = () => {
11+
// example of request using apollo
12+
const { loading, error, data } = useQuery(GET_USER)
913
const { logout } = useUser()
1014

1115
return (

src/services/auth.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,30 @@
11
import client from 'providers/fetchClient'
2+
import { gql } from '@apollo/client'
23

34
export const getUser = () => client.get('/v1/me')
45

56
export const login = data => client.post('/v1/users/login', data)
7+
8+
//example of rest query using graphql
9+
export const userQuery = gql`
10+
query getUser {
11+
me @rest(type: User, path: "/v1/me") {
12+
id
13+
name
14+
email
15+
document
16+
}
17+
}
18+
`
19+
20+
// example of a graphql query
21+
export const GET_USER = gql`
22+
query GetUser {
23+
user {
24+
id
25+
name
26+
email
27+
document
28+
}
29+
}
30+
`

0 commit comments

Comments
 (0)