2. GraphQL 스키마 및 카트 쿼리 설정
Shopify Storefront API는 GraphQL 기반입니다. 결제를 연동하기 위해 상품의 Variant ID를 조회하는 쿼리와 카트를 생성하는 뮤테이션을 선언해야 합니다.
2.1. 상품 목록 및 Variant ID 조회 쿼리
단품 구매를 위해 판매 가능한 상품의 옵션 ID(Variant ID)를 획득하기 위한 쿼리입니다.
const PRODUCT_LIST_QUERY = `
query ProductListForOnePageCheckout($first: Int = 20) {
products(first: $first, sortKey: TITLE) {
edges {
node {
id
title
variants(first: 10) {
edges {
node {
id
title
availableForSale
price {
amount
currencyCode
}
}
}
}
}
}
}
}
`;
2.2. 카트 생성 뮤테이션 (cartCreate)
구매자 및 배송지 주소를 포함하여 결제용 장바구니를 생성하고 checkoutUrl을 반환받는 뮤테이션입니다.
const CREATE_CART_MUTATION = `
mutation CreateCartWithBuyerAndAddress(
$lines: [CartLineInput!]
$email: String
$phone: String
$countryCode: CountryCode!
$firstName: String
$lastName: String
$address1: String
$address2: String
$city: String
$provinceCode: String
$zip: String
) {
cartCreate(
input: {
lines: $lines
buyerIdentity: {
email: $email
phone: $phone
countryCode: $countryCode
}
delivery: {
addresses: [
{
address: {
deliveryAddress: {
firstName: $firstName
lastName: $lastName
address1: $address1
address2: $address2
city: $city
provinceCode: $provinceCode
countryCode: $countryCode
zip: $zip
phone: $phone
}
}
selected: true
}
]
}
}
) {
cart {
id
checkoutUrl
}
userErrors {
field
message
}
}
}
`;