gpt4 book ai didi

ruby-on-rails - Rails、Graphql、Apollo 客户端的验证 token 无效

转载 作者:行者123 更新时间:2023-12-04 17:07:25 26 4
gpt4 key购买 nike

我正在尝试使基本的 Rails、Graphql、Apollo-Client 设置正常工作,但在 Rails 端遇到 422 错误“无效身份验证 token ”的问题。

我对阿波罗的使用看起来不对吗?

它是一个带有 graphql gem 和 apollo 客户端的 Rails 5 应用程序。

const csrfToken = document.getElementsByName('csrf-token')[0].content
const client = new ApolloClient({
networkInterface: createNetworkInterface({
uri: '/graphql',
credentials: 'same-origin',
headers: {
'X-CSRF-Token': csrfToken
}
}),
});
client.query({
query: gql`
query Camillo2 {
user {
id
email
created_at
}
}
`,
})
.then(data => console.log(data))
.catch(error => console.error(error));

rails 日志:
Started POST "/graphql" for ::1 at 2017-03-10 08:51:58 -0800
Processing by GraphqlController#create as */*
Parameters: {"query"=>"query Camillo2 {\n user {\n id\n email\n created_at\n __typename\n }\n}\n", "operationName"=>"Camillo2", "graphql"=>{"query"=>"query Camillo2 {\n user {\n id\n email\n created_at\n __typename\n }\n}\n", "operationName"=>"Camillo2"}}
Can't verify CSRF token authenticity.
Completed 422 Unprocessable Entity in 2ms (ActiveRecord: 0.0ms)

最佳答案

从 Apollo 2.0 开始,根据

https://github.com/apollographql/apollo-client/blob/master/Upgrade.md

就这样做

const csrfToken = document.querySelector('meta[name=csrf-token]').getAttribute('content');
const client = new ApolloClient({
link: new HttpLink({
credentials: 'same-origin',
headers: {
'X-CSRF-Token': csrfToken
}
}),
cache: new InMemoryCache()
});

关于ruby-on-rails - Rails、Graphql、Apollo 客户端的验证 token 无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42723989/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com