title: Best practices for custom query and result set in Mule 4 with APIKit
body: <p>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:</p>
  <p>RAML:</p>
  <pre><code>queryable:
      queryString:
        type: string
        example: &quot;CustomerId=100&quot;
  </code></pre>
  <p>Postman request:</p>
  <pre><code>{{systemlayerapi}}:{{port}}/api/customers?CustomerId=100
  </code></pre>
  <p>Stored variable:</p>
  <pre><code>&lt;set-variable value='#[attributes.queryString replace /([\\])/ with (&quot;&quot;) replace /([&quot;])/ with (&quot;&quot;)]' doc:name=&quot;queryString&quot; doc:id=&quot;79fcfb07-7947-4011-8ed4-ef8c6e28cdd4&quot; variableName=&quot;queryString&quot; /&gt;
  
  </code></pre>
  <p>Translation to system request:</p>
  <pre><code>%dw 2.0
  output application/xml inlineCloseOn=&quot;empty&quot;, skipNullOn = &quot;everywhere&quot;
  ---
  {
      content:
      {
          function @(controlid: uuid()): {
              readByQuery: {
                  fields: &quot;*&quot;,
                  (if ( vars.queryString != null or vars.queryString != &quot;&quot; )
                          query: vars.queryString
                      else
                          query: &quot;&quot;)
              }
          }
      }
  }
  </code></pre>
  <p>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 &quot;/customers?COUNTRY=US&quot; 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.</p>
  <p><strong>SO</strong>. I have this set up with the &quot;fields = *&quot; 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.</p>
  <p>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:</p>
  <pre><code>{{systemlayerapi}}:{{port}}/api/customers?query=Country=US&amp;filter=FIRSTNAME,LASTNAME
  </code></pre>
  <p>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.</p>