我使用uploadLink来设置用于向我的Apollo服务器发出请求的标头(请参见下面的代码)。但是,登录后我想重置标头,因为标头中还包括的“ organizationid”在Apollo Provider初始化期间本地存储中尚不可用。登录后,我只能通过查询从Apollo服务器获得“ organizationid”。
const {othertoken} = queryString.parse(window.location.search)
const header = {
Authorization: ' Bearer ' + localStorage.getItem('accounts:accessToken'),
othertoken,
organizationid: localStorage.getItem('organizationId')
}
const uri = CONFIG.graphQLServerTest
const uploadLink = createUploadLink({uri, headers: header})
const cache = new InMemoryCache()
const apolloClient = new ApolloClient({
cache,
resolvers: merge(dashboardStore.resolvers, collectionStore.resolvers, templateStore.resolvers, documentStore.resolvers) as any,
link: ApolloLink.from([errorLink, uploadLink])
})
cache.writeData({data: collectionStore.defaults})
cache.writeData({data: dashboardStore.defaults})
cache.writeData({data: documentStore.defaults})
cache.writeData({data: templateStore.defaults})
const accountsGraphQL = new GraphQLClient({graphQLClient: apolloClient})
const accountsClient = new AccountsClient({}, accountsGraphQL)
const accountsPassword = new AccountsClientPassword(accountsClient)
export {accountsClient, accountsGraphQL, accountsPassword, apolloClient}
在将所有查询保存到本地存储之后,我需要在所有查询中发送给服务器的每个查询中都包含“ organizationid”。为此,在将“ organizationid”保存到本地存储之后,我需要重新初始化标头。有没有办法做到这一点?
我想避免在所有查询中都包含“ organizationid”作为查询参数。
答案 0 :(得分:0)
我能够使用通过setContext创建的authLink来解决此问题:
Span