-
Notifications
You must be signed in to change notification settings - Fork 6
/
gatsby-node.js
228 lines (220 loc) · 4.73 KB
/
gatsby-node.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
const { createRemoteFileNode } = require(`gatsby-source-filesystem`)
exports.createResolvers = ({
actions,
cache,
createNodeId,
createResolvers,
store,
reporter,
}) => {
const { createNode } = actions
createResolvers({
Vendure_Asset: {
imageFile: {
type: `File`,
resolve(source, args, context, info) {
const url = new URL(source.preview).href;
return createRemoteFileNode({
url,
store,
cache,
createNode,
createNodeId,
reporter,
})
},
},
},
Vendure_SearchResultAsset: {
imageFile: {
type: `File`,
resolve(source, args, context, info) {
return createRemoteFileNode({
url: source.preview,
store,
cache,
createNode,
createNodeId,
reporter,
})
},
},
},
})
}
exports.createPages = async ({ graphql, actions }) => {
const { createPage } = actions
/**
* Create collection pages
*/
const getCollections = await graphql(GET_COLLECTIONS)
const collections = getCollections.data.vendure.collections.items
for (const collection of collections) {
const searchByCollection = await graphql(SEARCH_BY_COLLECTION, {
input: {
collectionSlug: collection.slug,
groupByProduct: true,
},
})
const products = searchByCollection.data.vendure.search.items
const childCollections = collections.filter(
c => c.parent.id === collection.id
)
createPage({
path: `/collection/${collection.slug}`,
component: require.resolve("./src/templates/collection.tsx"),
context: {
collection,
products,
childCollections,
},
defer: false,
})
}
/**
* Create product pages
*/
let skip
let totalItems
const take = 100
const productIds = []
do {
skip = skip == null ? 0 : skip + take
const getProductIds = await graphql(GET_PRODUCT_IDS, {
input: { skip, take },
})
totalItems = getProductIds.data.vendure.search.totalItems
productIds.push(
...getProductIds.data.vendure.search.items.map(i => i.productId)
)
} while (skip + take < totalItems)
for (const id of productIds) {
const getProduct = await graphql(GET_PRODUCT, { id })
const product = getProduct.data.vendure.product
createPage({
path: `/product/${product.slug}`,
component: require.resolve("./src/templates/product.tsx"),
context: {
product,
},
defer: false,
})
}
}
const GET_COLLECTIONS = /* GraphQL */ `
query {
vendure {
collections {
items {
id
name
description
slug
breadcrumbs {
id
name
slug
}
featuredAsset {
id
preview
imageFile {
childImageSharp {
gatsbyImageData(width: 300, height: 300, quality: 85)
}
}
}
parent {
id
name
}
}
}
}
}
`
const SEARCH_BY_COLLECTION = /* GraphQL */ `
query Search($input: Vendure_SearchInput!) {
vendure {
search(input: $input) {
items {
slug
productName
productAsset {
preview
imageFile {
childImageSharp {
gatsbyImageData(width: 300, height: 300, quality: 70)
}
}
}
currencyCode
priceWithTax {
... on Vendure_PriceRange {
min
max
}
}
}
}
}
}
`
const GET_PRODUCT_IDS = /* GraphQL */ `
query GetProductIds($input: Vendure_SearchInput!) {
vendure {
search(input: $input) {
totalItems
items {
productId
}
}
}
}
`
const GET_PRODUCT = /* GraphQL */ `
query GetProduct($id: ID!) {
vendure {
product(id: $id) {
id
slug
name
collections {
id
name
slug
breadcrumbs {
id
name
slug
}
}
description
featuredAsset {
id
preview
imageFile {
childImageSharp {
gatsbyImageData(width: 800, quality: 85)
}
}
}
assets {
id
preview
imageFile {
childImageSharp {
gatsbyImageData(width: 800, quality: 85)
}
}
}
variants {
id
name
priceWithTax
currencyCode
}
}
}
}
`