Picking a random has already been mentioned here and on github, but no feature request ticket seems to have been created.
I’m still in the need of such a feature as it would greatly improve:
Performances because I currently need to do random queries twice (in a transaction): First to count nodes, second to set one or more random offsets. My biggest and slowest query right now is to do matchmaking in my game, and I have to do it twice each time.
Code readability because my current solution requires a big function.
The syntax could be similar to first and offset keywords: random: 3 which would mean we want to pick 3 different random nodes from the current result set. It would perfectly work with offset and first to select the range before picking random nodes.
Please consider my request, I’m sure many projects would benefit from it
Once we have a count, we can pick random nodes from the client side. Right now there is no way to know what the maximum node is to provide a random offset to a maximum value.
This of course would be limited to picking a random group of N nodes and not N random nodes. The use case needing N random nodes could be worked around by doing N requests which could be within a single query.
Indeed that’s pretty much what I’m doing right now:
A first request to count available nodes according to my filters
A second request, in the same transaction, with same filters, and split into query blocks, as many as required random nodes, each having a random offset.
Be aware that doing the second read query in the same transaction may fail when there have been a hand full of mutations in the meanwhile. After some mutations a rollup happens which makes the startTs of your transaction disappear.
Sure, you can retry the two steps until you succeed, but just so you know that this situation can occur with concurrent writes.
Another good reason for having random selectors is to reduce transaction conflicts when returned nodes might be used by parallel threads for mutations.
In my scenario, random nodes are games opened by single players (creators). Opponent players try to pick a random game to play with game creators. But if 2 opponent pick the same game, one will be rejected thanks to a transaction conflict.
If MANY players play together, I get LOTS of transaction conflicts, because all opponents try to get the same open game, then all but one fail and finally they all try to get the same other open game, and so on.
I would like to work on this issue. I’m guessing this will have to go in the query/query.go file, but other than that I’m a little lost in the codebase. I’ve read the docs page on pagination (since first and offset were mentioned here).
Could anyone give me a rough idea of what needs to be done (in terms of the source code)?
can anyone please tell me if there is a rand function accessible from graphql?
From reading various posts on here it seems there was a pull request merged at some point but is somehow not available in recent versions?