Class: ShopifyAPI::GraphQL::Request

Inherits:
Object
  • Object
show all
Defined in:
lib/shopify_api/graphql/request.rb,
lib/shopify_api/graphql/request/version.rb

Overview

Small class to simplify the writing and handling of GraphQL queries and mutations for the Shopify Admin API. Comes with built-in retry, pagination, error handling, and more!

Defined Under Namespace

Classes: NotFoundError, UserError

Constant Summary collapse

Error =
Class.new(StandardError)
VERSION =
"0.0.2"

Instance Method Summary collapse

Constructor Details

#initialize(shop, token, options = nil) ⇒ Request

Create a new GraphQL client to connect to shop

Arguments

[shop (String)] Shopify domain to make requests against [token (String)] Shopify API token [options (Hash)] Client options. Optional.

Options

[:raise_if_not_found (Boolean)] If true raise a NotFoundError if the requested record is not found. Defaults to true. [:raise_if_user_errors (Boolean)] If true raise a UserError if the mutation resulted in user errors. Defaults to true. [:snake_case (Boolean)] Convert response Hash keys to snake_case symbols. Defaults to true.

Additional options: those accepted by ShopifyAPI::GraphQL::Tiny



66
67
68
69
70
71
72
73
74
75
76
# File 'lib/shopify_api/graphql/request.rb', line 66

def initialize(shop, token, options = nil)
  @options = (options || {}).dup

  [:snake_case, :raise_if_not_found, :raise_if_user_errors].each do |name|
    @options[name] = true unless @options.include?(name)
  end

  @gql = ShopifyAPI::GraphQL::Tiny.new(shop, token, @options)

  @gid = TinyGID.new("shopify")
end

Instance Method Details

#execute(query, variables = nil, options = nil) ⇒ Object

Executes a query or mutation

Arguments

[query (String)] Query or mutation to execute [token (String)] Optional variables accepted by query [options (Hash)] Optional

Options

These override the instance's defaults for a single query or mutation.

[:raise_if_not_found (Boolean)] - raise a ShopifyAPI::GraphQL::Request::NotFoundError if query target is not found; defaults to true [:raise_if_user_errors (Boolean)] - raise a ShopifyAPI::GraphQL::Request::UserError if the response contains them; defaults to true [:snake_case (Boolean)] - Accept :snake_case-style hash keys and returned response hash keys to be snake_case; defaults to true

Returns

The GraphQL response Hash

Errors

  • ArgumentError
  • ShopifyAPI::GraphQL::Request::UserError - the a mutation contains user errors
  • ShopifyAPI::GraphQL::Request::NotFoundError - if the query cannot be find the given object
  • ShopifyAPI::GraphQL::Tiny::ConnectionError
  • ShopifyAPI::GraphQL::Tiny::HTTPError
  • ShopifyAPI::GraphQL::Tiny::RateLimitError - if the retry attempts have been exceeded
  • ShopifyAPI::GraphQL::Tiny::GraphQLError


110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/shopify_api/graphql/request.rb', line 110

def execute(query, variables = nil, options = nil)
  options = @options.merge(options || {})

  variables = camelize_keys(variables) if options[:snake_case]

  data = gql.execute(query, variables)

  raise_if_not_found(data, variables) if options[:raise_if_not_found]
  raise_if_user_errors(data) if options[:raise_if_user_errors]

  data = snake_case_keys(data) if options[:snake_case]

  data
end

#paginate(query, variables = nil, options = nil) ⇒ Object

Executes a query using pagination.

Using pagination requires you to include the PageInfo in your query.

Arguments

Same as #execute but also accepts a block that will be called with each page. If a block not given returns an instance of Enumerator::Lazy that will fetch the next page on each iteration

Errors

See #execute



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/shopify_api/graphql/request.rb', line 142

def paginate(query, variables = nil, options = nil)
  options = @options.merge(options || {})

  variables = camelize_keys(variables) if options[:snake_case]

  pager = gql.paginate
  # execute() returns a lazy enumerator so we're not loading everything now.
  pages = pager.execute(query, variables).map do |page|
    raise_if_not_found(page, variables) if options[:raise_if_not_found]
    raise_if_user_errors(page) if options[:raise_if_user_errors]

    page = snake_case_keys(page) if options[:snake_case]
    page
  end

  return pages unless block_given?

  pages.each { |page| yield page }

  nil
end