Sha256: fbaf3713023e3b1dec5c768b100d3405ba65a2ab1dccd222e16e124323e5bab7

Contents?: true

Size: 1.51 KB

Versions: 9

Compression:

Stored size: 1.51 KB

Contents

# frozen_string_literal: true

module GraphqlRails
  # executes GraphQL queries and returns json
  class QueryRunner
    require 'graphql_rails/router'
    require 'graphql_rails/concerns/service'

    include ::GraphqlRails::Service

    def initialize(group: nil, params:, schema: nil, router: nil, **schema_options)
      @group = group
      @graphql_schema = schema
      @params = params
      @schema_options = schema_options
      @router = router
    end

    def call
      graphql_schema.execute(
        params[:query],
        variables: variables,
        operation_name: params[:operationName],
        **schema_options
      )
    end

    private

    attr_reader :schema_options, :params, :group

    def variables
      ensure_hash(params[:variables])
    end

    def graphql_schema
      @graphql_schema ||= router_schema
    end

    def router
      @router ||= ::GraphqlRouter
    end

    def router_schema
      router.graphql_schema(group)
    end

    def ensure_hash(ambiguous_param)
      if ambiguous_param.blank?
        {}
      elsif ambiguous_param.is_a?(String)
        ensure_hash(JSON.parse(ambiguous_param))
      elsif kind_of_hash?(ambiguous_param)
        ambiguous_param
      else
        raise ArgumentError, "Unexpected parameter: #{ambiguous_param.inspect}"
      end
    end

    def kind_of_hash?(object)
      return true if object.is_a?(Hash)

      defined?(ActionController) &&
        defined?(ActionController::Parameters) &&
        object.is_a?(ActionController::Parameters)
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
graphql_rails-2.0.0 lib/graphql_rails/query_runner.rb
graphql_rails-1.2.6 lib/graphql_rails/query_runner.rb
graphql_rails-1.2.4 lib/graphql_rails/query_runner.rb
graphql_rails-1.2.3 lib/graphql_rails/query_runner.rb
graphql_rails-1.2.2 lib/graphql_rails/query_runner.rb
graphql_rails-1.2.1 lib/graphql_rails/query_runner.rb
graphql_rails-1.2.0 lib/graphql_rails/query_runner.rb
graphql_rails-1.1.0 lib/graphql_rails/query_runner.rb
graphql_rails-1.0.0 lib/graphql_rails/query_runner.rb