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 |
# File 'lib/shopify_graphql/client.rb', line 7 def execute(query, **variables) response = client.query(query: query, variables: variables) ShopifyGraphql::Response.new(handle_response(response)) rescue ShopifyAPI::Errors::HttpResponseError => e ShopifyGraphql::Response.new(handle_response(e.response)) end |
#handle_graphql_errors(response) ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/shopify_graphql/client.rb', line 57 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) else raise ConnectionError.new(response, , code: error_code, doc: error_doc) end end |
#handle_response(response) ⇒ Object
22 23 24 25 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 |
# File 'lib/shopify_graphql/client.rb', line 22 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 .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
73 74 75 76 77 78 79 80 81 82 |
# File 'lib/shopify_graphql/client.rb', line 73 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
14 15 16 17 18 19 20 |
# File 'lib/shopify_graphql/client.rb', line 14 def parsed_body(response) if response.body.is_a?(Hash) JSON.parse(response.body.to_json, object_class: OpenStruct) else response.body end end |