How to filter the products based on product_type, that are retrieved using collectionByHandle in shopify storefront graphQL api? - graphql

In our app, we are displaying the list of collections from the shopify store. When user click on any of collection, it will redirect to product list, where all the products of that clicked collection will be displayed.
Here is the sample query which I used to get the products list of particular collection :
{
collectionByHandle(handle: "") {
products(first: 10) {
edges {
node {
id
title
}
}
}
}
}
Now, I want filtering on products list. I want to display products base on product_type. But there is no any option to filter the products.
query option only available when you retrieve simple products listing using this query :
{
products(query: "product_type:casual-things", first: 10) {
edges {
node {
id
title
productType
}
}
}
}
But, this query option not available on products node of collectionByHandle.
Can, anyone help me how to implement filter option using Shopify storefront graphQL api in collectionByHandle query ?

The products connection doesn't have a query argument. You have basically access to fetching all products within that collectionByHandle and then filtering them client-side. You must have to use liquid for these types of nested features.

Related

Shopify Graphql how to get available products for specific collection based on location

Shopify GraphQL. I want to have a list of products for specific collection based on location.
Expectrd list of products.
I tried:
query($collectionId: ID!, $locationId: ID!) {
collection(id: $collectionId) {
products(first: 50) {
edges {
node {
id
title
availableForSale
images(first: 50) {
edges {
node {
id
altText
originalSrc
}
}
}
variants(first: 50, query: "location_id:$locationId") {
edges {
node {
id
title
inventoryQuantity
sku
}
}
}
}
}
}
}
}
But I have an error for variants not support query
It is true. Variants do not in fact have a location. But they do have an inventory item ID. And when you leverage that, you'll find you can get the inventory levels of any particular inventory item ID at locations.
So if you were to extract the availability of an item at a location, ie) a variant, you would also know that the product the variant belongs to, is stocked at that location.
Your approach of starting with a collection, and then getting the products in that collection, and then getting the variants of each product, and then checking if the variant is stocked at one or more locations, is really really clunky.
I think instead you should extract the inventory at each location. That would then give you products. With those products you might be able to create a collection to hold them. But since location is not a criteria, you're going to have to make up something.

Hasura complex search of products

How can i solve this problem with complex searching in products?
This is what i have now as a standard get products and search on them.
query getProducts($limit: Int, $offset: Int, $searchQuery: String) {
products_products(limit: $limit, offset: $offset, where: {_or: [{title: {_ilike: $searchQuery}}, {shortDescription: {_ilike: $searchQuery}}], active: {_eq: true}}) {
__typename
id
shortDescription
title
likes {
userId
}
images(where: {main: {_eq: true}}) {
image
}
categories {
category {
title
}
}
}
}
i have products table, categories table, join table(productId, categoryId), later on there would be reviews table, wishlist table
how to show products only in any category (record in join table)?
how to show products with reviews 3.5+ ?
You know how it is with complex filtration.
Can someone show me how it could be done?
Lastly .. im using flutter with optimistic results, so thats why i chose this structure of query. it makes sense when "replicating" the state of wishlist.
how to show products only in any category (record in join table)?
You can filter on related data as well
query {
product(where: { category: { name: { _eq: "Kitchen" } } })
}
how to show products with reviews 3.5+ ?
I assume you mean an average review of 3.5 stars or greater?
There is a brand new feature in Hasura called Aggregation Predicates that lets you do this
See last month's community call demo for how to use it:
https://youtu.be/4LUiztVe8EA?t=1505
By the way, if you are building an e-commerce type application, there is a sample application which already implements much of this functionality so you can see how it is done:
https://hasura.io/reference-app/
https://github.com/hasura/hasura-ecommerce

shopify storefront api doesn't show filter results as expected

I'm building an app using shopify Storefront api. So far I'm able to fetch products, collections. But when I try to filter the products in a collection it is not returning results as expected. Here is what i'm trying in postman
{
collection(id:"Z2lkOi8vc2hvcGlmeS9Db2xsZWN // collection id"){
products(first:10, filters:[{productVendor:"ADDIDAS"}]){
edges{
node{
id
title
description
productType
vendor
}
}
}
}
}
But i'm getting results that have vendor of NIKE and other types. Do i need to change something?
But when i try filter using price it works fine but when i try filtering using productType it is giving me not expected results. Am i doing something wrong?
Please help here

Graphql- How to fetch result based on the condition of a field?

