Skip to content

Instantly share code, notes, and snippets.

View theorygeek's full-sized avatar

Ryan Foster theorygeek

  • Figma, Inc.
  • Houston, TX
  • 22:30 (UTC -05:00)
View GitHub Profile
@theorygeek
theorygeek / detect_bad_keys.rb
Created July 5, 2016 16:19
Finds keys in a hash that are not strings
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
@theorygeek
theorygeek / merged_query.rb
Created November 19, 2016 03:09
GraphQL Merged Queries
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
@theorygeek
theorygeek / association_loader.rb
Last active October 31, 2023 07:15
Preloading Associations with graphql-batch
# 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
@theorygeek
theorygeek / caching_instrumentation.rb
Last active April 28, 2020 12:40
GraphQL Caching
# 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)
@theorygeek
theorygeek / initializers_graphql.rb
Created September 6, 2017 14:50
GraphQL Connection Counts
# 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`
@theorygeek
theorygeek / error_instrumentation.rb
Created May 7, 2018 16:08
Error Instrumentation
# 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)
@theorygeek
theorygeek / graphql.rb
Created March 27, 2019 15:29
Guard Against Recursive Calls
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
@theorygeek
theorygeek / async_graph_resolution.rb
Last active June 28, 2019 22:38
Better GraphQL Promise Handling
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