4

I have a graphql type with a one-to-many connection to another type. I want to filter the many using the one. So Amplify has generated the graphql schema but in the input for the list query there isn't a value for the connection to use.

The type:

type Event @model @auth(rules: [{ allow: owner }]) {
  id: ID!
  name: String!
  date: AWSDateTime!
  user: User! @connection(name: "EventsUser", sortField: "date")
  isDeleted: Boolean!
}

The query:

type Query {
  listEvents(filter: ModelEventFilterInput, limit: Int, nextToken: String): modelEventConnection

The list query input

input ModelEventFilterInput {
  id: ModelIDFilterInput
  name: ModelStringFilterInput
  date: ModelStringFilterInput
  isDeleted: ModelBooleanFilterInput
  and: [ModelEventFilterInput]
  or: [ModelEventFilterInput]
  not: ModelEventFilterInput
}

I tried passing the id in the variables object using:

    variables: {
        filter: {
            eventUserId: {
                eq: props.id,
            },

where eventUserId is the field name generated by amplify and used in the DynamoDB table, but this didn't work. How do you filter based on this value? Do I have to write this manually?

Adam

Edit

I have some of this figured out. I've added:

eventUserId: ModelEventUserInput

where ModelEventUserInput is

input ModelEventUserInput {
    eq: ID
}

to the ModelEventFilterInput input and then I use:

variables: {
    filter: {
        eventUserId: {
            eq: props.id,
        },

This works to filter the correct Events when the app is loaded by the filtering doesn't apply with subscriptions. I've tried just adding the filter object to the subscription constructor:

this.props.subscribeToEvents(
      buildSubscription({
        query: gql(onCreateEvent),
        variables: {
          owner,
          filter: {
            eventUserId: {
              eq: id,
            },
            isDeleted: {
              eq: false,
            },
          }
        },
      }, gql(listEvents))
    );

but without any luck. How can this filtering be achieved on subscriptions?

Adam

adamdaly
  • 315
  • 2
  • 12
  • did you figure it out? – Kavin404 Aug 10 '21 at 12:33
  • 1
    Unfortunately as it's so long ago I can't remember whether I did or not, but what I can tell you is that there were so many issues to figure out with this and with creating custom resolvers in that scripting language that AWS forces you to use that I scrapped AppSync completely and went with a custom SQL implementation, which probably took less time than figuring out everything with AppSync etc. Sorry, probably not what you want to hear, maybe its gotten better in the intervening almost 2 years. – adamdaly Aug 11 '21 at 13:42

0 Answers0