I have a query that look like this:
query MyQuery {
products {
edges {
node {
featured
id
image {
altText
mediaItemUrl
slug
}
productId
name
onSale
}
}
}
}
What I want is only fetch the result that featured field is true, if the featured is false, then it never shown in the result.
Something like query like below in mysql:
SELECT id,image,name, featured FROM products WHERE featured = 'false'
But in graphql query above, I can't query the featured = false.
I tried:
query MyQuery {
products {
edges {
node {
featured #include(if: false)
id
... other field I need
}
}
}
}
But what this query do is, if featured field is true, then included the featured field in the result, else don't included the field in the result.This is not what I want.
What I want is,
If featured field of a product is true, then include the products into the result, else, remove the whole product from the result.
How can I achieve this in the MyQuery above?
The #include and #skip directives are used for field selection, not filtering. GraphQL has no built-in way of doing filtering, sorting or pagination. It's up to each individual service to implement these features by exposing the appropriate arguments.
In this case, products could expose an argument named filter or isFeatured to add the ability to filter the results by the featured value. The field's resolver should then use that argument value to determine the correct value to return.
If you're writing client queries and consuming a schema you did not write, check your API's documentation to determine what arguments are available for the products field. If the schema doesn't expose this capability and you don't have a way to change it, then as a client you don't have many options. At best, you can handle the filtering yourself after the result is fetched, but this is troublesome if you also use pagination.

GraphQL: Is it possible to search nested field?

I am specifically using the shopify graphql admin api to query orders.
I want to do a search for a nested related field.
Below is my query.
export const orderHistoryQuery = gql`
query Order($productsFirst: Int!, $productsAfter: String, $filterQuery: String) {
orders(first: $productsFirst, after: $productsAfter, reverse: true, query:$filterQuery) {
edges {
cursor
node {
id
name
customer {
id
metafields(first: 10) {
edges {
node {
id
key
value
namespace
}
cursor
}
}
}
totalPriceSet {
shopMoney {
amount
currencyCode
}
}
subtotalPriceSet {
shopMoney {
amount
currencyCode
}
}
totalRefundedSet {
shopMoney {
amount
currencyCode
}
}
currencyCode
email
phone
processedAt
totalShippingPriceSet {
shopMoney {
amount
currencyCode
}
}
totalTaxSet {
shopMoney {
amount
currencyCode
}
}
shippingAddress {
firstName
lastName
address1
address2
city
province
zip
country
}
billingAddress {
firstName
lastName
address1
address2
city
province
zip
country
}
customAttributes {
key
value
}
}
}
}
}
`;
I want to query metafields or ANYTHING really but it doesn't seem like it's supported. I am not sure if I just have the wrong query syntax or if it's not supported. The shopify search syntax documenation doesn't really help and this is where my knowledge of graphql falls apart.
Is it possible to do this in graphql? I also tried adding metafields(id: $whateverID) which is not supported by their setup.
Unfortunately, Shopify doesn't support query filters on metafields. The best way to figure this out is by using a graphql explorer like GraphiQL. Shopify dashboard has this built in if you go to Apps > Shopify GraphiQL App.
Using GraphiQL you can see that:
Customers query doesn't have metafields supported:
Orders query doesn't have customers or metafields supported:
And metafields on customers doesn't have a query param:
I think your options are to either query by what you can and filter after you get the results or use a customer tag and query by tag.
You would really help your cause out by simplifying things. My advice to you is to try a simple query. Can you get an order? Since an order has a customer (usually but not always), can you get a metafield associated with that customer?
You have so many obstacles in your attempt to show what you are trying to do, it is almost as if you want a migraine headache in trying to debug anything. GraphQL calls to endpoints are documented fairly well from the GraphQL website perspective, and Shopify is nothing but a vanilla implementation of that, with the caveat that they charge you for calls based on complexity, so you had best be monitoring your credits.
So ya, try simple calls. Get a product and it's Metafields. Get a customer record and it's Metafields. If you can do that, you are not challenging the documentation much, nor the concept of GraphQL queries. Once a basic all works, you can work in variables, cursors, paging, etc... but until a one-off call gives you what you want, debugging should be concentrated on the simplest of calls, not everything and the kitchen sink.
Also, when you screw up a call to the endpoint, Shopify usually returns a response with details about where you screwed up, providing you with a first place to look. We see nothing of your response, so there is little to go on to help you.

Resources