Mutations

Here are some examples of making GraphQL mutation calls in Shypyard.

Example: Update product variant

const newSKU = 'some-new-sku';
await shopify.graphql(`
  mutation {
    productVariantUpdate(
      input: {
        id: "${variant.admin_graphql_api_id}"
        sku: "${newSKU}"
      }
    ) {
      productVariant {
        sku
      }
      userErrors {
        field
        message
      }
    }
  }
`);
}
// print updated data
console.log(JSON.stringify(resp, null, 2));

Example: Adding tags


const tagsToAdd = ['tag1', 'tag2', 'tag3'];
const resp = await shopify.graphql(`
  mutation {
    tagsAdd(
      id: "${order.customer.admin_graphql_api_id}"
      tags: "${tagsToAdd}"
    ) {
      node {
        ... on Order {
          id
          name
          tags
        }
      }
      userErrors {
        field
        message
      }
    }
  }
`);
// print updated tags
console.log(JSON.stringify(resp, null, 2));

Example: Update order note

const resp = await shopify.graphql(`
  mutation {
    orderUpdate(
      input: {
        id: "${order.admin_graphql_api_id}"
        note: "${note}"
      }
    ) {
      userErrors {
        field
        message
      }
    }
  }
`);

// print updated tags
console.log(JSON.stringify(resp, null, 2));
  • Bulk update location inventory (see doc)

const update_mutations = [
  {inventoryItemID: 123, quantity: 10},
  {inventoryItemID: 345, quantity: 5},
];

const mutationQuery = `
mutation {  
  inventoryBulkAdjustQuantityAtLocation(
    locationId: "${gql_location}",    
    inventoryItemAdjustments: [
      ${update_mutations
        .map(
          (x) =>
            `{inventoryItemId: "${x.inventoryItemID}", availableDelta: ${x.quantity}}`,
        )
        .join(',')}
    ]
  ) {
    inventoryLevels {      
      id
      available
    }
    userErrors{
      field
      message
    }
  }
}`;

try {
  let response = await shopify.graphql(mutationQuery);
  console.log(JSON.stringify(response, null, 2));
} catch (error) {
  console.log(error.message);
  throw error;
}

Last updated