Skip to content

Commit 3ab107c

Browse files
committed
fix(projects): simplified useApolloClient hook
Previously, in order to use the Github API client, you had to: 1. Get the GitHub token from the appState 2. Call the `useApolloClient` hook and get the `initApolloClient` function 3. Call the `initApolloClient` function with the GitHub token as an argument and get the client But since `useApolloClient` is a hook, this commit simplifies it so that the appState is accessed directly from within the hook, and the actual API client is returned directly, so that the above steps get simplified to: 1. Call the `useApolloClient` hook and get the client
1 parent 1ed9b9b commit 3ab107c

File tree

5 files changed

+34
-44
lines changed

5 files changed

+34
-44
lines changed

apps/projects/app/App.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ const App = () => {
4040
const [ githubLoading, setGithubLoading ] = useState(false)
4141
const [ panel, setPanel ] = useState(null)
4242
const [ panelProps, setPanelProps ] = useState(null)
43-
const initApolloClient = useApolloClient()
43+
const client = useApolloClient()
4444

4545
const {
4646
repos = [],
@@ -57,8 +57,6 @@ const App = () => {
5757
selectIssue: setSelectedIssue,
5858
} = usePathSegments()
5959

60-
const client = initApolloClient(github.token)
61-
6260
useEffect(() => {
6361
setSelectedIssue(selectedIssueId)
6462
}, [selectedIssueId])

apps/projects/app/components/Content/IssueDetail/index.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
import React, { useMemo } from 'react'
1+
import React from 'react'
22
import PropTypes from 'prop-types'
3-
import { useAragonApi } from '../../../api-react'
43
import { useLayout } from '@aragon/ui'
54
import { useQuery } from '@apollo/react-hooks'
65
import useShapedIssue from '../../../hooks/useShapedIssue'
@@ -11,9 +10,7 @@ import DetailsCard from './DetailsCard'
1110
import BountyCard from './BountyCard'
1211

1312
const IssueDetail = ({ issueId }) => {
14-
const { appState: { github } } = useAragonApi()
15-
const initApolloClient = useApolloClient()
16-
const client = useMemo(() => initApolloClient(github.token), [])
13+
const client = useApolloClient()
1714
const { layoutName } = useLayout()
1815
const shapeIssue = useShapedIssue()
1916
const { loading, error, data } = useQuery(GET_ISSUE, {

apps/projects/app/components/Content/Issues.js

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -375,11 +375,10 @@ IssuesQuery.propTypes = {
375375
}
376376

377377
const IssuesWrap = props => {
378-
const initApolloClient = useApolloClient()
379-
const { appState: { github, repos } } = useAragonApi()
378+
const client = useApolloClient()
379+
const { appState: { repos } } = useAragonApi()
380380
const shapeIssue = useShapedIssue()
381381
const { query: { repoId } } = usePathSegments()
382-
const [ client, setClient ] = useState(null)
383382
const [ downloadedRepos, setDownloadedRepos ] = useState({})
384383
const [ query, setQuery ] = useState(null)
385384
const [ filters, setFilters ] = useState({
@@ -423,11 +422,6 @@ const IssuesWrap = props => {
423422
setQuery(getIssuesGQL(reposQueryParams))
424423
}, [ downloadedRepos, filters, repos ])
425424

426-
useEffect(() => {
427-
const apolloClient = initApolloClient(github.token)
428-
setClient(apolloClient)
429-
}, [github.token])
430-
431425
if (!query) return 'Loading...'
432426

433427
if (!client) return 'You must sign into GitHub to view issues.'

apps/projects/app/hooks/useGithubAuth.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@ import { CURRENT_USER } from '../utils/gql-queries'
55

66
const useGithubAuth = () => {
77
const { appState } = useAragonApi()
8-
const initApolloClient = useApolloClient()
8+
const client = useApolloClient()
99
const token = appState.github && appState.github.token
1010

1111
const [ githubCurrentUser, setGithubCurrentUser ] = useState({})
1212

1313
useEffect(() => {
1414
if (!token) return
1515

16-
initApolloClient(token)
16+
client
1717
.query({ query: CURRENT_USER })
1818
.then(res => setGithubCurrentUser(res.data.viewer))
1919
}, [token])

apps/projects/app/utils/apollo-client.js

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,31 +4,32 @@ import { GITHUB_TOKEN_REVOKED } from '../store/eventTypes'
44
import { STATUS } from '../utils/github'
55

66
export const useApolloClient = () => {
7-
const { api } = useAragonApi()
8-
const initApolloClient = token => {
9-
if (token === null) return null
10-
return new ApolloClient({
11-
uri: 'https://api.github.com/graphql',
12-
request: operation => {
13-
if (token) {
14-
operation.setContext({
15-
headers: {
16-
accept: 'application/vnd.github.starfire-preview+json', // needed to create issues
17-
authorization: `bearer ${token}`,
18-
},
19-
})
20-
}
21-
},
22-
onError: error => {
23-
if (error.networkError && error.networkError.statusCode === 401) {
24-
api.emitTrigger(GITHUB_TOKEN_REVOKED, {
25-
status: STATUS.REVOKED,
26-
scope: null,
27-
token: null,
28-
})
29-
}
7+
const {
8+
api,
9+
appState: {
10+
github: { token } = { token: null }
11+
}
12+
} = useAragonApi()
13+
if (token === null) return null
14+
return new ApolloClient({
15+
uri: 'https://api.github.com/graphql',
16+
request: operation => {
17+
operation.setContext({
18+
headers: {
19+
accept: 'application/vnd.github.starfire-preview+json', // needed to create issues
20+
authorization: `bearer ${token}`,
21+
},
22+
})
23+
},
24+
onError: error => {
25+
if (error.networkError && error.networkError.statusCode === 401) {
26+
api.emitTrigger(GITHUB_TOKEN_REVOKED, {
27+
status: STATUS.REVOKED,
28+
scope: null,
29+
token: null,
30+
})
3031
}
31-
})
32-
}
33-
return initApolloClient
32+
else console.error(error)
33+
}
34+
})
3435
}

0 commit comments

Comments
 (0)