我无法理解 Apollo Client 库,因为它没有按预期工作。它不是发送 GET
HTTP 方法,而是发送 OPTIONS
HTTP 方法,即使我已经使用了GET 仅当从 GraphQL 服务器检索数据时。
const client = new ApolloClient({
link: ApolloLink.from([
new MeteorAccountsLink(),
new HttpLink({
uri: 'https://selo-comments.herokuapp.com/graphql',
useGETForQueries: true
})
]),
cache: new InMemoryCache()
});
来自浏览器的控制台日志:
选项 https://selo-comments.herokuapp.com/graphql?query=%7B%0A%20%20comments(id%3A%20%22TFpQmhrDxQqHk2ryy%22)%20%7B%0A%20%20%20%20articleID %0A%20%20%20%20content%0A%20%20%20%20userId%0A%20%20%20%20createdAt%0A%20%20%20%20commentID%0A%20%20%20%20votes %0A%20%20%20%20blockedUsers%0A%20%20%20%20__typename%0A%20%20%7D%0A%7D%0A&variables=%7B%7D 405(方法不允许)
这显然意味着HTTP方法不正确,即使它在url中有查询参数。如果您使用 Postman 查询该 url 或使用浏览器的地址栏简单地导航到该 url,您将获得 GraphQL 数据。我必须使用 https://cors-anywhere.herokuapp.com/
才能成功执行查询。
我做错了什么?
最佳答案
选项请求可能是一个 preflight request对于 CORS .
A CORS preflight request is a CORS request that checks to see if the CORS protocol is understood. It is an OPTIONS request, using three HTTP request headers: Access-Control-Request-Method, Access-Control-Request-Headers, and the Origin header.
您可能需要配置您的服务器以允许跨源调用。
也许您可以在这里找到一些灵感来帮助您入门。 Allow CORS REST request to a Express/Node.js application on Heroku
关于javascript - Apollo 客户端发送 OPTIONS 而不是 GET HTTP 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53012629/