Cache isn't evicted if $extensions.evictFromApiCache is invoked in nested mutation

0

Hi, title says it all, recently we've made few changes to the mutations and how they are queried, from now on all mutations can nested inside one "main" mutation which made cache eviction impossible for them.

Mutations that aren't invoked by nesting are evicting the cache just fine, but the nested ones does not, despite having the same eviction configuration and working on the same keys, the single difference being the nesting of the mutations.

Since eviction is tied to root mutation response mapping there seems to be no way of working around it. Is it expected behavior? if so, is it described somewhere?

Any support or ideas will be greatly appreciated, thank you :)

There are some details:

Schema:

type Item @aws_iam @aws_api_key @aws_cognito_user_pools {
    id: ID!
    name: String!
}

type Query @aws_iam @aws_api_key @aws_cognito_user_pools {
    fetchItem(id: ID!): Item!
    nested: QueryNested!
}

type QueryNested @aws_iam @aws_api_key @aws_cognito_user_pools {
    fetchItem(id: ID!): Item!
}

type Mutation @aws_iam @aws_api_key @aws_cognito_user_pools {
   changeItemName(id: ID!, name: String): Item!
    nested: MutationNested!
}

type MutationNested @aws_iam @aws_api_key @aws_cognito_user_pools {
    changeItemName(id: ID!, name: String!): Item!
}

Caching keys: $context.arguments.id on Query.fetchItem and QueryNested.fetchItem

Evictions in response mappings (VTL):

Mutation.changeItemName:

$extensions.evictFromApiCache("Query", "fetchItem", {
      "context.arguments.id": $context.arguments.id
    })

MutationNested.changeItemName:

$extensions.evictFromApiCache("QueryNested", "fetchItem", {
      "context.arguments.id": $context.arguments.id
    })

Given Item:

{id: 1, name: "test}

We invoke not nested GQL with below requests and responses, all of them are fine:

  1. Not cached item: Req:
mutation Mutation {
         changeItemName(id:1, name: "not-nested-1") {
             id
             name
             }
}

Res:

{
    "data": {
        "changeItemName": {
            "id": 1,
            "name": "not-nested-1"
        }
    }
}

Req:

mutation Query {
         fetchItem(id:1) {
             id
             name
             }
}

Res:

{
    "data": {
        "fetchItem": {
            "id": 1,
            "name": "not-nested-1"
        }
    }
}

  1. Cached item: Req:
mutation Mutation {
         changeItemName(id:1, name: "not-nested-2") {
             id
             name
             }
}

Res:

{
    "extensions": {
      "apiCacheEntriesDeleted": 1
    },
    "data": {
        "changeItemName": {
            "id": 1,
            "name": "not-nested-2"
        }
    }
}

Req:

mutation Query {
         fetchItem(id:1) {
             id
             name
             }
}

Res:

{
    "data": {
        "fetchItem": {
            "id": 1,
            "name": "not-nested-2"
        }
    }
}

Then we invoke nested GQL with below requests and responses, second mutation does not evict the cache:

  1. Not cached item:

Req:

mutation MutationNested {
         changeItemName(id:1, name: "nested-1") {
             id
             name
             }
}

Res:

{
    "data": {
        "nested":{
            "changeItemName": {
                "id": 1,
                "name": "nested-1"
            }
        }
    }
}

Req:

mutation QueryNested {
         fetchItem(id:1) {
             id
             name
             }
}

Res:

{
    "data": {
        "nested":{
            "fetchItem": {
                "id": 1,
                "name": "nested-1" // OK: Didn't cached "not-nested-2" since its different typeName Query <> QueryNested, caching this response
            }
        }
    }
}
  1. Cached item: Req:
mutation MutationNested {
         changeItemName(id:1, name: "nested-2") {
             id
             name
             }
}

Res:

{
    // Notice lack of the "extension" key
    "data": {
        "nested":{
            "changeItemName": {
                "id": 1,
                "name": "nested-2"
            }
        }
    }
}

Req:

mutation QueryNested {
         fetchItem(id:1) {
             id
             name
             }
}

Res:

{
    "data": {
        "nested":{
            "fetchItem": {
                "id": 1,
                "name": "nested-1"  //NOK should return "nested-2", but instead hit the cache that should be evicted by previous MutationNested.changeItemName invocation.
            }
        }
    }
}

Resources:

  1. extensions doc
  2. article
질문됨 8달 전265회 조회
1개 답변
0

For future explorers: It's still unknown to me why such evict doesn't work in nested mutations but I've managed to find a workaround by changing nested mutations from unit to pipeline type and moving data source along with mappings to separate function and then calling just that one function from the resolver. It's basically wrapping a function with pass-trough resolver.

In that way when evict is invoked in the context of a function (instead of the resolver) even when called from nested resolver then it just works ;)

답변함 8달 전

로그인하지 않았습니다. 로그인해야 답변을 게시할 수 있습니다.

좋은 답변은 질문에 명확하게 답하고 건설적인 피드백을 제공하며 질문자의 전문적인 성장을 장려합니다.

질문 답변하기에 대한 가이드라인

관련 콘텐츠