{"componentChunkName":"component---src-templates-post-template-js","path":"/blog/posts/designing-filters-for-a-list-api/","result":{"data":{"markdownRemark":{"id":"f7716970-4c0a-5b7d-81f0-f3c9a0cc9e1e","html":"<p>An inventory screen needs to show active products that are low on stock or backordered. It also needs a total count and a stable sort order. I’d give the API a shared vocabulary for filters and pagination, then let each endpoint define the fields callers can use and the records they can see.</p>\n<h2 id=\"give-each-filter-three-parts\" style=\"position:relative;\"><a href=\"#give-each-filter-three-parts\" aria-label=\"give each filter three parts permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Give each filter three parts</h2>\n<p>A filter has a field, an operator, and a value. For example, <code class=\"language-text\">{&quot;field&quot;:&quot;stock&quot;,&quot;op&quot;:&quot;lt&quot;,&quot;value&quot;:10}</code> asks for products with fewer than ten units in stock. An inventory endpoint can allow numeric comparisons on stock and text matching on names. Each endpoint maps its public fields to SQL expressions, so a field like <code class=\"language-text\">color</code> can live inside a JSON column without changing the request shape.</p>\n<p>The query builder picks the SQL expression and comparison from those predefined mappings. It passes the value as a SQL parameter.</p>\n<h2 id=\"keep-access-rules-outside-the-filter\" style=\"position:relative;\"><a href=\"#keep-access-rules-outside-the-filter\" aria-label=\"keep access rules outside the filter permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Keep access rules outside the filter</h2>\n<p>In a multi-store catalog, the server determines which store the caller can see. The generated predicate should have this shape:</p>\n<div class=\"gatsby-highlight\" data-language=\"sql\"><pre class=\"language-sql\"><code class=\"language-sql\"><span class=\"token keyword\">WHERE</span> store_id <span class=\"token operator\">=</span> <span class=\"token variable\">@store_id</span>\n  <span class=\"token operator\">AND</span> <span class=\"token keyword\">status</span> <span class=\"token operator\">=</span> <span class=\"token variable\">@status</span>\n  <span class=\"token operator\">AND</span> <span class=\"token punctuation\">(</span>stock <span class=\"token operator\">&lt;</span> <span class=\"token variable\">@threshold</span> <span class=\"token operator\">OR</span> backordered <span class=\"token operator\">=</span> <span class=\"token variable\">@backordered</span><span class=\"token punctuation\">)</span></code></pre></div>\n<p>The parentheses keep the two alternatives inside the store’s scope. I’d test this with a backordered product in another store and assert that it stays out of both the results and the total.</p>\n<h2 id=\"make-the-page-and-count-agree\" style=\"position:relative;\"><a href=\"#make-the-page-and-count-agree\" aria-label=\"make the page and count agree permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Make the page and count agree</h2>\n<p>Use the same predicates for the page query and the count query. If two products share a name, add the product ID as a tie-breaker so their order is deterministic for a given dataset. A good pagination test requests one item per page and checks that each page reports the same matching total.</p>\n<p>The shared contract stays small. The endpoint still owns field mappings, access rules, and the database query.</p>","fields":{"slug":"/blog/posts/designing-filters-for-a-list-api/","tagSlugs":["/blog/tag/ap-is/","/blog/tag/sql/"]},"frontmatter":{"date":"2026-11-25T12:00:00.000Z","description":"A small query contract for filtering, sorting, and paging an inventory API.","tags":["APIs","SQL"],"title":"Designing filters for a list API"}}},"pageContext":{"slug":"/blog/posts/designing-filters-for-a-list-api/"}},"staticQueryHashes":["251939775","401334301","825871152"]}