3

I'm in the process of duplicating AWS services with LocalStack.

I've got my Lambda and API Gateway running in LocalStack in much the same way as they would in AWS. I've tested using a Rest API tool to verify. However, when I run the same query within a browser using an Apollo Client, the LocalStack version no longer works.

I've tried 3 different browsers for a given GraphQL and only Firefox shows any data when I inspect via the Network tab. Apollo always errors with a JSON decode error at position 1. The request always returns a 200 status code.

Our client setup is:

import { ApolloClient, InMemoryCache, createHttpLink } from '@apollo/client';
import 'cross-fetch/polyfill';

const httpLink = createHttpLink({
  uri: 'http://example.com/graphql',
  credentials: 'include', // Allow other domains too
  fetchOptions: {
    mode:  'no-cors',
  },
});

export const client = new ApolloClient({
  link: httpLink,
  credentials: 'include',
  cache: new InMemoryCache(),
});

Is it possible to have some CORS issue within Apollo? I'm pretty sure I have the headers set correctly in the gateway, access-control-allow-origin: http://localhost:3000, Content-Type: application/json, and so on.

Has anyone seen anything like these kinds of errors?

Update

I've found that switching from no-cors to cors allows it to work using a known good URL.
It seems to that:

  • when cors is set, there is a preflight check. This OPTIONS request provides a number of response headers of which the Access-Control-Allow-Origin must be set to the requesting domain.
  • when no-cors is set there is no preflight check and for whatever reason, the response is then rejected, even though that too contains the correct Access-Control-Allow-Origin. I tested using an URL that worked with the cors option.

In Summary

  1. At this point in time, LocalStack is unable to provide the correct OPTIONS response, see https://github.com/localstack/localstack/issues/4204 and that prevents me from using cors.
  2. Using no-cors fails, even when I use a known good URL.

I've abandoned LocalStack and switched to using API Gateway from AWS directly.

I hope this helps.

Dale O'Brien
  • 4,108
  • 2
  • 15
  • 10

0 Answers0