GraphQL Variables with Queries and Mutations

Learn how to use GraphQL variables with your queries and mutations.

Notes

query getCartById($id:ID!) {
cart(id: $id) {
totalUniqueItems
items {
name
unitTotal {
amount
formatted
currency {
code
}
}
}
}
}
mutation AddToCartWithoutVariables {
addItem(input: {
cartId: "some-unique-id",
id: "a-unique-id",
name: "A product name",
price: 1000
}) {
id
items {
name
unitTotal {
formatted
}
}
}
}
mutation AddToCartWithSpecificVariables($cartId: ID!, $id: ID!, $name: String!, $price: Int!) {
addItem(input: {
cartId: $cartId,
id: $id,
name: $name,
price: $price
}) {
id
items {
name
unitTotal {
formatted
}
}
}
}
mutation AddToCartWithType($input: AddToCartInput!) {
addItem(input: $input) {
id
items {
name
unitTotal {
formatted
}
}
}
}
Jamie Barton

Published on 20 Sept 2021 by Jamie Barton