-
Notifications
You must be signed in to change notification settings - Fork 263
/
Copy pathcollection-resource.js
130 lines (122 loc) · 5.03 KB
/
collection-resource.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import Resource from './resource';
import defaultResolver from './default-resolver';
import {paginateCollectionsProductConnectionsAndResolve} from './paginators';
// GraphQL
import collectionNodeQuery from './graphql/collectionNodeQuery.graphql';
import collectionNodeWithProductsQuery from './graphql/collectionNodeWithProductsQuery.graphql';
import collectionConnectionQuery from './graphql/collectionConnectionQuery.graphql';
import collectionConnectionWithProductsQuery from './graphql/collectionConnectionWithProductsQuery.graphql';
import collectionByHandleQuery from './graphql/collectionByHandleQuery.graphql';
/**
* The JS Buy SDK collection resource
* @class
*/
class CollectionResource extends Resource {
/**
* Fetches all collections on the shop, not including products.
* To fetch collections with products use [fetchAllsWithProducts]{@link Client#fetchAllsWithProducts}.
*
* @example
* client.collection.fetchAll().then((collections) => {
* // Do something with the collections
* });
*
* @return {Promise|GraphModel[]} A promise resolving with an array of `GraphModel`s of the collections.
*/
fetchAll(first = 20) {
return this.graphQLClient
.send(collectionConnectionQuery, {first})
.then(defaultResolver('collections'));
}
/**
* Fetches all collections on the shop, including products.
*
* @example
* client.collection.fetchAllWithProducts().then((collections) => {
* // Do something with the collections
* });
*
* @return {Promise|GraphModel[]} A promise resolving with an array of `GraphModel`s of the collections.
*/
fetchAllWithProducts({first = 20, productsFirst = 20} = {}) {
return this.graphQLClient
.send(collectionConnectionWithProductsQuery, {first, productsFirst})
.then(defaultResolver('collections'))
.then(paginateCollectionsProductConnectionsAndResolve(this.graphQLClient));
}
/**
* Fetches a single collection by ID on the shop, not including products.
* To fetch the collection with products use [fetchWithProducts]{@link Client#fetchWithProducts}.
*
* @example
* client.collection.fetch('Xk9lM2JkNzFmNzIQ4NTIY4ZDFiZTUyZTUwNTE2MDNhZjg==').then((collection) => {
* // Do something with the collection
* });
*
* @param {String} id The id of the collection to fetch.
* @return {Promise|GraphModel} A promise resolving with a `GraphModel` of the collection.
*/
fetch(id) {
return this.graphQLClient
.send(collectionNodeQuery, {id})
.then(defaultResolver('node'));
}
/**
* Fetches a single collection by ID on the shop, including products.
*
* @example
* client.collection.fetchWithProducts('Xk9lM2JkNzFmNzIQ4NTIY4ZDFiZTUyZTUwNTE2MDNhZjg==').then((collection) => {
* // Do something with the collection
* });
*
* @param {String} id The id of the collection to fetch.
* @return {Promise|GraphModel} A promise resolving with a `GraphModel` of the collection.
*/
fetchWithProducts(id, {productsFirst = 20} = {}) {
return this.graphQLClient
.send(collectionNodeWithProductsQuery, {id, productsFirst})
.then(defaultResolver('node'))
.then(paginateCollectionsProductConnectionsAndResolve(this.graphQLClient));
}
/**
* Fetches a collection by handle on the shop.
*
* @example
* client.collection.fetchByHandle('my-collection').then((collection) => {
* // Do something with the collection
* });
*
* @param {String} handle The handle of the collection to fetch.
* @return {Promise|GraphModel} A promise resolving with a `GraphModel` of the collection.
*/
fetchByHandle(handle) {
return this.graphQLClient
.send(collectionByHandleQuery, {handle})
.then(defaultResolver('collectionByHandle'));
}
/**
* Fetches all collections on the shop that match the query.
*
* @example
* client.collection.fetchQuery({first: 20, sortKey: 'CREATED_AT', reverse: true}).then((collections) => {
* // Do something with the first 10 collections sorted by title in ascending order
* });
*
* @param {Object} [args] An object specifying the query data containing zero or more of:
* @param {Int} [args.first=20] The relay `first` param. This specifies page size.
* @param {String} [args.sortKey=ID] The key to sort results by. Available values are
* documented as {@link https://help.shopify.com/api/storefront-api/reference/enum/collectionsortkeys|Collection Sort Keys}.
* @param {String} [args.query] A query string. See full documentation {@link https://help.shopify.com/api/storefront-api/reference/object/shop#collections|here}
* @param {Boolean} [args.reverse] Whether or not to reverse the sort order of the results
* @return {Promise|GraphModel[]} A promise resolving with an array of `GraphModel`s of the collections.
*/
fetchQuery({first = 20, sortKey = 'ID', query, reverse} = {}) {
return this.graphQLClient.send(collectionConnectionQuery, {
first,
sortKey,
query,
reverse
}).then(defaultResolver('collections'));
}
}
export default CollectionResource;