gql 0.0.1 → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/gql/call.rb +12 -9
- data/lib/gql/connection.rb +5 -1
- data/lib/gql/errors.rb +4 -4
- data/lib/gql/fields/connection.rb +5 -11
- data/lib/gql/node.rb +36 -17
- data/lib/gql/version.rb +1 -1
- metadata +1 -1
    
        checksums.yaml
    CHANGED
    
    | @@ -1,7 +1,7 @@ | |
| 1 1 | 
             
            ---
         | 
| 2 2 | 
             
            SHA1:
         | 
| 3 | 
            -
              metadata.gz:  | 
| 4 | 
            -
              data.tar.gz:  | 
| 3 | 
            +
              metadata.gz: e3b82548839c3920e499f9256cab69aa5ba31081
         | 
| 4 | 
            +
              data.tar.gz: c1631dc4bac082ad2086eadbe816791971f45b57
         | 
| 5 5 | 
             
            SHA512:
         | 
| 6 | 
            -
              metadata.gz:  | 
| 7 | 
            -
              data.tar.gz:  | 
| 6 | 
            +
              metadata.gz: 71d53e0856f5c0937d980deb1e1f27351dd9f76c84a6a9f449d23a26483bfd2745323c5d39fde5a480783f052389daf9ee5f690fc071f3eabc3bed3ebe308f8b
         | 
| 7 | 
            +
              data.tar.gz: 0247f336c99c7365ebe800e1df5213cbdc9d525d1f4cf9ceb44b689b60547fe99a7126a9ed7ebc59400b6264a6af0397a505808f8ee8f2e03e31de3300f15c20
         | 
    
        data/lib/gql/call.rb
    CHANGED
    
    | @@ -2,21 +2,24 @@ module GQL | |
| 2 2 | 
             
              class Call
         | 
| 3 3 | 
             
                attr_reader :target, :context
         | 
