Class: Rails::GraphQL::Request::PreparedData
- Inherits:
-
Object
- Object
- Rails::GraphQL::Request::PreparedData
- Defined in:
- lib/rails/graphql/request/prepared_data.rb
Overview
GraphQL Request Bypass Data
This class works in collaboration with the prepare stage of a request execution. In that stage, the strategy must check if the request already have a prepared data for the field. The field can result in a instance of this class, which than bypasses the prepare stage by grabbing the next value from here
Constant Summary collapse
- SCHEMA_BASED =
Helpers::WithSchemaFields::TYPE_FIELD_CLASS.keys.freeze
- NULL =
Object.new.freeze
- REPEAT_OPTIONS =
{ true => true, false => 1, once: 1, cycle: true, always: true, }.freeze
Class Method Summary collapse
-
.lookup(request, field) ⇒ Object
Look up the given
fieldusing the request as a reference.
Instance Method Summary collapse
-
#all ⇒ Object
The the whole value, because the prepare stage always deal with all the information available.
-
#enum ⇒ Object
Get the enumerable of the whole value.
-
#initialize(field, value, repeat: 1) ⇒ PreparedData
constructor
A new instance of PreparedData.
-
#next ⇒ Object
Get the next value, take into consideration the value on repeat.
-
#push(*values) ⇒ Object
Add one more item to the list of data.
Constructor Details
#initialize(field, value, repeat: 1) ⇒ PreparedData
Returns a new instance of PreparedData.
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/rails/graphql/request/prepared_data.rb', line 49 def initialize(field, value, repeat: 1) # Check if it has a valid field raise ::ArgumentError, (+" Unable to setup a prepared data for \"\#{field.inspect}\".\n You must provide a valid field.\n MSG\n\n @field = field\n @value = value\n @array = value.is_a?(Array) && !field.array?\n @repeat = true if !@array && !value.is_a?(Array)\n @repeat ||=\n case repeat\n when Numeric then repeat\n when Enumerator then repeat.size\n else REPEAT_OPTIONS[repeat]\n end\nend\n").squish unless field.is_a?(GraphQL::Field) |
Class Method Details
.lookup(request, field) ⇒ Object
Look up the given field using the request as a reference. It accepts any Rails::GraphQL::Field or a string where “query.create_user” means a query field on the request schema with create_user as name, or create_user as gql_name, and “User.devices” will use the schema of the request to lookup the type “User” and then pick the field devices
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/rails/graphql/request/prepared_data.rb', line 31 def self.lookup(request, field) return field if field.is_a?(GraphQL::Field) source, name = field.to_s.split('.') return if source.nil? || name.nil? if SCHEMA_BASED.any? { |item| item.to_s == source } request.schema.find_field!(source.to_sym, name) elsif (type = request.schema.find_type!(source)).is_a?(Helpers::WithFields) type.find_field!(name) else field end rescue NotFoundError # Return the original value, maybe it will be resolved somewhere else field end |
Instance Method Details
#all ⇒ Object
The the whole value, because the prepare stage always deal with all the information available
76 77 78 |
# File 'lib/rails/graphql/request/prepared_data.rb', line 76 def all @field.array? ? Array.wrap(@value) : @value end |
#enum ⇒ Object
Get the enumerable of the whole value
81 82 83 |
# File 'lib/rails/graphql/request/prepared_data.rb', line 81 def enum @enum ||= (@array ? @value.to_enum : @value.then) end |
#next ⇒ Object
Get the next value, take into consideration the value on repeat
86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/rails/graphql/request/prepared_data.rb', line 86 def next value = enum.next @field.array? ? Array.wrap(value) : value rescue StopIteration if @repeat == true || (@repeat != false && (@repeat -= 1) > 0) enum.rewind self.next else NULL end end |
#push(*values) ⇒ Object
Add one more item to the list of data
69 70 71 72 |
# File 'lib/rails/graphql/request/prepared_data.rb', line 69 def push(*values) return @value += values if @array @value = [@value, *values] end |