AWS amplify graphql appsync - do not return deleted items?
Asked Answered
A

4

9

I am using AWS amplify with graphql and appsync. When I do a standard list query, appsync includes deleted items in the list of items it returns.

What can I do to make it return only items that are not deleted?

I tried this query, but it throws an error:

query MyQuery($filter: ModelFrameFilterInput = {_deleted: {ne: true}}) {
  listFrames(filter: $filter) {
    items {
      _deleted
      name
      id
    }
  }
}

Here is the error message:

 "message": "Validation error of type BadValueForDefaultArg: Bad default value ObjectValue{objectFields=[ObjectField{name='_deleted', value=ObjectValue{objectFields=[ObjectField{name='ne', value=BooleanValue{value=true}}]}}]} for type ModelFrameFilterInput"
Anatropous answered 21/9, 2021 at 6:7 Comment(1)
any updates on this?Osmunda
P
2

This is not yet supported Look at this issue / feature request There's a workaround suggested, adding the _deleted in the filter input of the graphql schema.

input ModelTodoFilterInput {
    id: ModelIDInput
    name: ModelStringInput
    description: ModelStringInput
    and: [ModelTodoFilterInput]
    or: [ModelTodoFilterInput]
    not: ModelTodoFilterInput
    _deleted: ModelBooleanInput
}
Polio answered 8/6, 2022 at 10:1 Comment(2)
Do you know where to add this configuration? If I do it in the AppSync Console (in the Schema section) the code gets overwritten on my next push. If I do it in my front end (amplify/backend/api/<my app name>/schema.graphql then I need to copy dozens of lines of code from my console schema. For example, schema types for ModelIDInput, ModelStringInput, ModelStringInput, etc. Or is there a better way to do this that I don't understand?Jenifer
@Jenifer I don't know a better way than to copy the schema definition for the model inputs. I finally decided to use DataStore, which filters out the deleted items, and is working good for me. Copying the autogenerated code of the schema it's also annoying because you need to update it when you update your schema. I'm finally filtering the _deleted in the frontend for the few calls I do by graphql.Polio
F
1

One thing you can do is to disable Conflict Resolver, if it's not necessary for you, "after deleting elements that are using ConflictResolution, they are not immediately deleted from the database. Instead, two flags are being added: _deleted is set to true and _ttl is set to expire the object in 30 days." see: Error "Conflict resolver rejects mutation." when Delete in Amplify

To disable it, run amplify update api, and you will be prompt to a choice to disable conflict resolver

But if you are using DataStore, then it is must to enable conflict resolver. in that case, I don't know how to solve it.

Feel answered 8/3, 2022 at 7:33 Comment(0)
S
0

This is a known issue when using ConflictResolution. I have also faced this issue in the past. My solution was to use DynomoDB Streams Trigger with AWS Amplify to listen for DynamoDB update events and then delete the record via Lambda.

Here's the official documentation for AWS Amplify GraphQL Lambda Triggers

https://docs.amplify.aws/cli/usage/lambda-triggers/#as-a-part-of-the-graphql-api-types-with-model-annotation

DynamoDB triggers are almost real time so this works really well even with 128MB Lambda

Sluice answered 9/3, 2022 at 18:13 Comment(0)
I
0

You can try adding _deleted: { ne: true } to your filter like this:

const resp = API.graphql<GraphQLQuery<ListTodosQuery>>({
  query: queries.listTodos,
  variables: {
    filter: {
      _deleted: { ne: true }
    }
  }
});
Immoderate answered 18/9, 2023 at 9:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.