1

We are trying to use the JsonServiceClient to manually construct autoquery requests. The code is pretty simple for most operations but I don't see how filters are applied:

var client = new JsonServiceClient('https://my-app.azurewebsites.net');
var req = new dto.something();
req.pageSize = 10;
req.skip = 0;
req.take = 10;

What I don't see is how to send filters?

Here is the implementation of QueryBase:

export class QueryBase {
    // @DataMember(Order=1)
    skip: number;

    // @DataMember(Order=2)
    take: number;

    // @DataMember(Order=3)
    orderBy: string;

    // @DataMember(Order=4)
    orderByDesc: string;

    // @DataMember(Order=5)
    include: string;

    // @DataMember(Order=6)
    fields: string;

    // @DataMember(Order=7)
    meta: { [index: string]: string; };
}

Lastly, is there an analog to ToPostUrl/ToGetUrl for the javascript/typescript client?

lucuma
  • 18,247
  • 4
  • 66
  • 91

1 Answers1

2

New support for querying Services was added in 0.0.23 of the TypeScript JsonServiceClient which will let you add additional args when querying Services, e.g:

var request = new dto.MyQuery();

client.get(request, {MyField:"TheFilter"})
    .then(r => {});

It will also let call services by relative or absolute url with optional queryString arguments, e.g:

client.get<Response>("/my-request/foo")

client.get<Response>("http://example.org/my-request/foo")

client.get<Response>("/my-request", { arg: "foo"})

ServiceStack can only generate Typed properties for Explicit Conditions on your AutoQuery Request DTO.

There's no ToPostUrl/ToGetUrl equivalent in JavaScript/TypeScript but there are some URL helpers in ss-utils which can help create URLs, e.g:

var url = $.ss.createUrl("https://my-app.azurewebsites.net/myquery",   
    {pageSize:10,take:10,MyField:"TheFilter"});

$.getJSON(url, function(r) {
    console.log(r.Results);
});

Similar URL Helpers are also available in the servicestack-client npm package, e.g:

import { combinePaths, nameof, appendQueryString } from 'servicestack-client';

let baseUrl = "https://my-app.azurewebsites.net";
let requestDto = new MyQuery();
requestDto.take = 10;

let pathInfo = combinePaths(baseUrl, "json", "reply", nameof(requestDto));
let url = appendQueryString(pathInfo, requestDto);
mythz
  • 141,670
  • 29
  • 246
  • 390
  • Are you saying that the JsonServiceClient isn't a good candidate to send autoqueries? The thing is it is so nice to be able to use typescript to create the query request instead of using stringed urls. – lucuma Feb 28 '17 at 18:39
  • @lucuma it's for sending Typed Request DTOs, if you add properties on your Request DTO they will appear in the generated TypeScript DTOs. – mythz Feb 28 '17 at 18:41
  • I understood your point, only that skip, take, orderby, etc are in querybase but the filters are not so I thought I was missing something. – lucuma Feb 28 '17 at 18:43
  • @lucuma the typed properties on `QueryBase` are common to all AutoQuery requests. – mythz Feb 28 '17 at 18:50
  • If I create a custom querybase is there a way for me to manipulate the javascript/typescript client to set the query string params that I want? I'm trying to do this on the front end as opposed to the backend. I see your last update shows me how to get the URL. That is probably going to work in my case. – lucuma Feb 28 '17 at 18:53
  • @lucuma You shouldn't change QueryBase which is fixed, but you can create your own custom [TypeScript AutoQuery Request DTOs](http://techstacks.io/types/typescript?includeTypes=FindTechStacks,FindTechnologies,QueryBase,QueryDb`1,QueryResponse) and use that instead. The properties just has to match up with what AutoQuery expects. – mythz Feb 28 '17 at 18:58
  • 1
    Thanks @mythz. Being able to generate the url from the request object works great in the scenario (hooking into a kendo grid). I can manipulate the grid to send the correct querystring. I would suggest though allowing for some manipulation of the querystring for the javascript/typescript client. That seems to be missing (just the ability). – lucuma Feb 28 '17 at 19:14
  • 1
    This $.ss.createUrl is really great! Thanks! – labilbe Feb 28 '17 at 21:29
  • 1
    @lucuma See my updated answer for new support added to TypeScript JsonServiceClient – mythz Mar 01 '17 at 18:36
  • Awesome, I'll give it a go. If I had more time I'd have looked into updating the package too. – lucuma Mar 01 '17 at 18:38