我有一个Apollo客户端,我第一次尝试连接到graphql服务器。我似乎无法发送任何标题......
我实例化apollo客户端的组件看起来像这样......
const cache = new InMemoryCache();
const stateLink = withClientState({ resolvers, cache, apolloState });
const httpLink = new HttpLink({
uri: urls.graphqlServer + urls.graphqlEndpoint,
options: {
credentials: 'include',
headers: {
authorization: 'bearer token',
'X-CSRF-Token': 'sakldjsalkj',
'Custom-Header': 'my header'
}
}
});
const client = new ApolloClient({
cache,
link: ApolloLink.from([stateLink, httpLink]),
connectToDevTools: true
});
window.__APOLLO_CLIENT__ = client;
return (
<ApolloProvider client={client}>
<Root />
</ApolloProvider>
);
然后标题不包含我设置的任何内容......
:authority:localhost:8080
:method:OPTIONS
:path:/graphql
:scheme:https
accept:*/*
accept-encoding:gzip, deflate, br
accept-language:en-US,en;q=0.9,ko;q=0.8,zh-CN;q=0.7,zh;q=0.6
access-control-request-headers:content-type
access-control-request-method:POST
origin:http://localhost:8000
referer:http://localhost:8000/app
user-agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_3)
AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.186 Safari/537.36
知道如何解决这个问题吗?
答案 0 :(得分:2)
您在错误的“选项”对象中添加了标题的选项。看看docu。
将apollo http链接更改为:
时,它应该有效
....
const httpLink = new HttpLink({
uri: urls.graphqlServer + urls.graphqlEndpoint,
credentials: 'include',
headers: {
authorization: 'bearer token',
'X-CSRF-Token': 'sakldjsalkj',
'Custom-Header': 'my header'
}
});
...