Skip to content

Commit a1b280a

Browse files
authored
Merge pull request #4154 from Juemuren/patch-2
part8d: fix 'setContext'
2 parents e3f34f0 + e6424b4 commit a1b280a

File tree

1 file changed

+7
-9
lines changed

1 file changed

+7
-9
lines changed

src/content/8/en/part8d.md

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -157,25 +157,23 @@ const App = () => {
157157
After the backend changes, creating new persons requires that a valid user token is sent with the request. In order to send the token, we have to change the way we define the *ApolloClient* object in <i>main.jsx</i> a little.
158158

159159
```js
160-
import { ApolloClient, InMemoryCache, createHttpLink } from '@apollo/client' // highlight-line
160+
import { ApolloClient, InMemoryCache, HttpLink, } from '@apollo/client' // highlight-line
161161
import { ApolloProvider } from '@apollo/client/react'
162-
import { SetContextLink } from '@apollo/client/link/context' // highlight-line
162+
import { SetContextLink } from '@apollo/client/link/context'
163163

164164
// highlight-start
165-
const authLink = new SetContextLink(({ headers }) => {
165+
const authLink = new SetContextLink((preContext) => {
166166
const token = localStorage.getItem('phonenumbers-user-token')
167167
return {
168168
headers: {
169-
...headers,
170-
authorization: token ? `Bearer ${token}` : null,
171-
}
169+
...preContext.headers,
170+
authorization: token ? `Bearer ${token}` : '',
171+
},
172172
}
173173
})
174174
// highlight-end
175175

176-
const httpLink = createHttpLink({
177-
uri: 'http://localhost:4000',
178-
})
176+
const httpLink = new HttpLink({ uri: 'http://localhost:4000' })
179177

180178
const client = new ApolloClient({
181179
cache: new InMemoryCache(),

0 commit comments

Comments
 (0)