Sha256: 2afdce2ad6b9e85bc9c97ff63c7ecb0502a1396964ae2cf5aa01b6498e9432a8

Contents?: true

Size: 1.8 KB

Versions: 20

Compression:

Stored size: 1.8 KB

Contents

module NulogyGraphqlApi
  # This class provides a wrapper around the hash returned by GraphQL::Schema#execute.
  #
  # The hash is assumed to have String keys rather than Symbol keys because that is what
  # the Graphql-ruby gem outputs.
  class GraphqlResponse
    def initialize(graphql_response_json)
      @graphql_response_json = graphql_response_json
    end

    # Detects errors embedded in the GraphQL schema (intended to be shown to end-users):
    # {
    #   "data": {
    #     "somePayload": {
    #       "errors": [{
    #         "message": "Something went wrong!"
    #       }]
    #     }
    #   }
    # }
    def contains_errors_in_data_payload?
      if @graphql_response_json["data"].present?
        @graphql_response_json["data"].values.any? do |payload|
          payload.is_a?(Hash) ? payload["errors"].present? : false
        end
      else
        false
      end
    end

    # Detects errors in the standard GraphQL error list (intended to be shown to developers and API clients):
    # {
    #   "errors": [{
    #     "message": "Something went wrong!"
    #   }]
    # }
    def contains_errors_in_graphql_errors?
      @graphql_response_json["errors"].present?
    end

    def contains_errors?
      contains_errors_in_graphql_errors? || contains_errors_in_data_payload?
    end

    def http_response_code
      if contains_errors_in_graphql_errors?
        400
      elsif contains_errors_in_data_payload?
        # mimic Rails behaviour when there are validation errors. Also enable clients to easily
        # identify user-facing errors. CPI for instance will retry these to deal with race
        # conditions.
        422
      else
        200
      end
    end

    def render_http_response
      {
        json: @graphql_response_json,
        status: http_response_code
      }
    end
  end
end

Version data entries

20 entries across 20 versions & 1 rubygems

Version Path
nulogy_graphql_api-4.3.0 lib/nulogy_graphql_api/graphql_response.rb
nulogy_graphql_api-4.2.0 lib/nulogy_graphql_api/graphql_response.rb
nulogy_graphql_api-4.1.0 lib/nulogy_graphql_api/graphql_response.rb
nulogy_graphql_api-4.0.0 lib/nulogy_graphql_api/graphql_response.rb
nulogy_graphql_api-3.0.1 lib/nulogy_graphql_api/graphql_response.rb
nulogy_graphql_api-2.2.0 lib/nulogy_graphql_api/graphql_response.rb
nulogy_graphql_api-2.1.3 lib/nulogy_graphql_api/graphql_response.rb
nulogy_graphql_api-2.1.2 lib/nulogy_graphql_api/graphql_response.rb
nulogy_graphql_api-2.1.1 lib/nulogy_graphql_api/graphql_response.rb
nulogy_graphql_api-2.1.0 lib/nulogy_graphql_api/graphql_response.rb
nulogy_graphql_api-2.0.1 lib/nulogy_graphql_api/graphql_response.rb
nulogy_graphql_api-2.0.0 lib/nulogy_graphql_api/graphql_response.rb
nulogy_graphql_api-1.1.1 lib/nulogy_graphql_api/graphql_response.rb
nulogy_graphql_api-1.1.0 lib/nulogy_graphql_api/graphql_response.rb
nulogy_graphql_api-1.0.0 lib/nulogy_graphql_api/graphql_response.rb
nulogy_graphql_api-0.6.0 lib/nulogy_graphql_api/graphql_response.rb
nulogy_graphql_api-0.5.3 lib/nulogy_graphql_api/graphql_response.rb
nulogy_graphql_api-0.5.1 lib/nulogy_graphql_api/graphql_response.rb
nulogy_graphql_api-0.5.0 lib/nulogy_graphql_api/graphql_response.rb
nulogy_graphql_api-0.4.0 lib/nulogy_graphql_api/graphql_response.rb