aws / aws/aws-appsync-community

evictFromApiCache type definition seems inaccurate

Open
#404 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
HTML
Stars
507
Forks
37
PR merge metrics
No merged PRs in 30d

Description

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

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

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

### Cache Configuration
```typescript
{
cachingConfig: {
ttl: Duration.hours(1),
cachingKeys: ["$context.arguments"]
}
}
```

### Cached Query
```typescript
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
```typescript
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
```graphql
# ===== 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
```json
{
"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
```json
{
"extensions": {
"apiCacheEntriesDeleted": 1
},
"data": { ... }
}
```

Except for `evictBothWithValue`
```json
{
"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
```typescript
extensions.evictFromApiCache("Query", "postList", {
"$context.arguments": ctx.arguments
});
```
```json
{
"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
```typescript
extensions.evictFromApiCache("Query", "postList", {
"$context.arguments": {}
});
//
extensions.evictFromApiCache("Query", "postList", {
"$context.arguments": JSON.stringify({ }) //"{ }"
});
```

Contributor guide

Open the contributing guide

Research direction

Start with the evictFromApiCache declaration in //@aws-appsync/utils/lib/index.d.ts and reproduce the serialized and object-valued $context.arguments calls from the AWS console example. Compare the type definition with the observed cache-eviction behavior; done means the parameter type and runtime behavior are consistent.

Written by the indexing model from the issue text.

Assessment

Tech stack
aws, typescript
Domain
api
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.