Skip to content

Commit 4512092

Browse files
authored
part8d: fix 'setContext'
‘setContext’ is deprecated in Apollo Client 4, replace it with ’SetContextLink‘
1 parent 03944a7 commit 4512092

File tree

1 file changed

+8
-9
lines changed

1 file changed

+8
-9
lines changed

src/content/8/en/part8d.md

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -164,24 +164,23 @@ const App = () => {
164164
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.
165165

166166
```js
167-
import { ApolloClient, InMemoryCache, ApolloProvider, createHttpLink } from '@apollo/client' // highlight-line
168-
import { setContext } from '@apollo/client/link/context' // highlight-line
167+
import { ApolloClient, InMemoryCache, HttpLink, } from '@apollo/client' // highlight-line
168+
import { ApolloProvider } from '@apollo/client/react'
169+
import { SetContextLink } from '@apollo/client/link/context'
169170

170171
// highlight-start
171-
const authLink = setContext((_, { headers }) => {
172+
const authLink = new SetContextLink((preContext) => {
172173
const token = localStorage.getItem('phonenumbers-user-token')
173174
return {
174175
headers: {
175-
...headers,
176-
authorization: token ? `Bearer ${token}` : null,
177-
}
176+
...preContext.headers,
177+
authorization: token ? `Bearer ${token}` : '',
178+
},
178179
}
179180
})
180181
// highlight-end
181182

182-
const httpLink = createHttpLink({
183-
uri: 'http://localhost:4000',
184-
})
183+
const httpLink = new HttpLink({ uri: 'http://localhost:4000' })
185184

186185
const client = new ApolloClient({
187186
cache: new InMemoryCache(),

0 commit comments

Comments
 (0)