Aws-mobile-appsync-sdk-js: Need a query to initialize cache when using apollo-state-link

Created on 2 Jan 2019  路  4Comments  路  Source: awslabs/aws-mobile-appsync-sdk-js

Follow instruction of this post
https://github.com/awslabs/aws-mobile-appsync-sdk-js/issues/24#issuecomment-382066010.

If we delete the client.mutate(...) part at the bottom, but do a console.log(client.cache.data.data), which supposed to hold the default value set in withClientState, it will be an empty object. But if we add back the client.mutate(...), the default value set will appear in the `console.log(client.cache.data.data)`` call.

Also, changing client.mutate(...) to
client.query(query: gql` query networkStatus @client{ networkStatus { isConnected } } `, })
to query the networkStatus field will result in error Network error: Cannot read property 'networkStatus' of null.

good first issue

All 4 comments

@sfegsetsfe how did you configured the client (link)?

I used the exact config from @manueliglesias (except the client.mutation testing part ). Which was:

import { ApolloLink } from 'apollo-link';
import { withClientState } from 'apollo-link-state';
import AWSAppSyncClient, { createAppSyncLink, createLinkWithCache } from "aws-appsync";

const stateLink = createLinkWithCache(cache => withClientState({
  cache,
  resolvers: {
    Mutation: {
      updateNetworkStatus: (_, { isConnected }, { cache }) => {
        const data = {
          networkStatus: {
            __typename: 'NetworkStatus',
            isConnected
          },
        };
        cache.writeData({ data });
        return null
      },
    },
  },
  defaults: {
    networkStatus: {
      __typename: 'NetworkStatus',
      isConnected: false
    }
  }
}));

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

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

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

const result = await client.mutate({
  mutation: gql`mutation updateNetworkStatus($isConnected: Boolean) {
    updateNetworkStatus(isConnected: $isConnected) @client {
      isConnected
    }
  }`,
  variables: { isConnected: true }
});

console.log(result);

Is this ever going to be fixed?

@usmansbk we are still researching Apollo Link State, which at this time isn't fully supported in the SDK due to how cache can be manipulated out of the standard process. We have some ideas here and some prototype code but it will still take some time to fully support. In the meantime we recommend using the SDK without Apollo Link State.

Was this page helpful?
0 / 5 - 0 ratings