const SALEOR_API_URL = 'https://api.manoonoils.com/graphql/'; async function saleorFetch(query, variables = {}) { const response = await fetch(SALEOR_API_URL, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ query, variables }), }); const result = await response.json(); if (result.errors) { console.error('GraphQL Errors:', JSON.stringify(result.errors, null, 2)); throw new Error(JSON.stringify(result.errors)); } return result.data; } async function test() { // Create checkout const createResult = await saleorFetch(` mutation { checkoutCreate(input: { channel: "default-channel" email: "test@test.com" lines: [{ variantId: "UHJvZHVjdFZhcmlhbnQ6Mjk0", quantity: 1 }] languageCode: SR }) { checkout { id token totalPrice { gross { amount } } subtotalPrice { gross { amount } } } errors { field message code } } } `); if (createResult.checkoutCreate.errors?.length > 0) { console.error('Checkout creation errors:', createResult.checkoutCreate.errors); throw new Error('Checkout creation failed'); } if (!createResult.checkoutCreate.checkout) { console.error('Create result:', createResult); throw new Error('Checkout creation returned null'); } const checkout = createResult.checkoutCreate.checkout; const token = checkout.token; console.log('Created checkout:'); console.log(' ID:', checkout.id); console.log(' Token:', token); console.log(' Initial Total:', checkout.totalPrice.gross.amount); // Set address await saleorFetch(` mutation { checkoutShippingAddressUpdate( checkoutId: "${checkout.id}" shippingAddress: { firstName: "Test" lastName: "User" streetAddress1: "123 Street" city: "Belgrade" postalCode: "11000" country: "RS" phone: "+38160123456" } ) { checkout { shippingMethods { id name price { amount } } } } } `); // Query by token (what refreshCheckout does) const tokenQuery = await saleorFetch(` query { checkout(token: "${token}") { id token totalPrice { gross { amount } } subtotalPrice { gross { amount } } shippingPrice { gross { amount } } shippingMethods { id name price { amount } } } } `); console.log('\nQuery by token (before shipping method):'); console.log(' Total:', tokenQuery.checkout.totalPrice.gross.amount); console.log(' Subtotal:', tokenQuery.checkout.subtotalPrice.gross.amount); console.log(' Shipping:', tokenQuery.checkout.shippingPrice.gross.amount); console.log(' Methods:', tokenQuery.checkout.shippingMethods.length); if (tokenQuery.checkout.shippingMethods.length > 0) { const methodId = tokenQuery.checkout.shippingMethods[0].id; // Set shipping method await saleorFetch(` mutation { checkoutShippingMethodUpdate( checkoutId: "${checkout.id}" shippingMethodId: "${methodId}" ) { checkout { totalPrice { gross { amount } } subtotalPrice { gross { amount } } shippingPrice { gross { amount } } } } } `); // Query by token again (what should happen after refreshCheckout) const afterMethod = await saleorFetch(` query { checkout(token: "${token}") { totalPrice { gross { amount } } subtotalPrice { gross { amount } } shippingPrice { gross { amount } } } } `); console.log('\nQuery by token (AFTER shipping method):'); console.log(' Total:', afterMethod.checkout.totalPrice.gross.amount); console.log(' Subtotal:', afterMethod.checkout.subtotalPrice.gross.amount); console.log(' Shipping:', afterMethod.checkout.shippingPrice.gross.amount); } } test().catch(console.error);