From ab33b2a32134f8827e718bd16d059019485e8166 Mon Sep 17 00:00:00 2001 From: Stefan Slehta Date: Tue, 15 Oct 2019 21:52:38 -0400 Subject: [PATCH] Add an optional productsFirst param to fetchWithProducts --- README.md | 3 ++- src/collection-resource.js | 4 ++-- .../collectionNodeWithProductsQuery.graphql | 17 +++++++++++++++-- 3 files changed, 19 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 3f7baf22f..9dfc121ec 100644 --- a/README.md +++ b/README.md @@ -140,8 +140,9 @@ client.collection.fetchAllWithProducts().then((collections) => { // Fetch a single collection by ID, including its products const collectionId = 'Z2lkOi8vc2hvcGlmeS9Db2xsZWN0aW9uLzM2OTMxMjU4NA=='; +// Set a parameter for first x products, defaults to 20 if you don't provide a param -client.collection.fetchWithProducts(collectionId).then((collection) => { +client.collection.fetchWithProducts(collectionId, {productsFirst: 10}).then((collection) => { // Do something with the collection console.log(collection); console.log(collection.products); diff --git a/src/collection-resource.js b/src/collection-resource.js index 90c92e7de..d0a64217d 100644 --- a/src/collection-resource.js +++ b/src/collection-resource.js @@ -78,9 +78,9 @@ class CollectionResource extends Resource { * @param {String} id The id of the collection to fetch. * @return {Promise|GraphModel} A promise resolving with a `GraphModel` of the collection. */ - fetchWithProducts(id) { + fetchWithProducts(id, {productsFirst = 20} = {}) { return this.graphQLClient - .send(collectionNodeWithProductsQuery, {id}) + .send(collectionNodeWithProductsQuery, {id, productsFirst}) .then(defaultResolver('node')) .then(paginateCollectionsProductConnectionsAndResolve(this.graphQLClient)); } diff --git a/src/graphql/collectionNodeWithProductsQuery.graphql b/src/graphql/collectionNodeWithProductsQuery.graphql index 9da0f6283..d59dde9d9 100644 --- a/src/graphql/collectionNodeWithProductsQuery.graphql +++ b/src/graphql/collectionNodeWithProductsQuery.graphql @@ -1,6 +1,19 @@ -query($id: ID!) { +query($id: ID!, $productsFirst: Int!) { node(id: $id) { ...CollectionFragment - ...CollectionsProductsFragment + ... on Collection { + products(first: $productsFirst) { + pageInfo { + hasNextPage + hasPreviousPage + } + edges { + cursor + node { + ...ProductFragment + } + } + } + } } }