Aws-mobile-appsync-sdk-js: How can I add a custom link to the link chain?

Created on 30 Dec 2017  路  4Comments  路  Source: awslabs/aws-mobile-appsync-sdk-js

How would I extend the link chain with a link such as apollo-link-error?
https://github.com/apollographql/apollo-link/tree/master/packages/apollo-link-error

enhancement

Most helpful comment

Once PR #96 gets merged, it should be possible to use custom links with the AWS AppSync client like this: (e.g. apollo-link-error)

import { ApolloLink } from 'apollo-link';
import { onError } from 'apollo-link-error';
import AWSAppSyncClient, { createAppSyncLink } from "aws-appsync";

const onErrorLink = onError(({ graphQLErrors, networkError }) => {
  if (graphQLErrors)
    graphQLErrors.map(({ message, locations, path }) =>
      console.log(
        `[GraphQL error]: Message: ${message}, Location: ${locations}, Path: ${path}`
      )
    );
  if (networkError) console.log(`[Network error]: ${networkError}`);
});

const appSyncLink = createAppSyncLink({
  url: appSyncConfig.graphqlEndpoint,
  region: appSyncConfig.region,
  auth: {
    type: appSyncConfig.authenticationType,
    apiKey: appSyncConfig.apiKey
  }
});

const link = ApolloLink.from([
  onErrorLink,
  appSyncLink
]);

const client = new AWSAppSyncClient({}, { link });

All 4 comments

Hi @kabriel

Thanks for trying the service and the sdk!

As of now, it is not possible to customize the link chain. We're looking into supporting it, stay tuned.

For now, you can use a vanilla apollo-client instance with this package's links https://github.com/awslabs/aws-mobile-appsync-sdk-js/blob/87c52f48af957f21d335d1e53111d68479b41e38/packages/aws-appsync/src/client.js#L102-L120

Hey there, just want to chime in to mention that we'd like to use apollo-link-state for local state, and potentially try a different cache implementation like apollo-hermes-cache.

We're currently doing this now via the PR #62, usage (i.e. using the fork via npm/yarn link) is rather difficult mind you if your bundler isn't fond of symlinks (metro seems to choke on them)

Once PR #96 gets merged, it should be possible to use custom links with the AWS AppSync client like this: (e.g. apollo-link-error)

import { ApolloLink } from 'apollo-link';
import { onError } from 'apollo-link-error';
import AWSAppSyncClient, { createAppSyncLink } from "aws-appsync";

const onErrorLink = onError(({ graphQLErrors, networkError }) => {
  if (graphQLErrors)
    graphQLErrors.map(({ message, locations, path }) =>
      console.log(
        `[GraphQL error]: Message: ${message}, Location: ${locations}, Path: ${path}`
      )
    );
  if (networkError) console.log(`[Network error]: ${networkError}`);
});

const appSyncLink = createAppSyncLink({
  url: appSyncConfig.graphqlEndpoint,
  region: appSyncConfig.region,
  auth: {
    type: appSyncConfig.authenticationType,
    apiKey: appSyncConfig.apiKey
  }
});

const link = ApolloLink.from([
  onErrorLink,
  appSyncLink
]);

const client = new AWSAppSyncClient({}, { link });
Was this page helpful?
0 / 5 - 0 ratings

Related issues

yarax picture yarax  路  3Comments

ciocan picture ciocan  路  4Comments

wzup picture wzup  路  3Comments

jd-carroll picture jd-carroll  路  3Comments

wzup picture wzup  路  3Comments