Search code examples
graphqlamazon-dynamodbaws-amplifyaws-appsyncvtl

Appsync Filtering list on @connection


I have a schema as such

type District @model {
    id: ID!
    name: String!
    workers: [Worker] @connection(name: "DistrictWorker")
}
type Service @model{
    id: ID!
    name: String!
    workers: [Worker] @connection(name: "ServiceWorker")
}
type Worker @model {
    id: ID!
    name: String!
    service: Service @connection(name: "ServiceWorker")
    district: District @connection(name: "DistrictWorker")
}

I want to query at Worker applying Service and/or District filter over the connection. I guess i have to write some custom resolver maybe pipelined resolver, I want some guidence on how could i achieve it.

Are there any different approach for achieving the same.


Solution

  • One can filter their queries over the connection by adding the desired filter field to their respective ModelFilterInput's. Amplify with Appsync does a great job by generating the resolver and wiring them up to their specific tables just from the schema we define. I, wonder why the generated ModelFilterInput's for the tables don't include the connection field.

    For the schema above defined i have modified the generated code as follows:

    input ModelWorkerFilterInput {
        id: ModelIDFilterInput
        name: ModelStringFilterInput
        source: ModelStringFilterInput
        phone: ModelStringFilterInput
        workerServiceId: ModelStringFilterInput # added For @connection(name: "ServiceWorker")
        workerDistrictId: ModelStringFilterInput # added For @connection(name: "DistrictWorker") 
        and: [ModelWorkerFilterInput]
        or: [ModelWorkerFilterInput]
        not: ModelWorkerFilterInput
    }