Class: GraphitiGql::Schema::Util

Inherits:
Object
  • Object
show all
Defined in:
lib/graphiti_gql/schema/util.rb

Class Method Summary collapse

Class Method Details

.is_readable_sideload!(sideload) ⇒ Object



63
64
65
66
67
68
69
70
71
# File 'lib/graphiti_gql/schema/util.rb', line 63

def self.is_readable_sideload!(sideload)
  readable = sideload.instance_variable_get(:@readable)
  if readable.is_a?(Symbol)
    path = Graphiti.context[:object][:current_path].join(".")
    unless sideload.parent_resource.send(readable)
      raise Errors::UnauthorizedField.new(path)
    end
  end
end

.params_from_args(arguments) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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
# File 'lib/graphiti_gql/schema/util.rb', line 4

def self.params_from_args(arguments)
  lookahead = arguments.delete(:lookahead)
  params = arguments.as_json.deep_transform_keys { |key| key.to_s.underscore.to_sym }
  if params[:sort]
    params[:sort] = Util.transform_sort_param(params[:sort])
  end

  if (first = params.delete(:first))
    params[:page] ||= {}
    params[:page][:size] = first
  end


  if (last = params.delete(:last))
    params[:page] ||= {}
    params[:page][:size] = last
    params[:reverse] = true
  end

  if (after = params.delete(:after))
    params[:page] ||= {}
    params[:page][:after] = after
 end

  if (before = params.delete(:before))
    params[:page] ||= {}
    params[:page][:before] = before
  end

  if (id = params.delete(:id))
    params[:filter] ||= {}
    params[:filter][:id] = { eq: id }
  end

  if lookahead.selects?(:stats)
    stats = lookahead.selection(:stats)
    payload = {}
    stats.selections.map(&:name).each do |name|
      payload[name] = stats.selection(name).selections.map(&:name)
    end
    params[:stats] = payload

    # only requesting stats
    if lookahead.selections.map(&:name) == [:stats]
      params[:page] = { size: 0 }
    end
  end

  params
end

.transform_sort_param(sorts) ⇒ Object



55
56
57
58
59
60
61
# File 'lib/graphiti_gql/schema/util.rb', line 55

def self.transform_sort_param(sorts)
  sorts.map do |sort_param|
    sort = sort_param[:att].underscore
    sort = "-#{sort}" if sort_param[:dir] == "desc"
    sort
  end.join(",")
end