Passing optional parameters to endpoints

Hi again,

I need to create an endpoint with optional parameters:
/endpoint?param1=xxx&param2=yyy
or /endpoint?param1=xxxxxx
or /endpoint?param2=xxxx
or /endpoint

What is the way to implement this with raw sql?

Thank you!

Hello.

That HTTP endpoint that takes URL parameters, is mapped to an RQL function that takes these parameters, right?

RQL has a syntax to specify that a function parameter is optional. As an example I declare your endpoint RQL function with optional parameters:

f(param1: int := 2022, param2: string := "Switzerland") := {
   SELECT ... FROM ...
   WHERE year = param1 AND country = param2
} 

After pointing the .yml file to that function, the HTTP endpoint URL parameters behave the same way as those of the function. When omitted in the URL, execution takes place with the parameter taking the default value.

Thanks for the answer. But what happens if I do

foo(param1: int null, param2: string null) := {
   SELECT ... FROM ...
   WHERE year = param1 AND country = param2
} 

and I call /foo without parameters? the WHERE clause is going to check against null values.
Actually
if param1 is null and param2 is null I would want no WHERE clause.
if param1 is null then it should be testing only param2 in the WHERE clause
if param2 is null then it should be testing only param1 in the WHERE clause
How to do that ?
Thanks!

You can use the ISNULL function. For example, you could change your condition operations to the following;

foo(param1: int null, param2: string null) := {
   SELECT ... FROM ...
   WHERE ISNULL(year = param1, TRUE) AND ISNULL(country = param2, TRUE)
} 

Wow! Smart! :star_struck:

Thanks!