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
- #handle_graphql_errors(response) ⇒ Object
- #handle_response(response) ⇒ Object
- #handle_user_errors(response) ⇒ Object
- #parsed_body(response) ⇒ Object
Instance Method Details
#client ⇒ Object
| 3 4 5 | # File 'lib/shopify_graphql/client.rb', line 3 def client @client ||= ShopifyAPI::Clients::Graphql::Admin.new(session: ShopifyAPI::Context.active_session) end | 
#execute(query, **variables) ⇒ Object
| 7 8 9 10 11 12 13 14 15 16 | # File 'lib/shopify_graphql/client.rb', line 7 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)) rescue JSON::ParserError => e raise ServerError.new(e, "Invalid JSON response") rescue Errno::ECONNRESET, Errno::EPIPE, Net::ReadTimeout, Net::OpenTimeout, OpenSSL::SSL::SSLError => e raise ServerError.new(e, "Network error") end | 
#handle_graphql_errors(response) ⇒ Object
| 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 | # File 'lib/shopify_graphql/client.rb', line 61 def handle_graphql_errors(response) return response if response.errors.blank? error = response.errors.first = error. error_code = error.extensions&.code error_doc = error.extensions&.documentation case error_code when "THROTTLED" raise TooManyRequests.new(response, , code: error_code, doc: error_doc) when "INTERNAL_SERVER_ERROR" raise ServerError.new(response, , code: error_code, doc: error_doc) else raise ConnectionError.new(response, , code: error_code, doc: error_doc) end end | 
#handle_response(response) ⇒ Object
| 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 | # File 'lib/shopify_graphql/client.rb', line 26 def handle_response(response) case response.code when 200..400 handle_graphql_errors(parsed_body(response)) when 400 raise BadRequest.new(parsed_body(response), code: response.code) when 401 raise UnauthorizedAccess.new(parsed_body(response), code: response.code) when 402 raise PaymentRequired.new(parsed_body(response), code: response.code) when 403 raise ForbiddenAccess.new(parsed_body(response), code: response.code) when 404 raise ResourceNotFound.new(parsed_body(response), code: response.code) when 405 raise MethodNotAllowed.new(parsed_body(response), code: response.code) when 409 raise ResourceConflict.new(parsed_body(response), code: response.code) when 410 raise ResourceGone.new(parsed_body(response), code: response.code) when 412 raise PreconditionFailed.new(parsed_body(response), code: response.code) when 422 raise ResourceInvalid.new(parsed_body(response), code: response.code) when 429 raise TooManyRequests.new(parsed_body(response), code: response.code) when 401...500 raise ClientError.new(parsed_body(response), code: response.code) when 500...600 raise ServerError.new(parsed_body(response), code: response.code) else raise ConnectionError.new(parsed_body(response), "Unknown response code: #{response.code}") end end | 
#handle_user_errors(response) ⇒ Object
| 79 80 81 82 83 84 85 86 87 88 | # File 'lib/shopify_graphql/client.rb', line 79 def handle_user_errors(response) return response if response.userErrors.blank? error = response.userErrors.first = error. error_fields = error.field error_code = error.code raise UserError.new(response, , fields: error_fields, code: error_code) end | 
#parsed_body(response) ⇒ Object
| 18 19 20 21 22 23 24 | # File 'lib/shopify_graphql/client.rb', line 18 def parsed_body(response) if response.body.is_a?(Hash) JSON.parse(response.body.to_json, object_class: OpenStruct) else response.body end end |