Skip to content

evictFromApiCache type definition seems inaccurate #404

@robokozo

Description

@robokozo

The type definition provided for the evictFromApiCache parameter of keyValuePair (keyValuePair: Record<string, string>) doesn't seem correct. It doesn't seem to match a scenario where it works but adhering to the schema does not work

//@aws-appsync/utils/lib/index.d.ts    
evictFromApiCache(typeName: string, fieldName: string, keyValuePair: Record<string, string>): void;

Example

try it yourself in the aws console, all the parts needed are below

Cache Configuration

{
  cachingConfig: {
    ttl: Duration.hours(1),
    cachingKeys: ["$context.arguments"]
  }
}

Cached Query

export function response(ctx) {
    const nextToken = ctx.arguments.nextToken;
    const timestamp = util.time.nowISO8601();
    
    if (nextToken == null) {
        return [
            { id: "1", title: "AllNull-A", content: `AN-AAA @ ${timestamp}` },
            { id: "2", title: "AllNull-B", content: `AN-BBB @ ${timestamp}` }
        ];
    }
    
    return [
        { id: "80", title: "NextToken-A", content: `NT-AAA @ ${timestamp}` },
        { id: "81", title: "NextToken-B", content: `NT-BBB @ ${timestamp}` }
    ];
}

Mutation that manually evicts the cache

export function response(ctx) {
    const argsString = JSON.stringify(ctx.arguments); //has to be a string due to signature
    
    extensions.evictFromApiCache("Query", "postList", {
        "$context.arguments": argsString
    });
    
    // Return debug info
    return {
        args: argsString,
        success: true
    };
}

Testing Queries and Mutations

# ===== QUERIES =====

query NextTokenOmitted {
  nextTokenOmitted: postList {
    id
    title
    content
  }
}

query NextTokenWithValue($nextTokenValue: String!) {
  nextTokenWithValue: postList(nextToken: $nextTokenValue) {
    id
    title
    content
  }
}

query NextTokenNull($nextTokenNull: String) {
  nextTokenNull: postList(nextToken: $nextTokenNull) {
    id
    title
    content
  }
}

query BothWithValue($nextTokenBothValue: String!, $limitBothValue: Int!) {
  bothWithValue: postList(nextToken: $nextTokenBothValue, limit: $limitBothValue) {
    id
    title
    content
  }
}

# ===== MUTATIONS =====

mutation EvictNextTokenOmitted {
  evictNextTokenOmitted: bustCache {
    args
    keys
    success
  }
}

mutation EvictNextTokenWithValue($evictNextTokenValue: String!) {
  evictNextTokenWithValue: bustCache(nextToken: $evictNextTokenValue) {
    args
    keys
    success
  }
}

mutation EvictNextTokenNull($evictNextTokenNull: String) { 
  evictNextTokenNull: bustCache(nextToken: $evictNextTokenNull) {
    args
    keys
    success
  }
}

mutation EvictBothWithValue($evictNextTokenBothValue: String!, $evictLimitBothValue: Int!) {
  evictBothWithValue: bustCache(nextToken: $evictNextTokenBothValue, limit: $evictLimitBothValue) {
    args
    keys
    success
  }
}

Variables used for testing

{
  "nextTokenValue": "abc123",
  "nextTokenNull": null,
  "nextTokenBothValue": "abc123",
  "limitBothValue": 10,
  "evictNextTokenValue": "abc123",
  "evictNextTokenNull": null,
  "evictNextTokenBothValue": "abc123",
  "evictLimitBothValue": 10
}

Results

When running each of the queries and corresponding mutations I get the appropriate response

{
  "extensions": {
    "apiCacheEntriesDeleted": 1
  },
  "data": { ... }
}

Except for evictBothWithValue

{
  "data": {
    "evictBothWithValue": {
      "args": "{\"nextToken\":\"abc123\",\"limit\":10}",
      "success": true
    }
  }
}

However, if I violate the TS type and just use ctx.arguments it works correctly for all cases

    extensions.evictFromApiCache("Query", "postList", {
        "$context.arguments": ctx.arguments
    });
{
  "extensions": {
    "apiCacheEntriesDeleted": 1
  },
  "data": {
    "evictBothWithValue": {
      "args": "{\"nextToken\":\"abc123\",\"limit\":10}",
      "success": true
    }
  }
}

Maybe this is a bug in the ts type or its an evictFromApiCache bug.

Note

If evicting with an empty object both forms work as expected

    extensions.evictFromApiCache("Query", "postList", {
        "$context.arguments": {}
    });
    // 
    extensions.evictFromApiCache("Query", "postList", {
        "$context.arguments": JSON.stringify({ }) //"{ }"
    });

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions