← back to Qty Bulk 2026 04 23
test_small.js
58 lines
#!/usr/bin/env node
/**
* Smoke test: run the updater logic against a tiny filter (sku:DWJS-12006*)
* to verify metafieldsSet works end-to-end before hitting 8k products.
*/
process.env.SMOKE = '1';
const https = require('https');
const STORE = 'designer-laboratory-sandbox.myshopify.com';
const TOKEN = (process.env.SHOPIFY_ADMIN_TOKEN || '');
function gql(body) {
return new Promise((resolve, reject) => {
const data = JSON.stringify(body);
const req = https.request({
hostname: STORE, path: '/admin/api/2024-10/graphql.json', method: 'POST',
headers: { 'X-Shopify-Access-Token': TOKEN, 'Content-Type': 'application/json', 'Content-Length': Buffer.byteLength(data) },
}, res => { let c=''; res.on('data', d=>c+=d); res.on('end', ()=>{ try { resolve(JSON.parse(c)); } catch { resolve({ error: c.slice(0,300) }); } }); });
req.on('error', reject);
req.setTimeout(30000, ()=>{ req.destroy(); reject(new Error('timeout')); });
req.write(data); req.end();
});
}
(async () => {
// grab 2 DWJS-12006 products with variants
const r = await gql({ query: `{
products(first: 2, query: "sku:DWJS-12006*") {
edges { node { id vendor variants(first: 10) { edges { node { id sku title } } } } }
}
}`});
const products = r?.data?.products?.edges?.map(e => e.node) || [];
console.log(JSON.stringify(products, null, 2));
const mfs = [];
for (const p of products) {
mfs.push({ ownerId: p.id, namespace: 'global', key: 'v_prods_quantity_order_min', type: 'single_line_text_field', value: '2' });
mfs.push({ ownerId: p.id, namespace: 'global', key: 'v_prods_quantity_order_units', type: 'single_line_text_field', value: '2' });
for (const ve of p.variants.edges) {
const v = ve.node;
const isSample = (v.sku||'').toLowerCase().includes('sample') || (v.title||'').toLowerCase().includes('sample');
mfs.push({ ownerId: v.id, namespace: 'custom', key: 'v_prod_quantity_order_min', type: 'single_line_text_field', value: isSample ? '1' : '2' });
mfs.push({ ownerId: v.id, namespace: 'custom', key: 'v_prods_quantity_order_units', type: 'single_line_text_field', value: isSample ? '1' : '2' });
}
}
console.log('mf count:', mfs.length);
const out = await gql({
query: `mutation SetMF($metafields: [MetafieldsSetInput!]!) {
metafieldsSet(metafields: $metafields) {
metafields { id key namespace ownerType }
userErrors { field message code }
}
}`,
variables: { metafields: mfs },
});
console.log(JSON.stringify(out, null, 2));
})();