🧑‍🏫

【Shopify.dev和訳】API Examples/Customers

2021/09/18に公開

この記事について

この記事は、API Examples/Customersの記事を和訳したものです。

記事内で使用する画像は、公式ドキュメント内の画像を引用して使用させていただいております。

Shopify アプリのご紹介

Shopify アプリである、「商品ページ発売予告アプリ | リテリア Coming Soon」は、商品ページを買えない状態のまま、発売日時の予告をすることができるアプリです。Shopify で Coming Soon 機能を実現することができます。

https://apps.shopify.com/shopify-application-314?locale=ja&from=daniel

Shopify アプリである、「らくらく日本語フォント設定|リテリア Font Picker」は、ノーコードで日本語フォントを使用できるアプリです。日本語フォントを導入することでブランドを演出することができます。

https://apps.shopify.com/font-picker-1?locale=ja&from=daniel

顧客データを管理する

以下は、顧客と連携するための GraphQL クエリとミューテーションの例です。

顧客への問い合わせ

顧客へのクエリの詳細については、customer object reference.を参照してください。

顧客の名前、電子メール、およびデフォルトのアドレスを取得します

Query: POST /api/2021-07/graphql.json

{
  customer(id: "gid://shopify/Customer/957611081784") {
    email
    firstName
    lastName
    defaultAddress {
      address1
      city
      province
      zip
      country
    }
  }
}
jsonresponse
{
  "data": {
    "customer": {
      "email": "testcustomer@email.com",
      "firstName": "test",
      "lastName": "testopherson",
      "defaultAddress": {
        "address1": "123 Fake st.",
        "city": "Springfield",
        "province": "Kentucky",
        "zip": "123abc",
        "country": "United States"
      }
    }
  }
}

エイリアスを使用して ID で 2 つの特定の顧客を取得します

Query: POST /api/2021-07/graphql.json

{
  john: customer(id: "gid://shopify/Customer/98495135746") {
    firstName
    lastName
    email
  }
  sally: customer(id: "gid://shopify/Customer/958346526776") {
    firstName
    lastName
    email
  }
}
json response
{
  "data": {
    "john": {
      "firstName": "John",
      "lastName": "Smith",
      "email": "john@test.com"
    },
    "sally": {
      "firstName": "Sally",
      "lastName": "Testopherson",
      "email": "sally@test.com"
    }
  }
}

フラグメントを使用して、3 人の顧客の電子メール、名前、およびアカウントの作成日を取得します

Query: POST /api/2021-07/graphql.json

{
  John: customer(id: "gid://shopify/Customer/98495135746") {
    ...customerProfile
  }
  Sasha: customer(id: "gid://shopify/Customer/958346526776") {
    ...customerProfile
  }
  Rahmil: customer(id: "gid://shopify/Customer/6305714562") {
    ...customerProfile
  }
}

fragment customerProfile on Customer {
  firstName
  lastName
  email
  createdAt
}
json response
{
  "data": {
    "John": {
      "firstName": "John",
      "lastName": "Smith",
      "email": "john@test.com",
      "createdAt": "2017-11-07T21:02:17Z"
    },
    "Sasha": {
      "firstName": "Sally",
      "lastName": "Testopherson",
      "email": "sally@test.com",
      "createdAt": "2019-02-11T14:51:36Z"
    },
    "Rahmil": {
      "firstName": "Rahmil",
      "lastName": "Curvington",
      "email": "rahmil@test.com",
      "createdAt": "2017-08-24T19:34:09Z"
    }
  }
}

すべての顧客のフィールドと接続を取得します

Query: POST /api/2021-07/graphql.json

