I have a collection of listings that I want to display in my app by the newest added first. I have created an index to sort them via their timestamp like so:
CreateIndex({
name: "all_Listings_By_Newest",
serialized: true,
source: [Collection("Listings")],
values: [
{
field: ["data", "_ts"],
reverse: true
},
{
field: ["ref"]
}
]
})
This seems to work as intended.
Next, I try to create a UDF to access this index with pagination as per the example in the documentation.
CreateFunction({
name: "newest_listings",
body: Query(Lambda(["size", "after", "before"],
Let(
{
match: Match(Index("all_Listings_By_Newest"), true),
page: If(
Equals(Var("before"), null),
If(
Equals(Var("after"), null),
Paginate(Var("match"), { size: Var("size") }),
Paginate(Var("match"), { size: Var("size"), after: Var("after") })
),
Paginate(Var("match"), { size: Var("size"), before: Var("before") }),
)
},
Map(Var("page"), Lambda("ref", Get(Var("ref"))))
)
))
})
Next I try to access this through a resolver with my graphql schema
newestListing: [Listing!] @resolver(name: "newest_listings", paginated: true)
I can then access the graphQL query in the graphQL playground. I don’t get any errors, but I also don’t get any results. The data array is empty and before and after are both null.
If I alter the UDF to be:
CreateFunction({
name: "newest_listing",
body: Query(Lambda(["size", "after", "before"],
Let(
{
match: Match(Index("all_Listings_By_Newest"), true),
page: If(
Equals(Var("before"), null),
If(
Equals(Var("after"), null),
Paginate(Var("match"), { size: Var("size") }),
Paginate(Var("match"), { size: Var("size"), after: Var("after") })
),
Paginate(Var("match"), { size: Var("size"), before: Var("before") }),
)
},
Map(Var("page"), Lambda(['ts',"ref"], Get(Var("ref"))))
)
))
})
I am able to access the data, but before and after continue to be null.
I’m not sure if I have gone wrong in how I created the index or how I’m writing the UDF. If anyone can help point me towards what I’m overlooking, it is greatly appreciated.