Best practices for custom query and result set in Mule 4 with APIKit

106 Views Asked by At

I have a Mule 4 system layer API. It's currently set up in a manual way to return a list of data based on a custom query search:

RAML:

queryable:
    queryString:
      type: string
      example: "CustomerId=100"

Postman request:

{{systemlayerapi}}:{{port}}/api/customers?CustomerId=100

Stored variable:

<set-variable value='#[attributes.queryString replace /([\\])/ with ("") replace /(["])/ with ("")]' doc:name="queryString" doc:id="79fcfb07-7947-4011-8ed4-ef8c6e28cdd4" variableName="queryString" />

Translation to system request:

%dw 2.0
output application/xml inlineCloseOn="empty", skipNullOn = "everywhere"
---
{
    content:
    {
        function @(controlid: uuid()): {
            readByQuery: {
                fields: "*",
                (if ( vars.queryString != null or vars.queryString != "" )
                        query: vars.queryString
                    else
                        query: "")
            }
        }
    }
}

Keep in mind that I have the singular endpoint set up to get unique events (/api/customers/100) as is common practice. But I chose this way to retrieve lists of documents based on foreign keys. If they wanted to search for all customers in the US, they could do "/customers?COUNTRY=US" for example. This correctly passes the query to the system, which would return a paged result set of all customers with the foreign key of COUNTRY equating the the US.

SO. I have this set up with the "fields = *" parameter. Given the above example, I'd be returning ALL customer fields for ALL customers living in the US. The in-use memory spikes often and does a lot of garbage collection. I'm thinking that returning so many fields for so many records is hogging resources or causing a memory leak, and that I could reduce the load by giving developers the option to filter based on what fields they want returned.

Say I'm developing a customer process API, and I only want the FIRSTNAME, LASTNAME of all customers in the US. How could I add to my code to return those results? I don't want it to append the filter to the queryString variable:

{{systemlayerapi}}:{{port}}/api/customers?query=Country=US&filter=FIRSTNAME,LASTNAME

Not sure if this is a simple request and I'm just not familiar enough with Mule and APIKit to know how it's done or not. Thanks in advance.

1

There are 1 best solutions below

0
On

You should start from the API specification to add the second query param. Edit the RAML file then in Studio right-click on the project and then click on Mule / Regenerate Flows. Then you have to edit the flows to customize your logic.