Shopify / shopify-app-js

MIT License
284 stars 112 forks source link

Provide cursor param to productVendors field in GraphQL queries to simplify "all vendors" fetch #1182

Closed coren-frankel closed 3 months ago

coren-frankel commented 3 months ago

Overview

In my efforts to query all vendors from a store using the admin GraphQL client, I stumbled on someone else's deep dive into attempting the same on the Shopify Community discussion boards.

As the OP's question in that thread points out, there is no way to use the productVendors field in a grapqhl query for all vendors (if there are more than 250) because the first parameter is available, but a cursor parameter is not.

Getting vendors through Shop's productVendors object. Has limit of 250 records though "first" parameter and no way to iterate, thus absolutely useless when we need to get them all.

Code example: https://community.shopify.com/c/shopify-apis-and-sdks/json-list-of-vendors-api/m-p/600737/highlight/...

query productVendors {
  shop {
    productVendors(first: 200) {
      edges {
        node
      }
    }
  }
}

The cursor is already available when querying for productVendors, so I assume the proposed change is low hanging fruit. Here is how the productVendors query maxes out without access to the cursor param:

query getProductVendors {
  shop {
    productVendors(first: 250) {
      edges {
        node
        cursor
      }
    }
  }
}

As I understand it, the alternative (and best) graphql approach is to query all products for vendor names:

query getAllVendors($first: Int, $cursor: String) {
  products( first: $first, after: $cursor, query: "gift_card:false" ) {
    edges {
      node {
        vendor
      },
    cursor
    }
  }
}

Then after iteratively collecting all product edges with only the vendor name on each node, you need to reduce the list with a Set to get unique vendors. In my remix/TS project for example:

const listOfVendors = Array.from(
  allProducts.reduce((acc, curr) => {
    acc.add(curr.node.vendor)
    return acc;
  }, new Set<string>())
);

That all just seems so clunky when the productVendors field is already equipped with cursors.

What's it going to take to make the graphql productVendors field more useful? I would be open to alternative solutions, but I think I'm not alone in seeing this as a small but persistent problem with the graphql API client.

paulomarg commented 3 months ago

Hey, thanks for raising this, and thanks for all the information you provided.

Unfortunately, there's not a lot we'll be able to do about this from this repo, since it only contains a client package for the API.

I will forward this internally to the appropriate team, but we won't be able to track progress here, so I'm going to close this issue. In the future, we recommend that API issues be reported via Partner support.

coren-frankel commented 3 months ago

Understood. Thank you for passing it on!