Class: Graphiti::Query

Inherits:
Object show all
Defined in:
lib/graphiti/query.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(resource, params, association_name = nil, nested_include = nil, parents = []) ⇒ Query

Returns a new instance of Query.



5
6
7
8
9
10
11
12
13
14
# File 'lib/graphiti/query.rb', line 5

def initialize(resource, params, association_name = nil, nested_include = nil, parents = [])
  @resource = resource
  @association_name = association_name
  @params = params
  @params = @params.permit! if @params.respond_to?(:permit!)
  @params = @params.to_h if @params.respond_to?(:to_h)
  @params = @params.deep_symbolize_keys
  @include_param = nested_include || @params[:include]
  @parents = parents
end

Instance Attribute Details

#association_nameObject (readonly)

Returns the value of attribute association_name.



3
4
5
# File 'lib/graphiti/query.rb', line 3

def association_name
  @association_name
end

#paramsObject (readonly)

Returns the value of attribute params.



3
4
5
# File 'lib/graphiti/query.rb', line 3

def params
  @params
end

#resourceObject (readonly)

Returns the value of attribute resource.



3
4
5
# File 'lib/graphiti/query.rb', line 3

def resource
  @resource
end

Instance Method Details

#association?Boolean

Returns:

  • (Boolean)


16
17
18
# File 'lib/graphiti/query.rb', line 16

def association?
  !!@association_name
end

#debug_requested?Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/graphiti/query.rb', line 41

def debug_requested?
  !!@params[:debug]
end

#extra_fieldsObject



118
119
120
# File 'lib/graphiti/query.rb', line 118

def extra_fields
  @extra_fields ||= parse_fieldset(@params[:extra_fields] || {})
end

#fieldsObject



108
109
110
111
112
113
114
115
116
# File 'lib/graphiti/query.rb', line 108

def fields
  @fields ||= begin
    hash = parse_fieldset(@params[:fields] || {})
    hash.each_pair do |type, fields|
      hash[type] += extra_fields[type] if extra_fields[type]
    end
    hash
  end
end

#filtersObject



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/graphiti/query.rb', line 122

def filters
  @filters ||= begin
    {}.tap do |hash|
      (@params[:filter] || {}).each_pair do |name, value|
        name = name.to_sym

        if legacy_nested?(name)
          filter_name = value.keys.first.to_sym
          filter_value = value.values.first
          if @resource.get_attr!(filter_name, :filterable, request: true)
            hash[filter_name] = filter_value
          end
        elsif nested?(name)
          name = name.to_s.split(".").last.to_sym
          validate!(name, :filterable)
          hash[name] = value
        elsif top_level? && validate!(name, :filterable)
          hash[name] = value
        end
      end
    end
  end
end

#hashObject



45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/graphiti/query.rb', line 45

def hash
  @hash ||= {}.tap do |h|
    h[:filter] = filters unless filters.empty?
    h[:sort] = sorts unless sorts.empty?
    h[:page] = pagination unless pagination.empty?
    unless association?
      h[:fields] = fields unless fields.empty?
      h[:extra_fields] = extra_fields unless extra_fields.empty?
    end
    h[:stats] = stats unless stats.empty?
    h[:include] = sideload_hash unless sideload_hash.empty?
  end
end

#include_hashObject



186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/graphiti/query.rb', line 186

def include_hash
  @include_hash ||= begin
    requested = include_directive.to_hash

    allowlist = nil
    if @resource.context&.respond_to?(:sideload_allowlist)
      allowlist = @resource.context.sideload_allowlist
      allowlist = allowlist[@resource.context_namespace] if allowlist
    end

    allowlist ? Util::IncludeParams.scrub(requested, allowlist) : requested
  end

  @include_hash
end

#links?Boolean

Returns:

  • (Boolean)


24
25
26
27
28
29
30
31
# File 'lib/graphiti/query.rb', line 24

def links?
  return false if [:json, :xml, "json", "xml"].include?(params[:format])
  if Graphiti.config.links_on_demand
    [true, "true"].include?(@params[:links])
  else
    true
  end
