"rid"s and call output : possible?

Hello,

Let say I have a basic service which is a user database. I listen to topics like :

  • call.users.create
  • get.users.{$userid}
  • call.users.{$userid}.delete

I would like to implement a search route, which has very advanced capabilities, and thus, a “get” call won’t be suitable because passing all filters in the query string is not suited (it will be a json passed as POST body).
So I create :

  • call.users.search

I would like to return the results as "rid"s like this :
{
“result” : {
“collection” : [
{“rid”:“get.users.63215”},
{“rid”:“get.users.48293”},
{“rid”:“get.users.59219”}
]
}
}

But this always fail (the rids are not resolved)
And from my testing it seems to be linked to the fact that it’s a “call” and not a “get” request.

Am I missing something to implement that use case ?

Thanks,

Lionel

Hi Lionel, and welcome to the forum! :slight_smile:

A bit late reply, but hopefully not too late
You are correct in your assumption; call requests can only respond with static JSON data, or a reference to a single resource. But you have a few options how to solve this:

Get request

You can still go with a get request. This is the way to go if you want the resulting collection to be updated live; meaning, that the client can have the resulting collection be up-to-date, even when the underlaying data changes.

And regarding complex queries; you don’t have to use the “standard” query format. You are free to use any format, such as URI encoded JSON. Eg:

client.get("users?" + encodeURI(JSON.stringify({limit:10, roles:["admin","guest"], offset:100})))
    .then(result => console.log(result));

Call request returning resource reference to search result

This options let’s you use send params in the call request, but the result is fetched in a separate get request. So, in this case, you will actually have 2 handlers:

call.users.search
get.users.search.{$searchid}

The call.users.search request would take the provided (json) params, maybe validate them, and store them in a database table (maybe including some user ID of who made the call), and then return a reference to the search result resource:

Resource response to the call request:

{"resource":"users.search.ID_OF_THE_STORED_QUERY_PARAMS"}

This will make Resgate follow that resource reference and fetch the collection from the get.users.search.{$searchid} handler. So it is the get handler that uses the search params and performs the query against the database, and returns the collection that you showed above.

The client will only see this as a single call, even if Resgate makes 3 calls in total:

client.call("users", "search", {limit:10, roles: ["admin", "guest"], offset:100})
    .then(result => console.log(result));

Call request with static result

Or, you can just send a big JSON blob of data as the result to the call request. But then you lose all the beauty of simply referencing resources.

This is not really what I would recommend.

Hope that helps!
/Samuel

1 Like