CleverlyDone
CleverlyDone

Reputation: 69

AppSync util method converts filter to non valid condition object

I want to grab the results using my sort key (resource_id) and a property defining the user's workspace id. But I run into the following error message every time: "code.js:15:12: Property 'filter' is expected to be a valid condition object"

Without the filter the scan resolves and returns results. So I confirmed something is wrong with my filter object. I checked and it's like how the docs implemented it

I have the following query:

query MyQuery {
  listAuditLog(limit: 2, filter: {resource_id: {eq: "AUDITLOG"}, workspace: {eq: "WORKSPACE-123"}}) {
    nextToken
    items {
      workspace
      users
      created
    }
  }
}

My code for my resolver

import * as ddb from '@aws-appsync/utils/dynamodb'
import { util } from '@aws-appsync/utils'

/**
 * Queries a DynamoDB table, limits the number of returned items, and paginates with the provided `nextToken`
 * @param {import('@aws-appsync/utils').Context<{filter?: any; limit?: number; nextToken?: string}>} ctx the context
 * @returns {import('@aws-appsync/utils').DynamoDBScanRequest} the request
 */
export function request(ctx) {
    const { limit, nextToken } = ctx.args;
    const filter = util.transform.toDynamoDBFilterExpression(ctx.args.filter);
    
    return ddb.scan({
        filter: JSON.parse(filter),
        limit: limit || 20,
        nextToken,
    });
}

/**
 * Returns the query items
 * @param {import('@aws-appsync/utils').Context} ctx the context
 * @returns {{items: any[]; nextToken?: string}} the result
 */
export function response(ctx) {
    const { error, result } = ctx;
    if (error) {
        util.error(ctx.error.message, ctx.error.type);
    }
    const { items = [], nextToken } = result;
    return { items, nextToken };
}

Right before the ddb.scan method the filter object looks like this:

{
    "expression": "(#resource_id = :resource_id_eq) AND (#workspace = :workspace_eq)",
    "expressionNames": {
        "#workspace": "workspace",
        "#resource_id": "resource_id"
    },
    "expressionValues": {
        ":resource_id_eq": {
            "S": "AUDITLOG"
        },
        ":workspace_eq": {
            "S": "WORKSPACE-123"
        }
    }
}

Newer to AWS AppSync so I'm probably overlooking something but I read the docs over and over and idk what's going on. Another set of eyes may help though.

Upvotes: 0

Views: 85

Answers (1)

CleverlyDone
CleverlyDone

Reputation: 69

However, I didn't figure out the underlying issue. After searching around. I really shouldn't be using dynamo db scans for my use case.

I should be using a query operation. I also decided to leverage a global secondary index to get a specific set of items for my table that can be used for almost all entities.

Happy with the result - Thanks for reading

Upvotes: 0

Related Questions