end

#paginate?Boolean

Returns:

  • (Boolean)


217
218
219
# File 'lib/graphiti/query.rb', line 217

def paginate?
  ![false, "false"].include?(@params[:paginate])
end

#paginationObject



168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/graphiti/query.rb', line 168

def pagination
  @pagination ||= begin
    {}.tap do |hash|
      (@params[:page] || {}).each_pair do |name, value|
        if legacy_nested?(name)
          value.each_pair do |k, v|
            hash[k.to_sym] = v.to_i
          end
        elsif nested?(name)
          hash[name.to_s.split(".").last.to_sym] = value
        elsif top_level? && [:number, :size].include?(name.to_sym)
          hash[name.to_sym] = value.to_i
        end
      end
    end
  end
end

#pagination_links?Boolean

Returns:

  • (Boolean)


33
34
35
36
37
38
39
# File 'lib/graphiti/query.rb', line 33

def pagination_links?
  if Graphiti.config.pagination_links_on_demand
    [true, "true"].include?(@params[:pagination_links])
  else
    Graphiti.config.pagination_links
  end
end

#parentsObject



104
105
106
# File 'lib/graphiti/query.rb', line 104

def parents
  @parents ||= []
end

#resource_for_sideload(sideload) ⇒ Object



75
76
77
78
79
80
81
82
83
# File 'lib/graphiti/query.rb', line 75

def resource_for_sideload(sideload)
  if @resource.remote?
    Class.new(Graphiti::Resource) {
      self.remote = "_remote_sideload_"
    }.new
  else
    sideload.resource
  end
end

#sideload_hashObject



65
66
67
68
69
70
71
72
73
# File 'lib/graphiti/query.rb', line 65

def sideload_hash
  @sideload_hash = begin
    {}.tap do |hash|
      sideloads.each_pair do |key, value|
        hash[key] = sideloads[key].hash
      end
    end
  end
end

#sideloadsObject



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/graphiti/query.rb', line 85

def sideloads
  @sideloads ||= begin
    {}.tap do |hash|
      include_hash.each_pair do |key, sub_hash|
        sideload = @resource.class.sideload(key)

        if sideload || @resource.remote?
          sl_resource = resource_for_sideload(sideload)
          query_parents = parents + [self]
          sub_hash = sub_hash[:include] if sub_hash.key?(:include)
          hash[key] = Query.new(sl_resource, @params, key, sub_hash, query_parents)
        else
          handle_missing_sideload(key)
        end
      end
    end
  end
end

#sortsObject



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/graphiti/query.rb', line 146

def sorts
  @sorts ||= begin
    return @params[:sort] if @params[:sort].is_a?(Array)
    return [] if @params[:sort].nil?

    [].tap do |arr|
      sort_hashes do |key, value, type|
        if legacy_nested?(type)
          unless @resource.remote?
            @resource.get_attr!(key, :sortable, request: true)
          end
          arr << {key => value}
        elsif !type && top_level? && validate!(key, :sortable)
          arr << {key => value}
        elsif nested?("#{type}.#{key}")
          arr << {key => value}
        end
      end
    end
  end
end

#statsObject



202
203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'lib/graphiti/query.rb', line 202

def stats
  @stats ||= begin
    {}.tap do |hash|
      (@params[:stats] || {}).each_pair do |k, v|
        if legacy_nested?(k)
          raise NotImplementedError.new("Association statistics are not currently supported")
        elsif top_level?
          v = v.split(",") if v.is_a?(String)
          hash[k.to_sym] = Array(v).flatten.map(&:to_sym)
        end
      end
    end
  end
end

#top_level?Boolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/graphiti/query.rb', line 20

def top_level?
  !association?
end

#zero_results?Boolean

Returns:

  • (Boolean)


59
60
61
62
63
# File 'lib/graphiti/query.rb', line 59

def zero_results?
  !@params[:page].nil? &&
    !@params[:page][:size].nil? &&
    @params[:page][:size].to_i == 0
end