{
  customer(id: "gid://shopify/Customer/957611081784") {
    acceptsMarketing
    addresses(first: 5) {
      address1
    }
    averageOrderAmountV2 {
      amount
    }
    canDelete
    createdAt
    defaultAddress {
      address1
    }
    displayName
    email
    events(first: 5) {
      edges {
        node {
          message
        }
      }
    }
    firstName
    hasNote
    hasTimelineComment
    id
    image {
      id
    }
    lastName
    legacyResourceId
    lifetimeDuration
    metafield(key: "app_key", namespace: "affiliates") {
      description
    }
    metafields(first: 5) {
      edges {
        node {
          id
        }
      }
    }
    note
    orders(first: 5) {
      edges {
        node {
          id
        }
      }
    }
    ordersCount
    phone
    state
    tags
    taxExempt
    totalSpent
    totalSpentV2 {
      amount
    }
    updatedAt
    validEmailAddress
    verifiedEmail
  }
}

Query: POST /api/2021-07/graphql.json

json response
{
  "data": {
    "customer": {
      "acceptsMarketing": false,
      "addresses": [],
      "averageOrderAmountV2": null,
      "canDelete": true,
      "createdAt": "2019-02-05T14:36:10Z",
      "defaultAddress": null,
      "displayName": "test testopherson",
      "email": "testcustomer@email.com",
      "events": {
        "edges": [
          {
            "node": {
              "message": "Shopify GraphiQL App added the email testcustomer@email.com to this customer."
            }
          }
        ]
      },
      "firstName": "test",
      "hasNote": false,
      "hasTimelineComment": false,
      "id": "gid://shopify/Customer/957611081784",
      "image": {
        "id": null
      },
      "lastName": "testopherson",
      "legacyResourceId": "957611081784",
      "lifetimeDuration": "about 2 hours",
      "metafield": null,
      "metafields": {
        "edges": []
      },
      "note": null,
      "orders": {
        "edges": []
      },
      "ordersCount": "0",
      "phone": null,
      "state": "DISABLED",
      "tags": [],
      "taxExempt": false,
      "totalSpent": "0.00",
      "totalSpentV2": {
        "amount": "0.0"
      },
      "updatedAt": "2019-02-05T14:36:11Z",
      "validEmailAddress": true,
      "verifiedEmail": true
    }
  }
}

顧客の作成

詳細については、customerCreate mutation referenceを参照してください。

顧客を作成し、顧客 ID を返します

Query: POST /api/2021-07/graphql.json

mutation {
  customerCreate(
    input: { email: "testcustomer@email.com", firstName: "test", lastName: "testopherson" }
  ) {
    customer {
      id
    }
  }
}
json response
{
  "data": {
    "customerCreate": {
      "customer": {
        "id": "gid://shopify/Customer/957611081784"
      }
    }
  }
}

顧客のタグを更新する

Query: POST /api/2021-07/graphql.json

mutation {
  customerUpdate(
    input: { id: "gid://shopify/Customer/958361468984", tags: ["Test tag, New tag"] }
  ) {
    customer {
      id
      tags
    }
  }
}
json response
{
  "data": {
    "customerUpdate": {
      "customer": {
        "id": "gid://shopify/Customer/958361468984",
        "tags": [
          "New tag",
          "Test tag"
        ]
      }
    }
  }
}

顧客の削除

詳細については、customerDelete mutation referenceを参照してください。

顧客を削除して ID を返却する

Query: POST /api/2021-07/graphql.json

mutation {
  customerDelete(input: { id: "gid://shopify/Customer/958361468984" }) {
    deletedCustomerId
  }
}
json response
{
  "data": {
    "customerDelete": {
      "deletedCustomerId": "gid://shopify/Customer/958361468984"
    }
  }
}

Shopify アプリのご紹介

Shopify アプリである、「商品ページ発売予告アプリ | リテリア Coming Soon」は、商品ページを買えない状態のまま、発売日時の予告をすることができるアプリです。Shopify で Coming Soon 機能を実現することができます。

https://apps.shopify.com/shopify-application-314?locale=ja&from=daniel

Shopify アプリである、「らくらく日本語フォント設定|リテリア Font Picker」は、ノーコードで日本語フォントを使用できるアプリです。日本語フォントを導入することでブランドを演出することができます。

https://apps.shopify.com/font-picker-1?locale=ja&from=daniel

Discussion

ログインするとコメントできます