Skip to content

Instantly share code, notes, and snippets.

@Rafe
Created November 18, 2021 16:56
Show Gist options
  • Save Rafe/5c4e9c8e42a5a0899460fba927bea739 to your computer and use it in GitHub Desktop.
Save Rafe/5c4e9c8e42a5a0899460fba927bea739 to your computer and use it in GitHub Desktop.
class GraphQL::Execution::Interpreter::Runtime
def run_eager
# retrieve query root intormation
root_operation = query.selected_operation
root_op_type = root_operation.operation_type || "query"
root_type = schema.root_type_for_operation(root_op_type)
selection_response = GraphQLResultHash.new(nil, nil)
# create instance of type object, the #authorized_new method checks
# the authorized? method before initialize object
object = authorized_new(root_type, query.root_value, context)
# gather selections from query
gathered_selections = gather_selections(object, root_type, root_operation.selections)
evaluate_selections(context.scoped_context, object, root_type, gathered_selections, selection_response)
selection_response
end
def evaluate_selections(scoped_context, owner_object, owner_type, gathered_selections, results)
gathered_selections.each do |selection, ast_node|
# collect field informations from query ast
field_name = ast_node.name
field = owner_type.get_field(field_name)
return_type = field.type
# load arguments from query
@query.arguments_cache.dataload_for(ast_node, field, object) do |resolved_arguments|
# collect field information for next selections
return_type = field.type
next_selections = ast_node.selections
directives = ast_node.directives
field_result = resolve_with_directives(object, directives) do
field.resolve(object, arguments, context)
end
# handle nested selections
continue_field(owner_type, field_result, field, return_type, next_selections, object, selection, results)
set_result(results, field_name, field_result)
end
end
end
def continue_field(owner_type, value, field, current_type, next_selections, owner_object, arguments, selection, results)
# if return type is not graphql type, set result and return, else continue on selections
case current_type.kind.name
when "SCALAR", "ENUM"
set_result(results, selection, value)
when "OBJECT"
object = authorized_new(current_type, value, context)
gathered_selections = gather_selections(value, current_type, next_selections)
this_result = GraphQLResultHash.new(selection, results)
evaluate_selections(context.scoped_context, object, current_type, gathered_selections, this_result)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment