Module: GraphQL::Execution::Lookahead::ArgumentHelpers
- Defined in:
- lib/graphql/execution/lookahead.rb
Overview
TODO Dedup with interpreter
Class Method Summary collapse
-
.arg_to_value(query, graphql_object, arg_type, ast_value) ⇒ Array(is_present, value)
Get a Ruby-ready value from a client query.
-
.arguments(query, graphql_object, arg_owner, ast_node) ⇒ Object
-
.flatten_ast_value(query, v) ⇒ Object
Class Method Details
.arg_to_value(query, graphql_object, arg_type, ast_value) ⇒ Array(is_present, value)
Get a Ruby-ready value from a client query.
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 |
# File 'lib/graphql/execution/lookahead.rb', line 203 def arg_to_value(query, graphql_object, arg_type, ast_value) if ast_value.is_a?(GraphQL::Language::Nodes::VariableIdentifier) # If it's not here, it will get added later if query.variables.key?(ast_value.name) return true, query.variables[ast_value.name] else return false, nil end elsif ast_value.is_a?(GraphQL::Language::Nodes::NullValue) return true, nil elsif arg_type.is_a?(GraphQL::Schema::NonNull) arg_to_value(query, graphql_object, arg_type.of_type, ast_value) elsif arg_type.is_a?(GraphQL::Schema::List) # Treat a single value like a list arg_value = Array(ast_value) list = [] arg_value.map do |inner_v| _present, value = arg_to_value(query, graphql_object, arg_type.of_type, inner_v) list << value end return true, list elsif arg_type.is_a?(Class) && arg_type < GraphQL::Schema::InputObject # For these, `prepare` is applied during `#initialize`. # Pass `nil` so it will be skipped in `#arguments`. # What a mess. args = arguments(query, nil, arg_type, ast_value) # We're not tracking defaults_used, but for our purposes # we compare the value to the default value. return true, arg_type.new(ruby_kwargs: args, context: query.context, defaults_used: nil) else flat_value = flatten_ast_value(query, ast_value) return true, arg_type.coerce_input(flat_value, query.context) end end |
.arguments(query, graphql_object, arg_owner, ast_node) ⇒ Object
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 |
# File 'lib/graphql/execution/lookahead.rb', line 173 def arguments(query, graphql_object, arg_owner, ast_node) kwarg_arguments = {} arg_defns = arg_owner.arguments ast_node.arguments.each do |arg| arg_defn = arg_defns[arg.name] || raise("Invariant: missing argument definition for #{arg.name.inspect} in #{arg_defns.keys} from #{arg_owner}") # Need to distinguish between client-provided `nil` # and nothing-at-all is_present, value = arg_to_value(query, graphql_object, arg_defn.type, arg.value) if is_present # This doesn't apply to directives, which are legacy # Can remove this when Skip and Include use classes or something. if graphql_object value = arg_defn.prepare_value(graphql_object, value) end kwarg_arguments[arg_defn.keyword] = value end end arg_defns.each do |name, arg_defn| if arg_defn.default_value? && !kwarg_arguments.key?(arg_defn.keyword) kwarg_arguments[arg_defn.keyword] = arg_defn.default_value end end kwarg_arguments end |
.flatten_ast_value(query, v) ⇒ Object
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 |
# File 'lib/graphql/execution/lookahead.rb', line 238 def flatten_ast_value(query, v) case v when GraphQL::Language::Nodes::Enum v.name when GraphQL::Language::Nodes::InputObject h = {} v.arguments.each do |arg| h[arg.name] = flatten_ast_value(query, arg.value) end h when Array v.map { |v2| flatten_ast_value(query, v2) } when GraphQL::Language::Nodes::VariableIdentifier flatten_ast_value(query.variables[v.name]) else v end end |