Module: GraphQL::BackwardsCompatibility Private

Defined in:
lib/graphql/backwards_compatibility.rb

Overview

This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.

Helpers for migrating in a backwards-compatibile way

Defined Under Namespace

Classes: FirstArgumentsWrapper, LastArgumentsWrapper

Class Method Summary collapse

Class Method Details

.get_arity(callable) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.


33
34
35
36
37
38
39
40
# File 'lib/graphql/backwards_compatibility.rb', line 33

def get_arity(callable)
  case callable
  when Method, Proc
    callable.arity
  else
    callable.method(:call).arity
  end
end

.wrap_arity(callable, from:, to:, name:, last: false) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Given a callable whose API used to take from arguments, check its arity, and if needed, apply a wrapper so that it can be called with to arguments. If a wrapper is applied, warn the application with name.

If last, then use the last arguments to call the function.


13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/graphql/backwards_compatibility.rb', line 13

def wrap_arity(callable, from:, to:, name:, last: false)
  arity = get_arity(callable)
  case arity
  when to
    # It already matches, return it as is
    callable
  when from
    # It has the old arity, so wrap it with an arity converter
    message ="#{name} with #{from} arguments is deprecated, it now accepts #{to} arguments, see:"
    backtrace = caller(0, 20)
    # Find the first line in the trace that isn't library internals:
    user_line = backtrace.find {|l| l !~ /lib\/graphql/ }
    warn(message + "\n" + user_line + "\n")
    wrapper = last ? LastArgumentsWrapper : FirstArgumentsWrapper
    wrapper.new(callable, from)
  else
    raise "Can't wrap #{callable} (arity: #{arity}) to have arity #{to}"
  end
end