I'm finding GraphQL queries very unreliable.

Solved

I'm finding GraphQL queries very unreliable.

pgj99
Shopify Partner
5 0 0

I have a simple graphql query, see below.  But it behaves very oddly.  If the vendor search word has a space in it, it seems to work exactly as you would expect. However, if there is no space in the vendor name, it appears that everything gets returned.

The query is a bit limited anyway as I can only get back a max of about 30 orders.  What I'm trying to do is just get orders relevant to a particular vendor and ignore the rest.  Filtering could be done outside Shopify, but ideally we don't want all that data going to our servers.

 

{
  orders(first10query"lineItems.product.vendor:='My Vendor'") {
    edges {
      node {
        id
        updatedAt
        createdAt
        lineItems(first10) {
          edges {
            node {
              id
              title
              variant {
                id
                title
              }
              product {
                vendor
              }
            }
          }
        }
      }
    }
  }
}
Accepted Solution (1)

ShopifyDevSup
Shopify Staff
1453 238 508

This is an accepted solution.

Hi @pgj99 👋

 

I'd recommend reviewing this list of supported filter parameters for the `query` argument. Since `lineItems.product.vendor` is not a supported parameter, the `orders` query ignores the invalid filter and returns all unfiltered results. This guide highlights how to effectively use the search syntax as well.

 

Hope that helps!

Developer Support @ Shopify
- Was this reply helpful? Click Like to let us know!
- Was your question answered? Mark it as an Accepted Solution
- To learn more visit Shopify.dev or the Shopify Web Design and Development Blog

View solution in original post

Replies 4 (4)

Liam
Community Manager
3108 340 871

The issue you're encountering might be due to the way Shopify's Search API handles non-space-separated words. When the vendor name doesn't have a space, the search query may treat the vendor name as a single token and thus not return the expected results.

 

In your case, you can try enclosing your vendor name in quotes, like so:

 

{
  orders(first: 10, query: "lineItems.product.vendor:\"MyVendor\"") {
    edges {
      node {
        id
        updatedAt
        createdAt
        lineItems(first: 10) {
          edges {
            node {
              id
              title
              variant {
                id
                title
              }
              product {
                vendor
              }
            }
          }
        }
      }
    }
  }
}

 

This will explicitly treat "MyVendor" as a single string.

 

For dealing with the issue of only getting 30 orders back at a time, you can paginate your results using cursor-based pagination. You can use the `after` argument and pass the cursor of the last edge in the previous page to get the next page of results. Here is an example of how to do that:

{
  orders(first: 10, after:"<cursor>", query: "lineItems.product.vendor:\"MyVendor\"") {
    pageInfo {
      hasNextPage
    }
    edges {
      cursor
      node {
        id
        updatedAt
        createdAt
        lineItems(first: 10) {
          edges {
            node {
              id
              title
              variant {
                id
                title
              }
              product {
                vendor
              }
            }
          }
        }
      }
    }
  }
}

Here, replace `<cursor>` with the cursor of the last item from the previous query. `pageInfo` tells you whether there are more pages to fetch. If `hasNextPage` is true, you can take the `cursor` of the last edge in `edges` and use that as the `after` argument in the next query to fetch the next page.

 

Try this out and see if it works for your purposes!

Liam | Developer Advocate @ Shopify 
 - Was my reply helpful? Click Like to let me know! 
 - Was your question answered? Mark it as an Accepted Solution
 - To learn more visit Shopify.dev or the Shopify Web Design and Development Blog

pgj99
Shopify Partner
5 0 0

Thanks Liam,

The cursor should work well for what we need, however the issue with the query was actually that it works as 2 words but not as a single.

 

e.g.

orders(first10query"lineItems.product.vendor:=\"Yappy Dev\"")
This currently returns all orders that have a product with Yappy Dev as a vendor and nothing else.  If 2 lines on in the order one Yappy Dev one not, it will also return the order as expected.

This however...
orders(first10query"lineItems.product.vendor:=\"Burton\"")
Returns everything.  Some of my orders do have Burton as a vendor as this is just test data.

Also I could even query "asdf" or something and it still brings back all results.  But "asdf asdf" would bring back nothing, which would be correct in this case.

Thanks,
Paul

ShopifyDevSup
Shopify Staff
1453 238 508

This is an accepted solution.

Hi @pgj99 👋

 

I'd recommend reviewing this list of supported filter parameters for the `query` argument. Since `lineItems.product.vendor` is not a supported parameter, the `orders` query ignores the invalid filter and returns all unfiltered results. This guide highlights how to effectively use the search syntax as well.

 

Hope that helps!

Developer Support @ Shopify
- Was this reply helpful? Click Like to let us know!
- Was your question answered? Mark it as an Accepted Solution
- To learn more visit Shopify.dev or the Shopify Web Design and Development Blog

pgj99
Shopify Partner
5 0 0

OK Thanks.  Guess, I can't use that one then.  How odd it works when it has a space though 😄 I assume that maybe one that's being worked on for future.