This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module AsyncGraphResolution | |
extend ActiveSupport::Concern | |
class_methods do | |
def async(method_name) | |
unbound_method = instance_method(method_name) | |
define_method(method_name) do |*args| | |
FiberResolver.new(unbound_method.bind(self), context, args, method_name, unbound_method.owner.name) | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
GraphQL::Define::AssignObjectField.instance_eval do | |
alias original_call call | |
def call(*args, &block) | |
if !built_in_object(args[0]) && detect_proc_needed(args[2]) | |
raise "You must wrap class-based types inside of a proc before attaching them to legacy-style types. Check #{args[0].name}.#{args[1]}" | |
end | |
original_call(*args, &block) | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# frozen_string_literal: true | |
class ErrorInstrumentation | |
def instrument(type, field) | |
old_resolver = field.resolve_proc | |
new_resolver = -> (obj, args, ctx) do | |
begin # rubocop:disable Style/RedundantBegin | |
result = old_resolver.call(obj, args, ctx) | |
if result.is_a?(StandardError) | |
set_error_context(result, type, obj, field, args, ctx) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Monkey Patch: Add a count field to all connections by default. | |
# Note, I tried to make this nicer, where it would just call the original method and then add a count field, | |
# but the challenge outlasted my patience. So I just replaced the whole method. TODO: better way to do this | |
module GraphQL | |
module Relay | |
module ConnectionType | |
def self.create_type(wrapped_type, edge_type: wrapped_type.edge_type, edge_class: GraphQL::Relay::Edge, nodes_field: ConnectionType.default_nodes_field, &block) | |
custom_edge_class = edge_class | |
# Any call that would trigger `wrapped_type.ensure_defined` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Define an instrumentation that performs the caching | |
class CachingInstrumentation | |
def instrument(_type, field) | |
return field unless field.metadata.include?(:cache_proc) | |
old_resolver = field.resolve_proc | |
new_resolver = -> (obj, args, ctx) do | |
# Get the caching key | |
cache_key = field.metadata[:cache_proc].call(obj, args, ctx) | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# frozen_string_literal: true | |
class AssociationLoader < GraphQL::Batch::Loader | |
attr_reader :klass, :association | |
def initialize(klass, association) | |
raise ArgumentError, "association to load must be a symbol (got #{association.inspect})" unless association.is_a?(Symbol) | |
raise ArgumentError, "cannot load associations for class #{klass.name}" unless klass < ActiveRecord::Base | |
raise TypeError, "association #{association} does not exist on #{klass.name}" unless klass.reflect_on_association(association) | |
@klass = klass |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module GraphQL | |
module Language | |
class MergedQuery | |
attr_accessor :original_queries, :original_variables | |
def initialize(queries, variables) | |
@original_queries = queries | |
@original_variables = variables | |
@fragment_counter = 0 | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def detect_bad_keys(obj, parent_path) | |
result = [] | |
case obj | |
when Hash | |
obj.each do |key, value| | |
if key.is_a?(String) | |
result.concat(detect_bad_keys(value, [*parent_path, key])) | |
else | |
result.push([*parent_path, key]) | |
end |