Skip to content

Instantly share code, notes, and snippets.

@theorygeek
Created March 27, 2019 15:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save theorygeek/b48996407ecd8e3afa9137174b1351da to your computer and use it in GitHub Desktop.
Save theorygeek/b48996407ecd8e3afa9137174b1351da to your computer and use it in GitHub Desktop.
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