Last active
August 1, 2022 00:44
-
-
Save Acen/3e1bf5c42c6fce3f5a9b82689899049a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import fs from 'fs'; | |
import axios from 'axios'; | |
import axiosThrottle from 'axios-request-throttle'; | |
axiosThrottle.use(axios, {requestsPerSecond: 2}); | |
const domain = 'https://blah:blah@store.myshopify.com'; | |
const version = '2022-07' | |
const products = []; | |
const rejected = []; | |
const data = (id) => (JSON.stringify({ | |
query: `query { | |
product(id: ${id}) { | |
metafield(namespace: "my_fields", key: "features"){ | |
value | |
id | |
} | |
} | |
}`, | |
variables: {} | |
})); | |
const config = (id) => ({ | |
method: 'post', | |
url: `${domain}/admin/api/${version}/graphql.json`, | |
headers: { | |
'Content-Type': 'application/json' | |
}, | |
data: data(id) | |
}); | |
const productGid = (id) => { | |
return `"gid://shopify/Product/${id}"`; | |
} | |
axios.get(`${domain}/admin/api/${version}/products.json?limit=250`).then((response) => { | |
response.data.products.map((product) => { | |
products.push({id: product.id, title: product.title, handle: product.handle,}); | |
}) | |
return products; | |
}).then((products) => { | |
const features = []; | |
const promise = new Promise((resolve, reject) => { | |
fs.readFile('json/features2.json', 'utf-8', (err, fileContents) => { | |
if (err) throw err; | |
const productFeatures = JSON.parse(fileContents); | |
productFeatures.map((productFeature) => { | |
const found = products.find((item) => { | |
return item.handle === productFeature.slug; | |
}) | |
if (found) { | |
features.push({ | |
gid: productGid(found.id), | |
title: found.title, | |
handle: found.handle, | |
features: productFeature.features, | |
}) | |
} | |
if (productFeatures[productFeatures.length - 1] === productFeature) { | |
console.log('hit end of product features'); | |
resolve(features); | |
} | |
}); | |
}); | |
}) | |
promise.then((features) => { | |
const productFeatures = features.map((updatableProduct) => { | |
return new Promise((resolve, reject) => { | |
axios(config(updatableProduct.gid)) | |
.then(function (response) { | |
return response.data; | |
}) | |
.then((data) => { | |
resolve([data, updatableProduct]); | |
}) | |
.catch(function (error) { | |
reject(error); | |
}); | |
}); | |
}) | |
Promise.all(productFeatures) | |
.then((value) => { | |
console.log(value); | |
const data = (id, value, metafieldID) => (JSON.stringify({ | |
query: `mutation($input: ProductInput!) { | |
productUpdate(input: $input) { | |
product { | |
handle | |
onlineStoreUrl | |
metafields(first: 10){ | |
nodes{ | |
namespace | |
key | |
value | |
} | |
} | |
} | |
} | |
}`, | |
variables: { | |
"input": { | |
"id": id, | |
"metafields": [ | |
{ | |
"type": "list.single_line_text_field", | |
"namespace": "my_fields", | |
"key": "features", | |
"value": JSON.stringify(value), | |
} | |
] | |
} | |
} | |
})); | |
const config = (id, value, metafieldID) => ({ | |
method: 'post', | |
url: `${domain}/admin/api/${version}/graphql.json`, | |
headers: { | |
'Content-Type': 'application/json' | |
}, | |
data: data(id, value, metafieldID) | |
}); | |
const allRequests = value.map((item) => { | |
// const item = value[0]; | |
const [product, newData] = item; | |
const gid = newData.gid.split('"').join(''); | |
// const mfid = product.data.product.metafield.id | |
const mfid = null | |
return new Promise((resolve, reject) => { | |
axios(config(gid, newData.features, mfid)).then((response) => { | |
if (response.data.errors?.length) { | |
rejected.push(newData.handle); | |
reject(response.data); | |
} else { | |
const productUpdate = response.data.data.productUpdate; | |
console.log('success', productUpdate.product.handle, productUpdate.product.onlineStoreUrl, productUpdate.product.metafields.nodes); | |
// console.log('real success', response.data); | |
resolve(response.data); | |
} | |
}).catch((err) => { | |
// console.log('real reject', err); | |
reject(err); | |
}) | |
}) | |
// throw new Error('exit'); | |
}); | |
Promise.all(allRequests).then((requests) => { | |
console.log('all requests have resolve', requests); | |
// console.log(requests); | |
console.log('rejected then', rejected); | |
}).catch((err) => { | |
// console.log('rejected catch', rejected, err); | |
}) | |
// axios(value.id) | |
// .then(function (response) { | |
// console.log(JSON.stringify(response.data)); | |
// }) | |
// .catch(function (error) { | |
// console.log(error); | |
// }); | |
// value.map(( | |
// value | |
// ) => { | |
// console.log(value.data) | |
// }) | |
// console.log('value', value[0][1]); | |
// console.log('count', value.length); | |
// console.log('promise features', value[0]); | |
// console.log('throttle', value[value.length - 1]?.extensions); | |
}) | |
.catch((err) => { | |
console.log('failure on Promise.all(productFeatures)', err); | |
}) | |
}) | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment