-
Notifications
You must be signed in to change notification settings - Fork 263
/
Copy pathproduct-resource.js
144 lines (135 loc) · 5.25 KB
/
product-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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import Resource from './resource';
import defaultResolver from './default-resolver';
import {paginateProductConnectionsAndResolve} from './paginators';
import productHelpers from './product-helpers';
// GraphQL
import productNodeQuery from './graphql/productNodeQuery.graphql';
import productNodesQuery from './graphql/productNodesQuery.graphql';
import productConnectionQuery from './graphql/productConnectionQuery.graphql';
import productByHandleQuery from './graphql/productByHandleQuery.graphql';
import productRecommendationsQuery from './graphql/productRecommendations.graphql';
/**
* The JS Buy SDK product resource
* @class
*/
class ProductResource extends Resource {
get helpers() {
return productHelpers;
}
/**
* Fetches all products on the shop.
*
* @example
* client.product.fetchAll().then((products) => {
* // Do something with the products
* });
*
* @param {Int} [pageSize] The number of products to fetch per page
* @return {Promise|GraphModel[]} A promise resolving with an array of `GraphModel`s of the products.
*/
fetchAll(first = 20) {
return this.graphQLClient
.send(productConnectionQuery, {first})
.then(defaultResolver('products'))
.then(paginateProductConnectionsAndResolve(this.graphQLClient));
}
/**
* Fetches a single product by ID on the shop.
*
* @example
* client.product.fetch('Xk9lM2JkNzFmNzIQ4NTIY4ZDFi9DaGVja291dC9lM2JkN==').then((product) => {
* // Do something with the product
* });
*
* @param {String} id The id of the product to fetch.
* @return {Promise|GraphModel} A promise resolving with a `GraphModel` of the product.
*/
fetch(id) {
return this.graphQLClient
.send(productNodeQuery, {id})
.then(defaultResolver('node'))
.then(paginateProductConnectionsAndResolve(this.graphQLClient));
}
/**
* Fetches multiple products by ID on the shop.
*
* @example
* const ids = ['Xk9lM2JkNzFmNzIQ4NTIY4ZDFi9DaGVja291dC9lM2JkN==', 'Z2lkOi8vc2hvcGlmeS9Qcm9kdWN0Lzc4NTc5ODkzODQ='];
* client.product.fetchMultiple(ids).then((products) => {
* // Do something with the products
* });
*
* @param {String[]} ids The ids of the products to fetch
* @return {Promise|GraphModel[]} A promise resolving with a `GraphModel` of the product.
*/
fetchMultiple(ids) {
return this.graphQLClient
.send(productNodesQuery, {ids})
.then(defaultResolver('nodes'))
.then(paginateProductConnectionsAndResolve(this.graphQLClient));
}
/**
* Fetches a single product by handle on the shop.
*
* @example
* client.product.fetchByHandle('my-product').then((product) => {
* // Do something with the product
* });
*
* @param {String} handle The handle of the product to fetch.
* @return {Promise|GraphModel} A promise resolving with a `GraphModel` of the product.
*/
fetchByHandle(handle) {
return this.graphQLClient
.send(productByHandleQuery, {handle})
.then(defaultResolver('productByHandle'))
.then(paginateProductConnectionsAndResolve(this.graphQLClient));
}
/**
* Fetches all products on the shop that match the query.
*
* @example
* client.product.fetchQuery({first: 20, sortKey: 'CREATED_AT', reverse: true}).then((products) => {
* // Do something with the first 10 products 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/productsortkeys|Product Sort Keys}.
* @param {String} [args.query] A query string. See full documentation {@link https://help.shopify.com/api/storefront-api/reference/object/shop#products|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 products.
*/
fetchQuery({first = 20, sortKey = 'ID', query, reverse} = {}) {
return this.graphQLClient
.send(productConnectionQuery, {
first,
sortKey,
query,
reverse
})
.then(defaultResolver('products'))
.then(paginateProductConnectionsAndResolve(this.graphQLClient));
}
/**
* Find recommended products related to a given productId.
* To learn more about how recommendations are generated, see https://shopify.dev/themes/product-merchandising/recommendations.
*
* @example
* const productId 'Xk9lM2JkNzFmNzIQ4NTIY4ZDFi9DaGVja291dC9lM2JkN==';
* client.product.fetchProductRecommendations(productId).then((products) => {
* // Do something with the products
* });
*
* @param {String} productId The id of the product to fetch.
* @return {Promise|GraphModel[]} A promise resolving with an array of `GraphModel`s of the products.
*/
fetchRecommendations(productId) {
return this.graphQLClient
.send(productRecommendationsQuery, {productId})
.then(defaultResolver('productRecommendations'))
.then(paginateProductConnectionsAndResolve(this.graphQLClient));
}
}
export default ProductResource;