GraphQL Variables with Queries and Mutations

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

Notes

1query getCartById($id:ID!) {
2 cart(id: $id) {
3 totalUniqueItems
4 items {
5 name
6 unitTotal {
7 amount
8 formatted
9 currency {
10 code
11 }
12 }
13 }
14 }
15}
16
1mutation AddToCartWithoutVariables {
2 addItem(input: {
3 cartId: "some-unique-id",
4 id: "a-unique-id",
5 name: "A product name",
6 price: 1000
7 }) {
8 id
9 items {
10 name
11 unitTotal {
12 formatted
13 }
14 }
15 }
16}
17
1mutation AddToCartWithSpecificVariables($cartId: ID!, $id: ID!, $name: String!, $price: Int!) {
2 addItem(input: {
3 cartId: $cartId,
4 id: $id,
5 name: $name,
6 price: $price
7 }) {
8 id
9 items {
10 name
11 unitTotal {
12 formatted
13 }
14 }
15 }
16}
17
1mutation AddToCartWithType($input: AddToCartInput!) {
2 addItem(input: $input) {
3 id
4 items {
5 name
6 unitTotal {
7 formatted
8 }
9 }
10 }
11}
12