돌연변이

GraphQL 및 Adobe Commerce 시리즈 3부입니다. 돌연변이는 GraphQL을 사용하여 값을 저장, 업데이트 및 반환하는 기능입니다.

이 시리즈의 GraphQL 관련 비디오 및 튜토리얼

예시 돌연변이

모든 전체 API 사양은 데이터를 쿼리할 뿐만 아니라 만들고 업데이트하는 기능도 제공해야 합니다.

REST는 데이터를 변경하는 요청과 요청 유형 또는 "동사"가 없는 요청(GET과 POST 또는 PUT)을 구별합니다.
GraphQL을 사용하는 경우 데이터 수정 쿼리는 다른 키워드에 해당하는 mutation 키워드로 구분됩니다
서버에 정의된 스키마의 루트 유형입니다.

사용자의 장바구니에 제품을 추가하기 위한 이 예제 돌연변이를 살펴보십시오. (생성된 장바구니 ID가 필요합니다.
(로그인한 고객의 세션에 사용하거나 createEmptyCart 돌연변이를 사용하는 경우)

mutation doAddToCart(
    $cartId: String!,
    $cartItems: [CartItemInput!]!
) {
    addProductsToCart(
        cartId: $cartId
        cartItems: $cartItems
    ) {
        cart {
            total_quantity
            prices {
                grand_total {
                    value
                }
            }
        }
    }
}

위의 돌연변이가 다음 변수 사전과 함께 요청으로 전송되는 것을 상상할 수 있습니다.

{
  "cartId": "{cart-id}",
  "cartItems": [
    {
      "quantity": 1,
      "sku": "VT01-RN-XS"
    }
  ]
}

그리고 마지막으로 다음과 같은 응답을 받을 수 있습니다.

{
  "data": {
    "addProductsToCart": {
      "cart": {
        "total_quantity": 1,
        "prices": {
          "grand_total": {
            "value": 35.2
          }
        }
      }
    }
  }
}

위의 예에서 주의해야 할 주요 사항은 query 대신 mutation 키워드를 사용하는 것과 별도로,
the syntax is identical to a query. Like queries, the mutation includes:

  • An arbitrary operation name (doAddToCart)
  • A list of variables (for example, $cartId)
  • An initial field (addProductsToCart) with arguments (for example, cartId, set to the value of $cartId) in parentheses
  • A subselection of fields in braces

The fields subselection allows you to flexibly define the fields you would like returned (from the type assigned as the
return value of addProductsToCart - AddProductsToCartOutput) after the mutation is completed.

As explained previously, fields defined in a GraphQL schema start on a root type for queries (typically referred to as a Query). Similarly,
another root type exists for mutations (typically referred to as Mutation). addProductsToCart is a field
on that root type.

A few other notes about the above example:

  • The ! character suffixing String and CartItemInput indicates that the variable is required.
  • The square brackets ([]) around the CartItemInput type specified for $cartItems indicate a list
    of that type rather than a single value.

유용한 GraphQL 리소스

recommendation-more-help
commerce-learn-help-home