GraphQL Variables with Queries and Mutations

20 Sept 2021

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
      }
    }
  }
}