Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
def detect_proc_needed(type)
return true if type == false
return false if type.is_a?(Proc)
return true if type.is_a?(Module)
return false unless type.is_a?(GraphQL::BaseType) && type.unwrap.metadata&.include?(:type_class)
klass = type.unwrap.metadata[:type_class]
return false if klass.try(:default_scalar)
true
end
def built_in_object(obj)
return true if obj.name.end_with?('Edge', 'Connection')
false
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment