Skip to content

Commit

Permalink
feat: add specific headers to graphql client
Browse files Browse the repository at this point in the history
  • Loading branch information
uniquemo committed Jun 21, 2021
1 parent 139bf43 commit db6abcd
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,32 @@
import React from 'react'
import ReactDOM from 'react-dom'
import { ApolloClient, InMemoryCache, ApolloProvider } from '@apollo/client'
import { ApolloClient, InMemoryCache, ApolloProvider, createHttpLink } from '@apollo/client'
import { setContext } from '@apollo/client/link/context'
import App from './pages/App'
import { GRAPHQL_SERVER } from 'constants/server'

import 'normalize.css/normalize.css'
import '@blueprintjs/core/lib/css/blueprint.css'
import './styles/global.module.css'

const client = new ApolloClient({
const httpLink = createHttpLink({
uri: GRAPHQL_SERVER,
})

const authLink = setContext((_, { headers }) => {
const session = JSON.parse(localStorage.getItem('__session') || '{}')
return {
headers: {
...headers,
'netease-user-id': session.userId,
'netease-token': session.token,
'netease-nick-name': session.profile?.nickname,
},
}
})

const client = new ApolloClient({
link: authLink.concat(httpLink),
cache: new InMemoryCache(),
})

Expand Down

0 comments on commit db6abcd

Please sign in to comment.