0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

初めてのShopifyアプリ開発 - データへのアクセス権限付与の手順

0
Posted at

Shopifyとは

  • オンラインショップの運営、在庫管理、顧客管理のためのSaaS型プラットフォーム

Shopifyアプリとは

  • Shopifyの機能を拡張するプラグイン(例:Ship&Co:運送会社の伝票出力連携など)
  • アプリ開発環境(dev dashboard, shopifypertners、Shopify CLI)
  • 2026年1月にストア管理画面から作成できたレガシーカスタムアプリが廃止され、dev dashboardに統合

筆者の経験

  • オンラインショップをShopifyで5年ほど運営
  • Ship&Coやレビュー掲載などのShopifyアプリを使用
  • 自分で作るのは初めて

アプリ作成の目的

  • 年間生産量がほぼ決まっている各商品について、最新の当年出荷済量と現在の在庫量(ストア内でavailableになっている量)を把握したい

アプリがshop内の情報にアクセスするための権限(scopes)付与の手順

※ コマンドは更新されるかもしれないのでリファレンスを参照ください(https://shopify.dev/docs/api/shopify-cli/app/

  • まず、shopify app init → shopify app config link → アプリをインストールしてストアからアプリを確認できる状態にしておく
  • shopify.app.tomlのaccess_scopesを編集
shopify.app.toml
[access_scopes]
# Learn more at https://shopify.dev/docs/apps/tools/cli/configuration#access_scopes
scopes = "read_orders"
optional_scopes = [ ]
use_legacy_install_flow = false
  • 個人情報(name, address, mail, tel)へのアクセスが必要な時はshopifypertners( https://partners.shopify.com/*******/ )から アプリ配布>すべてのアプリ>アプリを選択>APIアクセス要求>保護された顧客データへのアクセス>各項目についてデータの用途と理由を選択 することが必要
    画像の「選択する」押下から選択肢にアクセス
    スクリーンショット 2026-01-24 8.55.52.png

  • shopify app deploy で新scopesのアプリをリリース

  • shopify app dev でアプリを起動

データへのアクセスをGraphiQL(ブラウザ)で確認

アプリを起動すると、アプリとともにAPIテスト用のブラウザが立ち上がる。以下にアクセスしてクエリを投げてみる
スクリーンショット 2026-01-24 9.27.26.png

クエリとエラーの内容

  • クエリ
 query getOrders($cursor: String){
      orders(first:50, after: $cursor){

        edges{
          node{
            id
            name
            totalPriceSet { shopMoney { amount } }
            displayAddress{
              address1
            	address2
              city
              company
              firstName
              lastName
            }
            fulfillmentOrders(first: 10) {
              edges {
                node {
                  status
                  lineItems(first: 20) {
                    edges {
                      node {
                        sku
                        totalQuantity
                        remainingQuantity
                        lineItem {
                          title
                        }
                      }
                    }
                  }
                }
              }
            }
            
            billingAddressMatchesShippingAddress
            shippingAddress {
              name
              address1
              city
              country
              
            }
          displayFulfillmentStatus
          lineItems(first:2){
            edges{
              node{
                title
                quantity
                }
              }
            }
          }
        }
      pageInfo{
        hasNextPage
        }
      }
    }
  • 結果
GraphiQL
  "errors": [
    {
      "message": "Access denied for fulfillmentOrders field.",
      "locations": [
        {
          "line": 18,
          "column": 9
        }
      ],
      "extensions": {
        "code": "ACCESS_DENIED",
        "documentation": "https://shopify.dev/api/usage/access-scopes"
      },
      "path": [
        "orders",
        "edges",
        0,
        "node",
        "fulfillmentOrders"
      ]
    }
  ],

アクセス拒否でした

ドキュメントを確認

shopify.app.toml
scopes = "read_orders, read_merchant_managed_fulfillment_orders, read_third_party_fulfillment_orders"

scopes追加後の実行結果

データはshopifyテスト用ストアのデータ

GraphiQL
{
  "data": {
    "orders": {
      "edges": [
        {
          "node": {
            "id": "gid://shopify/Order/6663644872871",
            "name": "#1003",
            "totalPriceSet": {
              "shopMoney": {
                "amount": "2522.15"
              }
            },
            "displayAddress": {
              "address1": "Box 42 - 151 O'Connor St",
              ...
            },
            "fulfillmentOrders": {
              "edges": [
                {
                  "node": {
                    "status": "OPEN",
                    "lineItems": {
                      "edges": [
                        {
                          "node": {
                            "sku": "sku-hosted-1",
                            "totalQuantity": 1,
                            "remainingQuantity": 1,
                            "lineItem": {
                              "title": "The 3p Fulfilled Snowboard"
                            }
                          }
                        }
                      ]
                    }
                  }
                },
                {
                  "node": {
                    "status": "CLOSED",
                    "lineItems": {
                      "edges": [
                        {
                          "node": {
                            "sku": null,
                            "totalQuantity": 1,
                            "remainingQuantity": 0,
                            "lineItem": {
                              "title": "Selling Plans Ski Wax"
                            }
                          }
                        }
                      ]
                    }
                  }
                }
              ]
            },
            "billingAddressMatchesShippingAddress": false,
            "shippingAddress": {
              "name": "Karine Ruby",
              ...
            },
            "displayFulfillmentStatus": "PARTIALLY_FULFILLED",
            "lineItems": {
              "edges": [
                {
                  "node": {
                    "title": "The 3p Fulfilled Snowboard",
                    "quantity": 1
                  }
                },
                {
                  "node": {
                    "title": "Selling Plans Ski Wax",
                    "quantity": 1
                  }
                }
              ]
            }
          }
        }
      ],
      "pageInfo": {
        "hasNextPage": false
      }
    }
  },
  "extensions": {
    "cost": {
      "requestedQueryCost": 422,
      "actualQueryCost": 6,
      "throttleStatus": {
        "maximumAvailable": 2000,
        "currentlyAvailable": 1994,
        "restoreRate": 100
      }
    }
  }
}

という感じで、PARTIALLY_FULFILLED な注文について何が出荷済で何が未出荷なのか確認できました。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?