AppSync GraphQL @connection query

725 Views Asked by At

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

0

There are 0 best solutions below