-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
gatsby-node.js
209 lines (199 loc) · 4.69 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
const { client } = require("./src/utils/client")
exports.createSchemaCustomization = ({ actions }) => {
const { createTypes } = actions
const typeDefs = `
type MedusaProduct implements Node {
id: ID!
title: String!
subtitle: String
description: String
handle: String!
is_giftcard: Boolean!
status: String!
images: [MedusaImage]
thumbnail: String
options: [MedusaProductOption]!
variants: [MedusaProductVariant]!
collection: MedusaCollection
collection_id: String
profile_id: String!
discountable: Boolean!
published_at: Date!
created_at: Date!
updated_at: Date!
weight: Int
length: Int
width: Int
}
type MedusaCollection @dontInfer {
id: ID!
handle: String!
title: String!
created_at: Date!
updated_at: Date!
}
type MedusaProductOption @dontInfer {
id: ID!
title: String!
product_id: String!
values: [MedusaProductOptionValue]!
created_at: Date!
updated_at: Date!
}
type MedusaProductOptionValue @dontInfer {
id: ID!
value: String!
created_at: Date!
updated_at: Date!
option_id: String!
variant_id: String!
}
type MedusaProductVariant @dontInfer {
id: ID!
title: String!
product_id: String!
prices: [MedusaMoneyAmount]!
sku: String
barcode: String
upc: String
variant_rank: Int
inventory_quantity: Int!
allow_backorder: Boolean!
manage_inventory: Boolean!
hs_code: String
origin_country: String
mid_code: String
material: String
weight: Int
length: Int
height: Int
width: Int
options: [MedusaProductOptionValue]!
created_at: Date!
updated_at: Date!
}
type MedusaMoneyAmount @dontInfer {
id: ID!
amount: Int!
currency_code: String!
created_at: Date!
updated_at: Date!
variant_id: String!
}
type MedusaImage @dontInfer {
id: ID!
url: String!
created_at: Date!
updated_at: Date!
}
type MedusaRegion implements Node {
id: ID!
name: String!
tax_rate: Float!
currency_code: String!
created_at: Date!
updated_at: Date!
countries: [MedusaCountry]!
}
type MedusaCountry @dontInfer {
id: ID!
display_name: String!
iso_2: String!
iso_3: String!
name: String!
num_code: Int!
region_id: String!
}
`
createTypes(typeDefs)
}
exports.sourceNodes = async ({
actions: { createNode },
createContentDigest,
reporter,
}) => {
const products = await client.products
.list()
.then(response => response.products)
.catch(_ => {
reporter.panic(
"Could not fetch products from Medusa, please ensure that you have a Medusa server running and that you have set the correct BASE_URL in the client."
)
return
})
for (const product of products) {
createNode({
...product,
parent: null,
children: [],
internal: {
type: "MedusaProduct",
contentDigest: createContentDigest(product),
},
})
}
const { regions } = await client.regions.list()
for (const region of regions) {
createNode({
...region,
parent: null,
children: [],
internal: {
type: "MedusaRegion",
contentDigest: createContentDigest(region),
},
})
}
}
exports.createPages = async function ({ actions, graphql }) {
const { data } = await graphql(`
query {
allMedusaProduct {
edges {
node {
handle
}
}
}
allMedusaRegion {
edges {
node {
id
countries {
iso_2
}
}
}
}
}
`)
const [first] = data.allMedusaRegion.edges
data.allMedusaProduct.edges.forEach(edge => {
const handle = edge.node.handle
if (handle) {
actions.createPage({
path: handle,
component: require.resolve(`./src/templates/product-page.js`),
context: {
handle,
region_id: first.node.id,
country: first.node.countries[0].iso_2,
},
})
data.allMedusaRegion.edges.forEach(edge => {
const { id: regionId, countries } = edge.node
for (const { iso_2 } of countries) {
actions.createPage({
path: `/${iso_2}/${handle}`,
component: require.resolve(`./src/templates/product-page.js`),
context: {
handle,
region_id: regionId,
country: iso_2,
},
})
}
})
}
})
}