How to select multiple documents from an array of ids?

Hello,

I’m trying to emulate a where id in (x,y,z) in GraphQL but I’m getting no results back.

I’ve created a custom query as such

timeSlotsByIds(ids: [ID!]): [TimeSlot] @resolver(name: "slots_by_ids", paginated: true)

and created an index with _id as the key.

my custom function is

Query(
  Lambda(
    ["ids", "foo", "bar", "baz"],
    Paginate(Match(Index("slot_by_id"), Var("ids")))
  )
)

Now I understand there is no _id field on the document, but I wasn’t sure how to index the ref part for it.

Thanks for any pointers!

Hi @Christophe_Verbinnen,

the resolver can be like below:

CreateFunction({
  name: "slots_by_ids",
  role: null,
  body: Query(
        Lambda('ids',
          Reduce(Lambda(['acc','value'],
            Append(Var('acc'),
              Select(['data'],
                Paginate(Match('slot_by_id',Var('value'))
                )
              )
            )
          ),
          [],
          Var('ids'))
        )
      )
  }
)

and you can call the function by passing an array:

Call(Function("slots_by_ids"),['baz','fos'])

and it returns an array of Refs.

Hope this helps.

Luigi

2 Likes

Thank you.

I ended up using

Query(
  Lambda(
    ["ids"],
    If(
      IsArray(Var("ids")),
      Map(
        Var("ids"),
        Lambda("id", Get(Ref(Collection("TimeSlot"), Var("id"))))
      ),
      []
    )
  )
)