Sha256: 4f3321bfd6cc47bf1c63d9c773a9f23a8ef565dc58f79936c420779153c05d09

Contents?: true

Size: 1.88 KB

Versions: 40

Compression:

Stored size: 1.88 KB

Contents

module Graphiti
  class Runner
    attr_reader :params
    attr_reader :deserialized_payload

    def initialize(resource_class, params, query = nil, action = nil)
      @resource_class = resource_class
      @params = params
      @query = query
      @action = action

      validator = RequestValidator.new(jsonapi_resource, params, action)
      validator.validate!

      @deserialized_payload = validator.deserialized_payload
    end

    def jsonapi_resource
      @jsonapi_resource ||= @resource_class.new
    end

    # Typically, this is 'self' of a controller
    # We're overriding here so we can do stuff like
    #
    # Graphiti.with_context my_context, {} do
    #   Runner.new ...
    # end
    def jsonapi_context
      Graphiti.context[:object]
    end

    def query
      @query ||= Query.new(jsonapi_resource, params, nil, nil, [], @action)
    end

    def query_hash
      @query_hash ||= query.hash
    end

    def wrap_context
      Graphiti.with_context(jsonapi_context, action_name.to_sym) do
        yield
      end
    end

    def jsonapi_scope(scope, opts = {})
      jsonapi_resource.build_scope(scope, query, opts)
    end

    def jsonapi_render_options
      options = {}
      options.merge!(default_jsonapi_render_options)
      options[:meta] ||= {}
      options[:expose] ||= {}
      options[:expose][:context] = jsonapi_context
      options
    end

    def proxy(base = nil, opts = {})
      base ||= jsonapi_resource.base_scope
      scope_opts = opts.slice :sideload_parent_length,
        :default_paginate,
        :after_resolve,
        :sideload,
        :parent,
        :params,
        :bypass_required_filters
      scope = jsonapi_scope(base, scope_opts)
      ResourceProxy.new jsonapi_resource,
        scope,
        query,
        payload: deserialized_payload,
        single: opts[:single],
        raise_on_missing: opts[:raise_on_missing]
    end
  end
end

Version data entries

40 entries across 40 versions & 1 rubygems

Version Path
graphiti-1.6.4 lib/graphiti/runner.rb
graphiti-1.6.3 lib/graphiti/runner.rb
graphiti-1.6.2 lib/graphiti/runner.rb
graphiti-1.6.1 lib/graphiti/runner.rb
graphiti-1.6.0 lib/graphiti/runner.rb
graphiti-1.5.3 lib/graphiti/runner.rb
graphiti-1.5.2 lib/graphiti/runner.rb
graphiti-1.5.1 lib/graphiti/runner.rb
graphiti-1.5.0 lib/graphiti/runner.rb
graphiti-1.4.0 lib/graphiti/runner.rb
graphiti-1.3.9 lib/graphiti/runner.rb
graphiti-1.3.8 lib/graphiti/runner.rb
graphiti-1.3.7 lib/graphiti/runner.rb
graphiti-1.3.6 lib/graphiti/runner.rb
graphiti-1.3.5 lib/graphiti/runner.rb
graphiti-1.3.4 lib/graphiti/runner.rb
graphiti-1.3.3 lib/graphiti/runner.rb
graphiti-1.3.2 lib/graphiti/runner.rb
graphiti-1.3.1 lib/graphiti/runner.rb
graphiti-1.3.0 lib/graphiti/runner.rb