awslabs / aws-mobile-appsync-sdk-js

JavaScript library files for Offline, Sync, Sigv4. includes support for React Native
Apache License 2.0
921 stars 266 forks source link

Need a query to initialize cache when using apollo-state-link #330

Open sfegsetsfe opened 5 years ago

sfegsetsfe commented 5 years ago

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.

elorzafe commented 5 years ago

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

sfegsetsfe commented 5 years ago

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);
usmansbk commented 5 years ago

Is this ever going to be fixed?

undefobj commented 5 years ago

@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.