Creating a Search Query

Last edit: Jun 05, 2019

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
    }
  }
}

Note

By default, the server does not return deleted records. This guide just uses it as an example of an argument that can be overridden.

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 graphql 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.

Important

$query that is passed through the form to the server will be passed to the GraphQL. It is a fuzzysearch and by default, it will filter results by every attribute there is.


---
slug: demo_search
---
{% assign query = params.query | default: nil %}
<form action="/demo_search" method="GET">
  {% graphql g = 'demo_search_transactables',
    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>

Important

Because you have set the is_deleted default value to false in the query, passing false is unnecessary.

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.