Search code examples
reactjsamazon-web-servicesapollo-clientaws-appsync

Cannot connect apollo client to aws appsync


I have a web app using aws appsync as backend and react + apollo client (v3) as front end. But when I try connecting apollo client to appsync, I get an error message from the library:

./node_modules/aws-appsync-react/lib/offline-helpers.js

Module not found: Can't resolve 'react-apollo' in '/Users/mypath/web/node_modules/aws-appsync-react/lib'

Here's the config for the client:

import AWSAppSyncClient from "aws-appsync";
import AppSyncConfig from "./aws-exports";

export const apolloClient = new AWSAppSyncClient({
  url: AppSyncConfig.aws_appsync_graphqlEndpoint,
  region: AppSyncConfig.aws_appsync_region,
  auth: {
    type: AppSyncConfig.aws_appsync_authenticationType,
    apiKey: AppSyncConfig.aws_appsync_apiKey,
  },
});

And the in my App.ts:

import { ApolloProvider } from "@apollo/client";
import { Rehydrated } from "aws-appsync-react";
import { apolloClient } from "./apollo";

...
<ApolloProvider client={apolloClient}>
  <Rehydrated>
      <MyApp />
  </Rehydrated>
</ApolloProvider>

Looks like a compatibility issue?

I'm using "@apollo/client": "^3.1.3", "aws-appsync": "^4.0.0","aws-appsync-react": "^4.0.0",.


Solution

  • It is a compatibility issue. Current version of aws-appsync doesn't support apollo-client v3, see this thread for progress: https://github.com/awslabs/aws-mobile-appsync-sdk-js/issues/448

    Best workaround is this: Proper way to setup AWSAppSyncClient, Apollo & React

    Note the workaround does use two deprecated libraries but can be slightly improved as:

    import { ApolloClient, ApolloLink, InMemoryCache } from "@apollo/client";
    import { createAuthLink } from "aws-appsync-auth-link";
    import { createHttpLink } from "apollo-link-http";
    import AppSyncConfig from "./aws-exports";
    
    const url = AppSyncConfig.aws_appsync_graphqlEndpoint;
    const region = AppSyncConfig.aws_project_region;
    const auth = {
      type: AppSyncConfig.aws_appsync_authenticationType,
      apiKey: AppSyncConfig.aws_appsync_apiKey,
    };
    const link = ApolloLink.from([
      // @ts-ignore
      createAuthLink({ url, region, auth }),
      // @ts-ignore
      createHttpLink({ uri: url }),
    ]);
    const client = new ApolloClient({
      link,
      cache: new InMemoryCache(),
    });
    
    export default client;