Cart Mutations
Create Cart
mutation {
createEmptyCart
}
# Returns: cart_id string
Add Simple Product to Cart
mutation {
addSimpleProductsToCart(
input: {
cartId: "cart123"
cartItems: [
{
data: {
quantity: 1
sku: "SKU-123"
}
}
]
}
) {
cart {
id
total_quantity
items {
product {
name
sku
}
quantity
}
}
}
}
Add Configurable Product
mutation {
addConfigurableProductsToCart(
input: {
cartId: "cart123"
cartItems: [
{
data: {
quantity: 1
sku: "SKU-CONFIG"
}
selectable_options: [
{
option_id: "93"
option_value: "53"
}
]
}
]
}
) {
cart {
items {
product { name sku }
quantity
}
}
}
}
Update Cart Item
mutation {
updateCartItems(
input: {
cartId: "cart123"
cartItems: [
{
cart_item_id: 1
quantity: 2
}
]
}
) {
cart {
items {
product { name }
quantity
}
}
}
}
Remove from Cart
mutation {
removeItemFromCart(
input: {
cartId: "cart123"
cartItemId: 1
}
) {
cart {
items {
product { name }
}
}
}
}
Order Mutations
Set Shipping Address
mutation {
setShippingAddressesOnCart(
input: {
cartId: "cart123"
shippingAddresses: [
{
address: {
firstname: "John"
lastname: "Doe"
street: ["123 Main St"]
city: "New York"
region: "NY"
postcode: "10001"
country_code: "US"
telephone: "555-1234"
}
}
]
}
) {
cart {
shipping_addresses {
firstname
lastname
}
}
}
}
Set Shipping Method
mutation {
setShippingMethodsOnCart(
input: {
cartId: "cart123"
shippingMethods: [
{
carrier_code: "flatrate"
method_code: "flatrate"
}
]
}
) {
cart {
shipping_addresses {
selected_shipping_method {
carrier_code
method_code
}
}
}
}
}
Set Payment Method
mutation {
setPaymentMethodOnCart(
input: {
cartId: "cart123"
payment_method: {
code: "checkmo"
}
}
) {
cart {
available_payment_methods {
code
title
}
selected_payment_method {
code
}
}
}
}
Place Order
mutation {
placeOrder(
input: {
cartId: "cart123"
}
) {
order {
order_number
order_id
}
}
}
Customer Mutations
Create Customer
mutation {
createCustomer(
input: {
email: "customer@example.com"
firstname: "John"
lastname: "Doe"
password: "password123"
}
) {
customer {
id
email
firstname
lastname
}
}
}
Update Customer
mutation {
updateCustomer(
input: {
firstname: "Jane"
lastname: "Smith"
}
) {
customer {
id
email
firstname
lastname
}
}
}
Change Password
mutation {
changeCustomerPassword(
currentPassword: "oldpassword"
newPassword: "newpassword"
) {
customer {
email
}
}
}
Create Customer Address
mutation {
createCustomerAddress(
input: {
firstname: "John"
lastname: "Doe"
street: ["123 Main St"]
city: "New York"
region: {
region_code: "NY"
region: "New York"
}
postcode: "10001"
country_code: "US"
telephone: "555-1234"
default_shipping: true
default_billing: true
}
) {
id
firstname
lastname
street
city
}
}
Custom Mutations
Create Custom Mutation Interface
<?php
namespace Vendor\Module\GraphQl\Resolver\Mutation;
use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
use Magento\Framework\GraphQl\Resolver\ResolverInterface;
class CreateItem implements ResolverInterface
{
public function __construct(
private \Vendor\Module\Api\ItemRepositoryInterface $itemRepository,
private \Vendor\Module\Api\Data\ItemDataInterfaceFactory $itemFactory,
) {
}
public function resolve(
Field $field,
$context,
ResolveInfo $info,
array $value = null,
array $args = null
) {
$input = $args['input'];
$item = $this->itemFactory->create();
$item->setName($input['name']);
$item->setEmail($input['email']);
$this->itemRepository->save($item);
return [
'id' => $item->getId(),
'name' => $item->getName(),
'email' => $item->getEmail()
];
}
}
Schema Definition
# schema.graphqls
type Mutation {
createItem(input: CreateItemInput!): ItemOutput
}
input CreateItemInput {
name: String!
email: String!
}
type ItemOutput {
id: ID!
name: String!
email: String!
}
Use Custom Mutation
mutation {
createItem(
input: {
name: "Test Item"
email: "test@example.com"
}
) {
id
name
email
}
}
Error Handling
public function resolve(...)
{
try {
$item = $this->itemRepository->save($item);
return ['id' => $item->getId()];
} catch (\Exception $e) {
throw new \GraphQL\Error\GraphQLError(
$e->getMessage(),
null,
null,
null,
['input' => $args['input']]
);
}
}
Quiz
1. How do you create a cart in GraphQL?
2. How do you add a product to cart?
3. How do you place an order?
Flashcards
Question
How do you create a cart?
Click to reveal answer
Answer
createEmptyCart mutation
Question
How do you add simple products?
Click to reveal answer
Answer
addSimpleProductsToCart mutation
Question
How do you place an order?
Click to reveal answer
Answer
placeOrder(cartId: "...")
Question
How do you update customer info?
Click to reveal answer
Answer
updateCustomer mutation
Question
How do you create custom mutations?
Click to reveal answer
Answer
Define in schema.graphqls and implement resolver
Revision Notes
Key Takeaways
- 1. Mutations modify data (create, update, delete)
- 2. Cart flow: create → add items → set shipping → set payment → place order
- 3. Customer mutations require authentication
- 4. Custom mutations need schema definition and resolver
- 5. Always handle errors gracefully
Interview Tips
- • Know the cart and order mutation flow
- • Understand customer mutation patterns
- • Be ready to create custom mutations
- • Discuss error handling in mutations
Cheat Sheet
Cart:
createEmptyCart → returns cart_id
addSimpleProductsToCart(cartId, cartItems)
setShippingAddressesOnCart
setPaymentMethodOnCart
placeOrder(cartId)
Customer:
createCustomer(input)
updateCustomer(input)
changePassword(current, new)