AWS AppSync initial batch put does not update cache

573 Views Asked by At

I am using AWS AppSync together with an angular 7 application and initialize the client like this:

let instance: AWSAppSyncClient<NormalizedCacheObject> = new AWSAppSyncClient({
  url: environment.graphqlEndpoint,
  region: environment.region,
  auth: {
       type: AUTH_TYPE.AMAZON_COGNITO_USER_POOLS,
       jwtToken: async() => { //return token }
    },
    cacheOptions: {
        dataIdFromObject: (obj : any) => `${obj.__typename}:${obj.key}`
    }
});

which works fine when AppSync conducts a query and dynamodb has already the requested values in it:

{
  "data": {
    "getDefaultSettings": [
      {
        "key": "setting_a",
        "value": "true"
      },
      {
        "key": "setting_b",
        "value": "false"
      }
    ]
  }
}

This query result is cached and when i conduct a mutation the response of the mutation:

{
  "data": {
    "addDefaultSettings": [
      {
        "key": "setting_a",
        "value": "false"
      },
      {
        "key": "setting_b",
        "value": "false"
      }
    ]
  }
}

causes the cache to update via the key comparison which is also working correctly.

However, when there are no values in the dynamodb yet, the fetch returns a result like:

{
  "data": {
    "getDefaultSettings": [
      null,
      null
    ]
  }
}

So now this query is cached and when conducting a mutation with the result

{
  "data": {
    "addDefaultSettings": [
      {
        "key": "setting_a",
        "value": "false"
      },
      {
        "key": "setting_b",
        "value": "false"
      }
    ]
  }
}

it can't update the cache. So the next few times a conduct the query it is taken from the cache and I still get the null result, but I need to immediately update the cache after receiving the mutation result for the initial put mutation.

As soon as the next query fetches the new data from the server all further mutations are updating the cache immediately because now there are entries with the same key in the cache.

So my problem only occurs on the initial put and I don't know what to change.

  • Maybe update the resolver mapping from
$util.toJson($ctx.result.data.$tableName)

to revealing the queried key + the null result value?

  • Clearing the cache after the initial put?

I would definitely like to use the network-and-cache option as it is working great except for the initial put.

I would appreciate if someone could help me out with this one.

1

There are 1 best solutions below

0
On

You didn't post the code for your mutation or the resolver, so I'm just guessing here that the problem is in DynamoDB read consistency.

  1. Try adding "consistentRead" : true to your mutation response template in AppSync.

  2. Build an optimistic response inside the response template like like this

    ## Raise a GraphQL field error in case of a datasource invocation error
    #if($ctx.error)
        $util.error($ctx.error.message, $ctx.error.type)
    #end

    ## Pass back an optimistic result from DynamoDB. **
    #set ($items = {[{"key":"setting_a", "value": $ctx.args.setting_a}, {...}]})
    $util.toJson($items)