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

Delta Sync is failing silently #490

Closed IntelligentDevelopments closed 4 years ago

IntelligentDevelopments commented 4 years ago

Note: If your issue/feature-request/question is regarding the AWS AppSync service, please log it in the official AWS AppSync forum

I initially submitted my question on the appSync forum, but have received no response there.

Do you want to request a feature or report a bug? bug

What is the current behavior? when calling AWSAppSyncClient.sync including a base query and a subscription query:

If the current behavior is a bug, please provide the steps to reproduce and if possible a minimal demo of the problem. use amplify cli to create a new project with a graphql api using the todo schema use vue/cli to create a new vue app set up auth using cognito user pools create a new user in the app pool and use amplify-sign-in to sign in update main.ts to match the following:

import Vue from 'vue';
import App from './App.vue';
import router from './router';
import Amplify, * as AmplifyModules from 'aws-amplify'
import AWSAppSyncClient, { AWSAppSyncClientOptions, buildSync } from 'aws-appsync'
import VueApollo from 'vue-apollo'
import { AmplifyPlugin } from 'aws-amplify-vue'
import awsconfig from './aws-exports'
import { ApolloClientOptions } from 'apollo-client';
import { NormalizedCacheObject } from 'apollo-cache-inmemory';
import gql from 'graphql-tag';

Vue.config.productionTip = false;

Amplify.configure(awsconfig);

const config: AWSAppSyncClientOptions = {
  url: awsconfig.aws_appsync_graphqlEndpoint,
  region: awsconfig.aws_appsync_region,
  auth: {
    type: awsconfig.aws_appsync_authenticationType,
      jwtToken: async () => { 
        var result = (await AmplifyModules.Auth.currentSession()
        .then(data => data)
        .catch(err =>  err));

        console.log({ result });

        return result.getIdToken().getJwtToken();
      }
    }
};

const options: Partial<ApolloClientOptions<NormalizedCacheObject>> = {
  defaultOptions: {
    watchQuery: {
      fetchPolicy: 'cache-and-network',
    }
  }
};

const client = new AWSAppSyncClient(config, options);

client.hydrated().then(
  function() { 
    var baseQuery = gql`query Base {
      listTodos {
        items {
          id
          name
          description
        }
        nextToken
      }
    }`;

    var subscriptionQuery = gql`subscription Subscriptions {
      onCreateTodo {
        id
        name
        description
      }
    }`;

    var syncOptions = {
      baseQuery: { 
        query: baseQuery,
        variables: {},
        update: (cache:any, { data }:any) => {
          console.log({ query: "syncTasksBase", cache, data });
        }
      },
      subscriptionQuery: { 
        query: subscriptionQuery,
        variables: {},
        update: (cache:any, { data }:any) => {
          console.log({ query: "onCreateTaskWithVersion", cache, data });
        }
      }
    };

    var syncResult = client.sync(syncOptions);

    console.log({ syncOptions, syncResult });
  }
);

const appsyncProvider = new VueApollo({
  defaultClient: client
})

Vue.use(AmplifyPlugin, AmplifyModules);
Vue.use(VueApollo);

new Vue({
  router,
  render: (h) => h(App),
  apolloProvider: appsyncProvider,
}).$mount('#app');

What is the expected behavior? I expected delta sync to populate the offline cache with the results of the base query and to also listen for new todos, placing those in the offline cache. If i comment out subscriptionQuery in syncOptions, the offline cache does in fact get populated with the results from the base query. If there is something wrong with my subscriptionQuery object or with the options object as a whole, i would expect an error message.

Which versions and which environment (browser, react-native, nodejs) / OS are affected by this issue? Did this work in previous versions? I created the test app today, so i think i'm on all latest versions:

We're loving amplify and appSync so far, hoping we can get past this road block. Let me know if there is any additional info required.

undefobj commented 4 years ago

Currently there is no automation in the CLI which will setup the Pipeline resolvers which are needed for Delta Sync, so the Todo schema will not work. You'll need something like this: https://docs.aws.amazon.com/appsync/latest/devguide/tutorial-delta-sync.html#one-click-setup

However there are some upcoming enhancements to automate Delta Sync in Amplify. I'd suggest holding tight for a couple weeks to see if they meet your needs.

undefobj commented 4 years ago

@IntelligentDevelopments we have just released DataStore which includes Delta Sync and is a more streamlined setup, including AppSync integration: https://aws-amplify.github.io/docs/js/datastore

While you can still use AppSync as described above manually configuring Delta Sync queries and resolvers it is recommended to use DataStore going forward for offline use cases.

IntelligentDevelopments commented 4 years ago

@undefobj thanks for letting us know.

We've spent some time looking at DataStore and we've got some questions. I'll add those as new issues.