How do I create a function for basic filtering?

I have a Query to filter my Premises by a query called premiseBySelectedFlag Like this.

 premiseBySelectedFlag(selected: Boolean!): [Premise!]!
    @resolver(name: "listAllSelectedPremises", paginated: true)

How do I create a function for this? I want to show Premises that have the selected value of trueonly.

Currently, I have a basic function like this:

Query(
  Lambda(
    ["size", "after", "before"],
    Let(
      {
        match: Match(Index("latestSelectedPremises")),
        page: If(
          Equals(Var("before"), null),
          If(
            Equals(Var("after"), null),
            Paginate(Var("match"), { size: Var("size") }),
            Paginate(Var("match"), { after: Var("after"), size: Var("size") })
          ),
          Paginate(Var("match"), { before: Var("before"), size: Var("size") })
        )
      },
      Map(Var("page"), Lambda(["_", "ref"], Get(Var("ref"))))
    )
  )
)

But When I do this in my GraphQL playground

query{
  premiseBySelectedFlag(selected: true){
    data{
      _id
      content
    }
  }
}

I get this error in my GraphQL

{
  "errors": [
    {
      "message": "Lambda expects an array with 3 elements. Array contains 4.\n",
      "extensions": {
        "code": "invalid argument"
      }
    }
  ]
}

I then changed it to this:

Query(
  Lambda(
    ["selected","size", "after", "before"],
    Let(
      {
        match: Match(Index("latestSelectedPremises")),
        page: If(
          Equals(Var("before"), null),
          If(
            Equals(Var("after"), null),
            Paginate(Var("match"), { size: Var("size") }),
            Paginate(Var("match"), { after: Var("after"), size: Var("size") })
          ),
          Paginate(Var("match"), { before: Var("before"), size: Var("size") })
        )
      },
      Map(Var("page"), Lambda(["_", "ref"], Get(Var("ref"))))
    )
  )
)

When I do this in my GraphQL playground, It shows all Premises for both selected: true and selected:false

What is the correct way to create this function? To be honest, I don’t even know where to start. I just want to filter my Premise object by Selected: True.

PS. I created the index latestSelectedPremises

Is this correct?

Thank you

I’d create an index like this. You need to add selected as a term so that you can match on only selected Premises. Also, I’ve added reverse:true so that you get them in descending order since you specified ‘latest’. Finally, I changed the index name because you can’t update an existing index’s terms or values:

CreateIndex({
  name: 'latestSelectedPremises2',
  source: [
    {
      collection: Collection('Premise')
    }
  ],
  terms: [
    {
      field: ['data', 'selected']
    }
  ],
  values: [
    {
      field: ['data', 'createdAt'],
      reverse: true
    },
    {
      field: ['ref']
    }
  ]
})

Now when you do your match, make sure to pass in the term:

Match(Index("latestSelectedPremises2"), true)

And that will get you the set of Premises that have selected === true. And finally, the match result is a single array, which you have to use Select to grab individual values from. The values aren’t spread out as multiple parameters in the Map’s lambda. Select(1, Var("matchResult")) will get you the value at index 1, which is the second value in the values array:

Query(
  Lambda(
    ["selected","size", "after", "before"],
    Let(
      {
        match: Match(Index("latestSelectedPremises2"), true),
        page: If(
          Equals(Var("before"), null),
          If(
            Equals(Var("after"), null),
            Paginate(Var("match"), { size: Var("size") }),
            Paginate(Var("match"), { after: Var("after"), size: Var("size") })
          ),
          Paginate(Var("match"), { before: Var("before"), size: Var("size") })
        )
      },
      Map(Var("page"), Lambda("matchResult", Get(Select(1, Var("matchResult")))))
    )
  )
)
1 Like

Absolutely amazing. Not only does this work, but I have also learned much more about how this whole system works.

Thank you so much for taking the time to help me out.

1 Like

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.