Created
March 27, 2019 15:29
-
-
Save theorygeek/b48996407ecd8e3afa9137174b1351da to your computer and use it in GitHub Desktop.
Guard Against Recursive Calls
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 | |
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