Creating a Search Query

Last edit:  Jan 03, 2019

Contributors:  lemingos diana-lakatos

This guide will help you create a search query to find all Transactables that haven't been deleted. This is a minimum example to show you the basics of creating search queries.

Requirements

So that you can follow the steps in this tutorial, you should understand the concept of Search:

Steps

Creating a search query is a two-step process:

Step 1: Create GraphQL query

Create a GraphQL query to return all Transactables that haven't been deleted.

query demo_search_transactables($query: String, $is_deleted: Boolean = false) {
  listings(query: $query, listing: { is_deleted: $is_deleted }) {
    total_entries
    results {
      slug
      name
    }
  }
}

The above GraphQL query consists of three main sections:

  1. Query arguments: usually passed from the liquid to parameterize your query. In the example below, $query: String. Define what will be used in the filters section and what is the data type of passed argument. It is also possible to set a default value for any given argument. The default value will be used if it is not passed to the execute query tag.
  2. Filters: used to filter down the results by given arguments. In the example above, listings are filtered so they only contain results with is_deleted flag set to false. If you pass true in $is_deleted it will do the opposite.
  3. Results: just like in any other query, defines which data you want to receive and in what form.

Step 2: Display search results

Now pull out slug and name of the resulting Transactables into an unordered list.


---
slug: demo_search
---
{% assign query = params.query | default: nil %}
<form action="/demo_search" method="GET">
  {% query_graph 'demo_search_transactables',
    result_name: 'g',
    query: query
  %}
  <input name="query" value="{{ query }}" />
  <input type="submit" value="Search" />
</form>

<p>Total: <strong>{{ g.listings.total_entries }}</strong></p>

<ul>
{% for listing in g.listings.results %}
  <li>{{ listing.slug }} - {{ listing.name }}</li>
{% endfor %}
</ul>

Next steps

Congratulations! You have created a search query. Now you can go on and learn about payments:

Questions?

We are always happy to help with any questions you may have. Check out our Help page, or contact us.