Class: ShopifyGraphql::Client
- Inherits:
-
Object
- Object
- ShopifyGraphql::Client
- Defined in:
- lib/shopify_graphql/client.rb
Instance Method Summary collapse
- #client ⇒ Object
- #execute(query, headers: nil, **variables) ⇒ Object
- #generate_error_message(message: nil, code: nil, doc: nil) ⇒ Object
- #generate_user_errors_message(messages: nil, codes: nil, fields: []) ⇒ Object
- #handle_graphql_errors(response) ⇒ Object
- #handle_response(response, error = nil) ⇒ Object
- #handle_user_errors(response) ⇒ Object
- #parsed_body(response) ⇒ Object
Instance Method Details
#client ⇒ Object
28 29 30 |
# File 'lib/shopify_graphql/client.rb', line 28 def client @client ||= ShopifyAPI::Clients::Graphql::Admin.new(session: ShopifyAPI::Context.active_session) end |
#execute(query, headers: nil, **variables) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/shopify_graphql/client.rb', line 32 def execute(query, headers: nil, **variables) response = client.query(query: query, variables: variables, headers: headers) Response.new(handle_response(response)) rescue ShopifyAPI::Errors::HttpResponseError => e Response.new(handle_response(e.response, e)) rescue JSON::ParserError => e raise ServerError.new(response: response), "Invalid JSON response: #{e.message}" rescue Errno::ECONNRESET, Errno::EPIPE, Errno::ECONNREFUSED, Errno::ENETUNREACH, Net::ReadTimeout, Net::OpenTimeout, OpenSSL::SSL::SSLError, EOFError => e raise ServerError.new(response: response), "Network error: #{e.message}" rescue => e if (defined?(Socket::ResolutionError) and e.is_a?(Socket::ResolutionError)) raise ServerError.new(response: response), "Network error: #{e.message}" else raise e end end |
#generate_error_message(message: nil, code: nil, doc: nil) ⇒ Object
141 142 143 144 145 146 147 |
# File 'lib/shopify_graphql/client.rb', line 141 def (message: nil, code: nil, doc: nil) string = "Failed.".dup string << " Response code = #{code}." if code string << " Response message = #{message}.".gsub("..", ".") if string << " Documentation = #{doc}." if doc string end |
#generate_user_errors_message(messages: nil, codes: nil, fields: []) ⇒ Object
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 |
# File 'lib/shopify_graphql/client.rb', line 149 def (messages: nil, codes: nil, fields: []) if fields.any? field_count = fields.size result = ["#{field_count} #{"field".pluralize(field_count)} have failed:"] fields.each.with_index do |field, index| field_details = ["\n-"] field_details << "Response code = #{codes[index]}." if codes&.at(index) field_details << "Response message = #{messages[index]}.".gsub("..", ".") if &.at(index) field_details << "Field = #{field}." if field result << field_details.join(" ") end result.join("\n") else result = ["Failed."] result << "Response code = #{codes.join(", ")}." if codes&.any? result << "Response message = #{messages&.join(", ")}.".gsub("..", ".") if &.any? result.join(" ") end end |
#handle_graphql_errors(response) ⇒ Object
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/shopify_graphql/client.rb', line 94 def handle_graphql_errors(response) parsed_body = parsed_body(response) if parsed_body.errors.nil? || parsed_body.errors.empty? return parsed_body(response) end error = parsed_body.errors.first error_code = error.extensions&.code = ( message: error., code: error_code, doc: error.extensions&.documentation ) exception = case error_code when "THROTTLED" TooManyRequests.new(response: response) when "INTERNAL_SERVER_ERROR" ServerError.new(response: response) else ConnectionError.new(response: response) end exception.error_code = error_code exception.error_codes = [ error_code ] exception. = [ error. ] raise exception, end |
#handle_response(response, error = nil) ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/shopify_graphql/client.rb', line 57 def handle_response(response, error = nil) case response.code when 200..400 handle_graphql_errors(response) when 400 raise BadRequest.new(response: response), error. when 401 raise .new(response: response), error. when 402 raise PaymentRequired.new(response: response), error. when 403 raise ForbiddenAccess.new(response: response), error. when 404 raise ResourceNotFound.new(response: response), error. when 405 raise MethodNotAllowed.new(response: response), error. when 409 raise ResourceConflict.new(response: response), error. when 410 raise ResourceGone.new(response: response), error. when 412 raise PreconditionFailed.new(response: response), error. when 422 raise ResourceInvalid.new(response: response), error. when 423 raise ShopLocked.new(response: response), error. when 429, 430 raise TooManyRequests.new(response: response), error. when 401...500 raise ClientError.new(response: response), error. when 500...600 raise ServerError.new(response: response), error. else raise ConnectionError.new(response: response), error. end end |
#handle_user_errors(response) ⇒ Object
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/shopify_graphql/client.rb', line 122 def handle_user_errors(response) return response if response.userErrors.blank? error = response.userErrors.first errors = response.userErrors = ( messages: errors.map(&:message), codes: errors.map(&:code), fields: errors.map(&:field), ) exception = UserError.new(response: response) exception.error_code = error.code exception.error_codes = errors.map(&:code) exception.fields = errors.map(&:field) exception. = errors.map(&:message) raise exception, end |
#parsed_body(response) ⇒ Object
49 50 51 52 53 54 55 |
# File 'lib/shopify_graphql/client.rb', line 49 def parsed_body(response) if response.body.is_a?(Hash) JSON.parse(response.body.to_json, object_class: OpenStruct) else response.body end end |