我正在尝试使基本的 Rails、Graphql、Apollo-Client 设置正常工作,但在 rails 端出现 422 错误“无效的身份验证令牌”时遇到问题。
我对阿波罗的使用看起来不对吗?
这是一个带有 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));
导轨日志:
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)