Skip to content

Commit

Permalink
Add an optional productsFirst param to fetchWithProducts
Browse files Browse the repository at this point in the history
  • Loading branch information
StefanSlehta committed Nov 5, 2019
1 parent e334cc9 commit ab33b2a
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 5 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions src/collection-resource.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand Down
17 changes: 15 additions & 2 deletions src/graphql/collectionNodeWithProductsQuery.graphql
Original file line number Diff line number Diff line change
@@ -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
}
}
}
}
}
}

0 comments on commit ab33b2a

Please sign in to comment.