Class: ShopifyGraphql::Client
- Inherits:
-
Object
- Object
- ShopifyGraphql::Client
- Defined in:
- lib/shopify_graphql/client.rb
Instance Method Summary collapse
- #client ⇒ Object
- #execute(query, **variables) ⇒ Object
- #generate_error_message(message: nil, code: nil, doc: nil, fields: nil) ⇒ 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, **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, **variables) response = client.query(query: query, variables: variables) 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.}" 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.}" rescue => e if (defined?(Socket::ResolutionError) and e.is_a?(Socket::ResolutionError)) raise ServerError.new(response: response), "Network error: #{e.}" else raise e end end |
#generate_error_message(message: nil, code: nil, doc: nil, fields: nil) ⇒ Object
137 138 139 140 141 142 143 144 |
# File 'lib/shopify_graphql/client.rb', line 137 def (message: nil, code: nil, doc: nil, fields: nil) string = "Failed.".dup string << " Response code = #{code}." if code string << " Response message = #{}.".gsub("..", ".") if string << " Documentation = #{doc}." if doc string << " Fields = #{fields}." if fields string 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 |
# 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 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 UnauthorizedAccess.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
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/shopify_graphql/client.rb', line 120 def handle_user_errors(response) return response if response.userErrors.blank? error = response.userErrors.first error_code = error.code = ( message: error., code: error_code, fields: error.field, ) exception = UserError.new(response: response) exception.error_code = error_code exception.fields = error.field 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 |