| 4 4 |  | 
| 5 | 
            -
                def initialize(caller, ast_node, target,  | 
| 5 | 
            +
                def initialize(caller, ast_node, target, variables, context)
         | 
| 6 6 | 
             
                  @caller, @ast_node, @target = caller, ast_node, target
         | 
| 7 | 
            -
                  @ | 
| 7 | 
            +
                  @variables, @context = variables, context
         | 
| 8 8 | 
             
                end
         | 
| 9 9 |  | 
| 10 10 | 
             
                def execute
         | 
| 11 | 
            -
                  args = @ast_node.arguments | 
| 12 | 
            -
                  target = instance_exec(*args,  | 
| 11 | 
            +
                  args = substitute_variables(@ast_node.arguments)
         | 
| 12 | 
            +
                  target = instance_exec(*args, &self.class.const_get(:Function))
         | 
| 13 13 |  | 
| 14 | 
            -
                   | 
| 14 | 
            +
                  result_class = self.class.const_get(:Result) || @caller.class
         | 
| 15 15 |  | 
| 16 | 
            -
                   | 
| 17 | 
            -
             | 
| 18 | 
            -
                  node = node_class.new(@ast_node, target, @variables, context)
         | 
| 19 | 
            -
                  node.__value
         | 
| 16 | 
            +
                  result = result_class.new(@ast_node, target, @variables, context)
         | 
| 17 | 
            +
                  result.__value
         | 
| 20 18 | 
             
                end
         | 
| 19 | 
            +
             | 
| 20 | 
            +
                private
         | 
| 21 | 
            +
                  def substitute_variables(args)
         | 
| 22 | 
            +
                    args.map { |arg| arg.is_a?(Symbol) ? @variables[arg] : arg }
         | 
| 23 | 
            +
                  end
         | 
| 21 24 | 
             
              end
         | 
| 22 25 | 
             
            end
         | 
    
        data/lib/gql/connection.rb
    CHANGED
    
    
    
        data/lib/gql/errors.rb
    CHANGED
    
    | @@ -23,7 +23,7 @@ module GQL | |
| 23 23 | 
             
                class UndefinedType < SchemaError
         | 
| 24 24 | 
             
                  def initialize(name)
         | 
| 25 25 | 
             
                    types = Schema.fields.keys.sort.map { |name| "`#{name}`" }
         | 
| 26 | 
            -
                    types = types.size > 0 ? " Available types  | 
| 26 | 
            +
                    types = types.size > 0 ? " Available types: #{types.to_sentence}." : ''
         | 
| 27 27 |  | 
| 28 28 | 
             
                    super("The field type `#{name}` is not known to the schema. Define it with `GQL::Schema.fields[my_type] = MyFieldType`.#{types}")
         | 
| 29 29 | 
             
                  end
         | 
| @@ -31,8 +31,8 @@ module GQL | |
| 31 31 |  | 
| 32 32 | 
             
                class UndefinedCall < SchemaError
         | 
| 33 33 | 
             
                  def initialize(name, node_class)
         | 
| 34 | 
            -
                    calls = node_class. | 
| 35 | 
            -
                    calls = calls.size > 0 ? " Available calls  | 
| 34 | 
            +
                    calls = node_class.call_classes.keys.sort.map { |name| "`#{name}`" }
         | 
| 35 | 
            +
                    calls = calls.size > 0 ? " Available calls: #{calls.to_sentence}." : ''
         | 
| 36 36 |  | 
| 37 37 | 
             
                    super("#{node_class} has no call named `#{name}`.#{calls}")
         | 
| 38 38 | 
             
                  end
         | 
| @@ -41,7 +41,7 @@ module GQL | |
| 41 41 | 
             
                class UndefinedField < SchemaError
         | 
| 42 42 | 
             
                  def initialize(name, node_class)
         | 
| 43 43 | 
             
                    fields = node_class.field_classes.keys.sort.map { |name| "`#{name}`" }
         | 
| 44 | 
            -
                    fields = fields.size > 0 ? " Available fields  | 
| 44 | 
            +
                    fields = fields.size > 0 ? " Available fields: #{fields.to_sentence}." : ''
         | 
| 45 45 |  | 
| 46 46 | 
             
                    super("#{node_class} has no field named `#{name}`.#{fields}")
         | 
| 47 47 | 
             
                  end
         | 
| @@ -10,20 +10,14 @@ module GQL | |
| 10 10 | 
             
                  end
         | 
| 11 11 |  | 
| 12 12 | 
             
                  def __raw_value
         | 
| 13 | 
            -
                     | 
| 13 | 
            +
                    connection_class = self.class.const_get(:CONNECTION_CLASS)
         | 
| 14 | 
            +
                    node_class = self.class.const_get(:NODE_CLASS)
         | 
| 14 15 |  | 
| 15 | 
            -
                     | 
| 16 | 
            +
                    raise Errors::InvalidNodeClass.new(connection_class, GQL::Connection) unless connection_class <= GQL::Connection
         | 
| 17 | 
            +
             | 
| 18 | 
            +
                    connection = connection_class.new(node_class, @ast_node, __target, @variables, __context)
         | 
| 16 19 | 
             
                    connection.__value
         | 
| 17 20 | 
             
                  end
         | 
| 18 | 
            -
             | 
| 19 | 
            -
                  private
         | 
| 20 | 
            -
                    def __connection_class__
         | 
| 21 | 
            -
                      self.class.const_get :CONNECTION_CLASS
         | 
| 22 | 
            -
                    end
         | 
| 23 | 
            -
             | 
| 24 | 
            -
                    def __node_class__
         | 
| 25 | 
            -
                      self.class.const_get :NODE_CLASS
         | 
| 26 | 
            -
                    end
         | 
| 27 21 | 
             
                end
         | 
| 28 22 | 
             
              end
         | 
| 29 23 | 
             
            end
         | 
    
        data/lib/gql/node.rb
    CHANGED
    
    | @@ -3,8 +3,8 @@ require 'active_support/core_ext/string/inflections' | |
| 3 3 |  | 
| 4 4 | 
             
            module GQL
         | 
| 5 5 | 
             
              class Node
         | 
| 6 | 
            -
                class_attribute : | 
| 7 | 
            -
                self. | 
| 6 | 
            +
                class_attribute :call_classes
         | 
| 7 | 
            +
                self.call_classes = {}
         | 
| 8 8 |  | 
| 9 9 | 
             
                class_attribute :field_classes
         | 
| 10 10 | 
             
                self.field_classes = {}
         | 
| @@ -17,12 +17,30 @@ module GQL | |
| 17 17 | 
             
                  end
         | 
| 18 18 |  | 
| 19 19 | 
             
                  def call(name, options = {}, &block)
         | 
| 20 | 
            -
                     | 
| 21 | 
            -
             | 
| 22 | 
            -
                      body: block || lambda { |*args| __target.public_send(name, *args) }
         | 
| 23 | 
            -
                    }
         | 
| 20 | 
            +
                    result_class = options[:returns]
         | 
| 21 | 
            +
                    function = block || lambda { |*args| target.public_send(name, *args) }
         | 
| 24 22 |  | 
| 25 | 
            -
                     | 
| 23 | 
            +
                    if result_class.is_a? Array
         | 
| 24 | 
            +
                      result_class.unshift Connection if result_class.size == 1
         | 
| 25 | 
            +
                      result_class.unshift Fields::Connection if result_class.size == 2
         | 
| 26 | 
            +
             | 
| 27 | 
            +
                      field_class, connection_class, node_class = result_class
         | 
| 28 | 
            +
             | 
| 29 | 
            +
                      raise Errors::InvalidNodeClass.new(field_class, Fields::Connection) unless field_class <= Fields::Connection
         | 
| 30 | 
            +
             | 
| 31 | 
            +
                      result_class = Class.new(field_class)
         | 
| 32 | 
            +
                      result_class.const_set :NODE_CLASS, node_class
         | 
| 33 | 
            +
                      result_class.const_set :CONNECTION_CLASS, connection_class
         | 
| 34 | 
            +
                    else
         | 
| 35 | 
            +
                      raise Errors::InvalidNodeClass.new(result_class, Node) unless result_class.nil? || result_class < Node
         | 
| 36 | 
            +
                    end
         | 
| 37 | 
            +
             | 
| 38 | 
            +
                    call_class = Class.new(Call)
         | 
| 39 | 
            +
                    call_class.const_set :Function, function
         | 
| 40 | 
            +
                    call_class.const_set :Result, result_class
         | 
| 41 | 
            +
             | 
| 42 | 
            +
                    self.const_set "#{name.to_s.camelize}Call", call_class
         | 
| 43 | 
            +
                    self.call_classes = call_classes.merge(name => call_class)
         | 
| 26 44 | 
             
                  end
         | 
| 27 45 |  | 
| 28 46 | 
             
                  def fields(&block)
         | 
| @@ -31,8 +49,12 @@ module GQL | |
| 31 49 |  | 
| 32 50 | 
             
                  def field(*names, base_class: nil, node_class: nil, connection_class: nil)
         | 
| 33 51 | 
             
                    classes = names.reduce({}) do |result, name|
         | 
| 34 | 
            -
                       | 
| 35 | 
            -
             | 
| 52 | 
            +
                      base_class ||= Field
         | 
| 53 | 
            +
             | 
| 54 | 
            +
                      raise Errors::InvalidNodeClass.new(base_class, Field) unless base_class <= Field
         | 
| 55 | 
            +
             | 
| 56 | 
            +
                      field_class = Class.new(base_class)
         | 
| 57 | 
            +
             | 
| 36 58 | 
             
                      field_class.const_set :NODE_CLASS, node_class
         | 
| 37 59 | 
             
                      field_class.const_set :CONNECTION_CLASS, connection_class
         | 
| 38 60 |  | 
| @@ -57,10 +79,6 @@ module GQL | |
| 57 79 | 
             
                  end
         | 
| 58 80 | 
             
                end
         | 
| 59 81 |  | 
| 60 | 
            -
                call :_identity do
         | 
| 61 | 
            -
                  target
         | 
| 62 | 
            -
                end
         | 
| 63 | 
            -
             | 
| 64 82 | 
             
                attr_reader :__target, :__context
         | 
| 65 83 |  | 
| 66 84 | 
             
                def initialize(ast_node, target, variables, context)
         | 
| @@ -70,11 +88,11 @@ module GQL | |
| 70 88 |  | 
| 71 89 | 
             
                def __value
         | 
| 72 90 | 
             
                  if ast_call = @ast_node.call
         | 
| 73 | 
            -
                     | 
| 91 | 
            +
                    call_class = self.class.call_classes[ast_call.name]
         | 
| 74 92 |  | 
| 75 | 
            -
                    raise Errors::UndefinedCall.new(ast_call.name, self.class) if  | 
| 93 | 
            +
                    raise Errors::UndefinedCall.new(ast_call.name, self.class.superclass) if call_class.nil?
         | 
| 76 94 |  | 
| 77 | 
            -
                    call =  | 
| 95 | 
            +
                    call = call_class.new(self, ast_call, __target, @variables, __context)
         | 
| 78 96 | 
             
                    call.execute
         | 
| 79 97 | 
             
                  elsif ast_fields = @ast_node.fields
         | 
| 80 98 | 
             
                    ast_fields.reduce({}) do |memo, ast_field|
         | 
| @@ -91,7 +109,8 @@ module GQL | |
| 91 109 | 
             
                          target = public_send(ast_field.name)
         | 
| 92 110 | 
             
                          field_class = self.class.field_classes[ast_field.name]
         | 
| 93 111 |  | 
| 94 | 
            -
                          raise Errors:: | 
| 112 | 
            +
                          raise Errors::UndefinedField.new(ast_field.name, self.class) if field_class.nil?
         | 
| 113 | 
            +
                          raise Errors::InvalidNodeClass.new(field_class.superclass, Field) unless field_class <= Field
         | 
| 95 114 |  | 
| 96 115 | 
             
                          field = field_class.new(ast_field, target, @variables, __context)
         | 
| 97 116 | 
             
                          field.__value
         | 
    
        data/lib/gql/version.rb
    CHANGED