Can we query from Javascript?

Support Ticket

+status
answered
 

Not sure if it's better to ask this here or on github, but we've encountered a use case where we'd like to use WQL in the client javascript.  I know it's generally considered poor practice to query from the client, but I don't know of any alternative within Wagn.  We noticed that one of the example sites, WikiRate, does this with their metrics but it looks like they're using the RESTful API to get the card data.  Is this the best practice?

The simplest example would be presenting the average rating of a game to a user.  We'd like to search for each rating of this game entry and then take the average in the same javascript that handles the interface.  A simple hack would be create a search card that finds these ratings in open view and then uses javascript to hide all the searched items, displaying only the average rating.  But obviously that's not a permanent solution (and it doesn't solve more complex use cases).  Can we do this without editing the underlying ruby code or other internals of the Wagn site?  For now we're using the default hosting through cloudstore, but we'd be willing to host it ourselves and make simple modifications if there's no other way.

 

Generally speaking, the recommended pattern is something like this:

 

1. compose and execute WQL on the server (in cards)

2. request a json response to that query (typically by adding ".json" to a search card)

3. use that json in your javascript.

 

 

For #1 you can consider passing variable names (see $keyword in search bar) or daisy-chaining search cards (eg ratings+gametype+whatever).

 

For #2, you might look at some of the views here, especially atom.

 

Neither of these are well documented, in part because the API is not considered finalized. So there's a chance future upgrades may require some code refactoring, but we are deeply committed to taking our support for this kind of work much further.

--Ethan McCutchen.....2016-10-30 02:06:51 UTC