Skip to content
This repository was archived by the owner on Jun 1, 2023. It is now read-only.

class ShopifyCli::AdminAPI

Konstantin Tennhard edited this page Jan 28, 2021 · 6 revisions

ShopifyCli::AdminAPI wraps our graphql functionality with authentication so that these concerns are taken care of.

Class Methods

query

query(ctx, query_name, shop:, api_version: nil, **variables) issues a graphql query or mutation to the Shopify Admin API. It loads a graphql query from a file so that you do not need to use large unwieldy query strings.

Parameters

  • ctx: running context from your command
  • query_name: name of the query you want to use, loaded from the lib/graphql directory.
  • api_version: an api version string to specify version. If no version is supplied then unstable will be used
  • shop: shop domain string for which shop that you are calling the admin API on. If not supplied, then it will be fetched from the .env file
  • **variable: a hash of variables to be supplied to the query ro mutation

Raises

  • http 404 will raise a ShopifyCli::API::APIRequestNotFoundError
  • http 400..499 will raise a ShopifyCli::API::APIRequestClientError
  • http 500..599 will raise a ShopifyCli::API::APIRequestServerError
  • All other codes will raise ShopifyCli::API::APIRequestUnexpectedError

Returns

  • resp - graphql response data hash. This can be a different shape for every query.

Example

ShopifyCli::AdminAPI.query(@ctx, 'all_organizations')
see source

# File lib/shopify-cli/admin_api.rb, line 40
def query(ctx, query_name, shop:, api_version: nil, **variables)
  authenticated_req(ctx, shop) do
    api_client(ctx, api_version, shop).query(query_name, variables: variables)
  end
end

rest_request

rest_request(ctx, shop:, path:, body: nil, method: "GET", api_version: nil, token: nil)

Parameters

  • ctx: running context from your command

  • shop: shop domain string for shop whose admin you are calling

  • path: path string (excluding prefixes and API version) for specific JSON that you are requesting ex. "data.json" instead of "/admin/api/unstable/data.json"

  • body: data string for corresponding REST request types

  • method: REST request string for the type of request; if nil, will perform GET request

  • api_version: API version string to specify version; if nil, latest will be used

  • token: shop password string for authentication to shop

Raises

  • http 404 will raise a ShopifyCli::API::APIRequestNotFoundError
  • http 400..499 will raise a ShopifyCli::API::APIRequestClientError
  • http 500..599 will raise a ShopifyCli::API::APIRequestServerError
  • All other codes will raise ShopifyCli::API::APIRequestUnexpectedError

Returns

  • resp - JSON response array

Example

ShopifyCli::AdminAPI.rest_request(@ctx,
                                  shop: 'shop.myshopify.com',
                                  path: 'data.json',
                                  token: 'password')
see source

# File lib/shopify-cli/admin_api.rb, line 78
def rest_request(ctx, shop:, path:, body: nil, method: "GET", api_version: nil, token: nil)
  ShopifyCli::DB.set(admin_access_token: token) unless token.nil?
  url = URI::HTTPS.build(host: shop, path: "/admin/api/#{fetch_api_version(ctx, api_version, shop)}/#{path}")
  resp = api_client(ctx, api_version, shop, path: path).request(url: url.to_s, body: body, method: method)
  ShopifyCli::DB.set(admin_access_token: nil) unless token.nil?
  resp
end

Clone this wiki locally