javascript - Apollo 客户端发送 OPTIONS 而不是 GET HTTP 方法

标签 javascript meteor graphql apollo-client

我无法理解 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/

相关文章:

javascript - 无法对 Gatsby 站点中 Ant Design Table 中的列进行排序

javascript - 每行带有按钮的 Html 表格

javascript - 如何返回特定索引处的字符?

javascript - 列出 DOM 对象上的变量和方法

meteor - 如何使用铁路由器或 meteor 本身提供文件?

graphql - 如何通过 graphql 使用 Passport-local

javascript - d3js - 如何将 x- 域值向右移动

meteor - 在 Meteor 1.3+ 中使用包含相同 npm 包的多个包

javascript - 嵌套数组的排序不会导致顺序发生变化

java - 如何从 Spring Boot 应用程序中的 graphQL 端点中的传入消息中读取请求 header