Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apollo Client sending OPTIONS instead of GET HTTP method

I'm having trouble understanding Apollo Client library as it does not work as intended. Instead of sending the GET HTTP method, it sends the OPTIONS HTTP method even though I've put to use GET only when retrieving data from GraphQL server.

const client = new ApolloClient({
    link: ApolloLink.from([
        new MeteorAccountsLink(),
        new HttpLink({
            uri: 'https://selo-comments.herokuapp.com/graphql',
            useGETForQueries: true
        })
    ]),
    cache: new InMemoryCache()
});

Console log from the browser: OPTIONS 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 (Method Not Allowed)

Which obviously means that the HTTP method is incorrect even if it has the query parameter in the url. If you query that url using Postman or simply navigating to the url using browser's address bar, you will get GraphQL data. I have to use https://cors-anywhere.herokuapp.com/ in order to execute the query successfully.

What am I doing wrong?

like image 668
Vladimir Jovanović Avatar asked Oct 26 '18 16:10

Vladimir Jovanović


People also ask

Does Apollo Client use Fetch?

A fetch policy defines how Apollo Client uses the cache for a particular query. The default policy is cache-first , which means Apollo Client checks the cache to see if the result is present before making a network request. If the result is present, no network request occurs.

Does GraphQL use GET method?

Making a GraphQL requests using the GET methodYou can also make GraphQL requests using the GET HTTP method.

How are HTTP requests sent ApolloClient authenticated?

Luckily, Apollo provides a nice way for authenticating all requests by using the concept of middleware, implemented as an Apollo Link. import { setContext } from '@apollo/client/link/context'; This middleware will be invoked every time ApolloClient sends a request to the server.

Is GraphQL POST or get?

GraphQL requests can be sent via HTTP POST or HTTP GET requests. GET requests must be sent in the following format. The query, variables, and operation are sent as URL-encoded query parameters in the URL. In either request method (POST or GET), only query is required.


1 Answers

The options request is probably a preflight request for 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.

You probably need to configure your server to allow cross origin calls.

Maybe you can find some inspiration here to get u started. Allow CORS REST request to a Express/Node.js application on Heroku

like image 51
JOSEFtw Avatar answered Sep 28 '22 14:09

JOSEFtw