Products Filter by title in rails app + shopify_app gem

Viewed 488

I have tried mostly everything. Checked all docs and stack questions and shopify community like:

Shopify API how to do a search query with like

https://community.shopify.com/c/Shopify-APIs-SDKs/Shopify-api-search-products-by-title/td-p/341866

How to search products by title using Shopify product search API?

https://github.com/Shopify/shopify_app

https://community.shopify.com/c/Shopify-APIs-SDKs/Search-product-from-title-handle-and-description/td-p/469156

and found out that

@search = "2018";
@products = ShopifyAPI::Product.find(:all, params: { limit: 10,title:@search })

but this is returning empty array although I have may records containing this in title. https://prnt.sc/sx37o6

I want to get records according to @search

I have tried Product.search too but it causes: undefined method `search' for ShopifyAPI::Product:Class

1 Answers

Using the RestAPI I failed doing filtering (with wildcards for example) as well. But with the GraphQL-API the search functionalities (see here https://shopify.dev/concepts/about-apis/search-syntax) are pretty solid.

This is an example including auth, filtering by title including wildcard-support (for part matching) and mapping results to a simpel array of hashes:

@responses = []

shopify_session = ShopifyAPI::Session.temp(
  domain:  shop.shopify_domain,
  token: shop.shopify_token,
  api_version: ShopifyApp.configuration.api_version
) do
  client = ShopifyAPI::GraphQL.client
  ql_query = <<-GRAPHQL
    {
      products(first: 10, query: "title:*#{query}*") {
        edges {
          node {
            id
            title
            handle
          }
        }
      }
    }
  GRAPHQL

  query_result = client.query(client.parse(ql_query))
  query_result.data.products.edges.each do |result|
    @responses << {
      id: result.node.id,
      title: result.node.title,
      handle: result.node.handle
    }
  end
end

@responses
Related