Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

apollo-client does not work with CORS

Tags:

I am writing a graphql server component on AWS Lambda (NOT using graphql-server). On the client side I'm using apollo-client. On the response of the lambda function I'm setting

const response = {     statusCode: 200,     headers: {         "Access-Control-Allow-Origin": "*" // Required for CORS support to work     },     body: JSON.stringify({         result: 'mock data',         input: event,     }), }; callback(null, response); 

On the client side using ApolloClient I get the following error

Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8080' is therefore not allowed access.

However when I execute the same request using something like axios then it works fine. Furthermore when I just execute the request over something like postman I see the "Access-Control-Allow-Origin" setting enabled on the response. Is this a known issue with apollo-client and how do I fix this?

like image 700
tmp dev Avatar asked Feb 16 '18 00:02

tmp dev


People also ask

How do I enable CORS Apollo Client?

You can enable credentials with CORS by setting the Access-Control-Allow-Credentials HTTP header to true . You must specify an origin to enable credentialed requests. If your server sends the * wildcard value for the Access-Control-Allow-Origin HTTP header, your browser will refuse to send credentials.

Can I use Apollo Client with REST API?

You can use apollo-link-rest to call REST API inside your GraphQL queries.

Does Apollo Client need redux?

Apollo is suitable for managing remote data, and 20% of it was managed in a separate Redux Store, hence there is a need to integrate GraphQL and Redux. Apollo GraphQL no longer requires Redux. Apollo Client automatically catches the data and normalizes new data in query responses and after mutation.

Does Apollo Client use Websockets?

Setting up the transport. Because subscriptions usually maintain a persistent connection, they shouldn't use the default HTTP transport that Apollo Client uses for queries and mutations. Instead, Apollo Client subscriptions most commonly communicate over WebSocket, via the graphql-ws library.


2 Answers

To workaround the CORS issue with Apollo you have to pass the no-cors option to the underlying fetch.

import ApolloClient from "apollo-boost";  const client = new ApolloClient({   uri: "your client uri goes here",   fetchOptions: {     mode: 'no-cors',   }, }); 

This is not a specific Apollo problem, rather a configuration that is meant to be tackled on the fetch side, see this for more information: https://developers.google.com/web/ilt/pwa/working-with-the-fetch-api#cross-origin_requests

I wonder why it does works with Axios for you, I'd expect you to have the no-cors mode set somewhere.

like image 108
ivanalejandro0 Avatar answered Oct 21 '22 17:10

ivanalejandro0


I'd assume you're using the AWS API Gateway.

One question for you is: have you enabled CORS for your gateway? See how

I believe that should solve your issues, if you're also sending cookies, you can also set the "Access-Control-Allow-Credentials" : true header as well. `

like image 22
oreoluwa Avatar answered Oct 21 '22 17:10

oreoluwa