amplify-category-api icon indicating copy to clipboard operation
amplify-category-api copied to clipboard

Expected JSON object for '$[filter]' but got a 'STRING' instead." when querying a custom index

Open knuspertante opened this issue 2 years ago • 6 comments

Before opening, please confirm:

  • [X] I have installed the latest version of the Amplify CLI (see above), and confirmed that the issue still persists.
  • [X] I have searched for duplicate or closed issues.
  • [X] I have read the guide for submitting bug reports.
  • [X] I have done my best to include a minimal, self-contained set of instructions for consistently reproducing the issue.
  • [X] I have removed any sensitive information from my code snippets and submission.

How did you install the Amplify CLI?

npm

If applicable, what version of Node.js are you using?

v16.16.0

Amplify CLI Version

10.5.1

What operating system are you using?

Mac

Did you make any manual changes to the cloud resources managed by Amplify? Please describe the changes made.

No manual changes made

Amplify Categories

api

Amplify Commands

Not applicable

Describe the bug

Use Filter in secondary index

const listPatternByOutCompany = async (
  outCompanyID: string,
  status?: Status,
  type?: PatternType,
  searchString?: string,
  limit?: number,
  nextTokenOld?: string | undefined | null
) => {
  const filter: ModelPatternFilterInput | undefined =
    status === undefined && type === undefined && searchString === undefined
      ? undefined
      : {
          and: [
            type === undefined
              ? null
              : {
                  type: {
                    eq: type,
                  },
                },
            status === undefined
              ? null
              : {
                  status: {
                    eq: status,
                  },
                },
            searchString === undefined
              ? null
              : {
                  or: [
                    {comment: {contains: searchString}},
                    {name: {contains: searchString}},
                    {id: {contains: searchString}},
                    {tags: {contains: searchString}},
                    {tags: {contains: searchString}},
                  ],
                },
          ],
        };
  const response = API.graphql(
    graphqlOperation(patternByOutCompany, {
      outCompanyID,
      filter,
      limit,
      nextToken: nextTokenOld,
    })
  ) as Promise<GraphQLResult<PatternByOutCompanyQuery>>;
@key(
    name: "patternByOutCompany"
    fields: ["outCompanyID"]
    queryField: "patternByOutCompany"
  )

When this Query / Statement is used and status, type or searchString are defined and not null, then the following Error is thrown: Expected JSON object for '$[filter]' but got a 'STRING' instead.

This Issue should already be fixed but I think it is not. See: https://github.com/aws-amplify/amplify-cli/issues/1554

Expected behavior

Filter also works on secondary indexes

Reproduction steps

  1. put in a @key directive in your model
  2. query using the custom index
  3. add a filter
  4. See error

GraphQL schema(s)

@key(
    name: "patternByOutCompany"
    fields: ["outCompanyID"]
    queryField: "patternByOutCompany"
  ) {
  id: ID!
  inCompanyID: ID!
  inCompany: Company! @connection(fields: ["inCompanyID"])
  outCompanyID: ID!
  outCompany: Company! @connection(fields: ["outCompanyID"])


Project Identifier

No response

Log output

# Put your logs below this line


Additional information

No response

knuspertante avatar Nov 30 '22 09:11 knuspertante

Hey @knuspertante :wave: thanks for raising this! It appears you are using version 1 of the Amplify GraphQL Transformer, which is supported today from a security standpoint. In a separate environment, are you able to migrate the schema to see whether you still experience this on v2? https://docs.amplify.aws/cli/migration/transformer-migration/

josefaidt avatar Dec 01 '22 19:12 josefaidt

Hey @josefaidt , thanks for coming up here!

Unfortunately currently I'm not able to migrate to v2, cause we are using the same dynamodb-tables with java and there we use optimistic locking with attribute 'version' ( https://sdk.amazonaws.com/java/api/latest/software/amazon/awssdk/enhanced/dynamodb/extensions/annotations/DynamoDbVersionAttribute.html)

When we migrate to v2 we need to rebuild the old way, as used in transformer v1. Or ist there another way to accomplish this?

Thanks!

knuspertante avatar Dec 02 '22 17:12 knuspertante

Hey @josefaidt,

sorry that I have to address you again. But is there a good way to combine both, upgrade to v2 and use optimistic locking on the same data in other services (e.g. a java lambda function)?

Thanks and best wishes for 2023!

knuspertante avatar Dec 29 '22 11:12 knuspertante

Hey @knuspertante so sorry for the delay here!! Yes, you should be able to migrate to v2 without losing the conflict resolution strategy. Were you able to migrate successfully?

josefaidt avatar May 09 '23 17:05 josefaidt

Hey @josefaidt,

no worries ;-) Nope, until now I did not try it again. As described here: https://docs.amplify.aws/cli/migration/transformer-migration/ @versioned is deprecated and no longer supported.

I unterstand, that AppSync’s built-in conflict detection is now the default behaviour. But I need a version attribute, cause I use the same dynamodb table in my backend java service which also uses / needs versioning for optimistic locking.

Is there any possible solution for my situation?

Thanks for any advice!

knuspertante avatar May 11 '23 15:05 knuspertante

Hey @josefaidt,

do you have any suggestion, how I can use the new transformer and also optimistic locking per attribute in my java backend?

THX!

knuspertante avatar Aug 03 '23 14:08 knuspertante