gql 0.0.5 → 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 96bce7d738e7b21e2253ec10315f64b4fb33408e
4
- data.tar.gz: c28e4961a061063f51703cd714eb646efb8465ff
3
+ metadata.gz: 20b402e587fe168bb43d6b683c6f41d5d313d828
4
+ data.tar.gz: 5b5a28f87cce7e5e72df1ef7d366ae06689b269a
5
5
  SHA512:
6
- metadata.gz: 8069bf344b4c87f43b718f4b0b0a8ad2c30be35f186bf4d1f1e2f1119f356feef3c8a126982a177c860817d32dfa5b1c63d4268c96486f2272691e7f1593f59c
7
- data.tar.gz: 18e1efe2c211f3579b12b808fdd842eb938c8f45f2b0b50049582e819adb6fe4f99eb33a2c0e6bb6a642b69acd20a04deb72f7054edc96001821f940cb2e237d
6
+ metadata.gz: 1ce7cd9bf3934dbc32adf6c6d1c69db6f1c8cbe78cdc8a0eb3aefcaa7a9f2511a385ffab599a2fe1bac156df59249fdcecaf73f40b2daf43ffd9e5fb5b5280ce
7
+ data.tar.gz: 910724d32eb06b282a067955789b2ff554dbe1034911a6e033e18720ab2b140126e85548e2d6c23e1b06fc1f48e77977d6384a712e8d70c0023f505de534f24e
data/lib/gql.rb CHANGED
@@ -1,11 +1,19 @@
1
1
  module GQL
2
+ autoload :Array, 'gql/array'
3
+ autoload :Boolean, 'gql/boolean'
2
4
  autoload :Call, 'gql/call'
3
5
  autoload :Config, 'gql/config'
4
6
  autoload :Connection, 'gql/connection'
7
+ autoload :Error, 'gql/errors'
5
8
  autoload :Executor, 'gql/executor'
6
9
  autoload :Field, 'gql/field'
10
+ autoload :List, 'gql/list'
7
11
  autoload :Node, 'gql/node'
12
+ autoload :Number, 'gql/number'
13
+ autoload :Object, 'gql/object'
8
14
  autoload :Parser, 'gql/parser'
15
+ autoload :Simple, 'gql/simple'
16
+ autoload :String, 'gql/string'
9
17
  autoload :Tokenizer, 'gql/tokenizer'
10
18
  autoload :VERSION, 'gql/version'
11
19
 
@@ -19,20 +27,10 @@ module GQL
19
27
  autoload :UndefinedFieldType, 'gql/errors'
20
28
  end
21
29
 
22
- module Fields
23
- autoload :Array, 'gql/fields/array'
24
- autoload :Boolean, 'gql/fields/boolean'
25
- autoload :Connection, 'gql/fields/connection'
26
- autoload :Float, 'gql/fields/float'
27
- autoload :Integer, 'gql/fields/integer'
28
- autoload :Object, 'gql/fields/object'
29
- autoload :String, 'gql/fields/string'
30
- end
31
-
32
30
  module Schema
33
31
  autoload :Call, 'gql/schema/call'
34
- autoload :Connection, 'gql/schema/connection'
35
32
  autoload :Field, 'gql/schema/field'
33
+ autoload :List, 'gql/schema/list'
36
34
  autoload :Node, 'gql/schema/node'
37
35
  autoload :Parameter, 'gql/schema/parameter'
38
36
  autoload :Placeholder, 'gql/schema/placeholder'
@@ -43,7 +41,7 @@ module GQL
43
41
  Thread.current[:gql_config] ||= Config.new
44
42
  end
45
43
 
46
- %w(root_node_class field_types).each do |method|
44
+ %w(root_node_class field_types default_list_class).each do |method|
47
45
  module_eval <<-DELEGATORS, __FILE__, __LINE__ + 1
48
46
  def #{method}
49
47
  config.#{method}
@@ -78,24 +76,21 @@ module GQL
78
76
  tokenizer = Tokenizer.new
79
77
  tokenizer.scan_setup input
80
78
 
81
- result = []
82
-
83
- while token = tokenizer.next_token
84
- result << token
85
- yield token if block_given?
79
+ [].tap do |result|
80
+ while token = tokenizer.next_token
81
+ result << token
82
+ yield token if block_given?
83
+ end
86
84
  end
87
-
88
- result
89
85
  end
90
86
  })
91
87
 
92
88
  self.field_types.update(
93
- array: Fields::Array,
94
- boolean: Fields::Boolean,
95
- connection: Fields::Connection,
96
- float: Fields::Float,
97
- integer: Fields::Integer,
98
- object: Fields::Object,
99
- string: Fields::String
89
+ array: Array,
90
+ boolean: Boolean,
91
+ connection: Connection,
92
+ number: Number,
93
+ object: Object,
94
+ string: String
100
95
  )
101
96
  end
data/lib/gql/array.rb ADDED
@@ -0,0 +1,36 @@
1
+ require 'active_support/core_ext/class/attribute'
2
+
3
+ module GQL
4
+ class Array < Field
5
+ class_attribute :item_class, instance_accessor: false, instance_predicate: false
6
+
7
+ class << self
8
+ def build_class(id, method, options = {})
9
+ item_class = options[:item_class] || self.item_class
10
+
11
+ if item_class.nil?
12
+ raise Errors::UndefinedNodeClass.new(self, 'item')
13
+ end
14
+
15
+ unless item_class <= Node
16
+ raise Errors::InvalidNodeClass.new(item_class, Node)
17
+ end
18
+
19
+ Class.new(self).tap do |field_class|
20
+ field_class.id = id.to_s
21
+ field_class.method = method
22
+ field_class.item_class = item_class
23
+ end
24
+ end
25
+ end
26
+
27
+ call :size, Number, -> { target.size }
28
+
29
+ def value
30
+ target.map do |item|
31
+ node = self.class.item_class.new(ast_node, item, variables, context)
32
+ node.value
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,4 @@
1
+ module GQL
2
+ class Boolean < Simple
3
+ end
4
+ end
data/lib/gql/call.rb CHANGED
@@ -16,18 +16,23 @@ module GQL
16
16
 
17
17
  class << self
18
18
  def build_class(id, result_class, method)
19
- if result_class.is_a? Array
20
- result_class.unshift Connection if result_class.size == 1
21
- result_class.unshift Fields::Connection if result_class.size == 2
19
+ if result_class.is_a? ::Array
20
+ if result_class.size == 1
21
+ result_class.unshift GQL.default_list_class || Connection
22
+ end
23
+
24
+ list_class, item_class = result_class
22
25
 
23
- field_type_class, connection_class, node_class = result_class
26
+ unless list_class <= Connection
27
+ raise Errors::InvalidNodeClass.new(list_class, Connection)
28
+ end
24
29
 
25
- unless field_type_class <= Fields::Connection
26
- raise Errors::InvalidNodeClass.new(field_type_class, Fields::Connection)
30
+ unless item_class <= Node
31
+ raise Errors::InvalidNodeClass.new(list_class, Node)
27
32
  end
28
33
 
29
- options = { connection_class: connection_class, node_class: node_class }
30
- result_class = field_type_class.build_class(:result, nil, options)
34
+ options = { list_class: list_class, item_class: item_class }
35
+ result_class = Connection.build_class(:result, nil, options)
31
36
  elsif result_class && !(result_class <= Node)
32
37
  raise Errors::InvalidNodeClass.new(result_class, Node)
33
38
  end
@@ -60,7 +65,7 @@ module GQL
60
65
 
61
66
  private
62
67
  def substitute_variables(args)
63
- args.map { |arg| arg.is_a?(Symbol) ? variables[arg] : arg }
68
+ args.map { |arg| arg.is_a?(::Symbol) ? variables[arg] : arg }
64
69
  end
65
70
  end
66
71
  end
data/lib/gql/config.rb CHANGED
@@ -23,5 +23,13 @@ module GQL
23
23
  def field_types=(value)
24
24
  @@field_types = value
25
25
  end
26
+
27
+ def default_list_class
28
+ @@default_list_class ||= Connection
29
+ end
30
+
31
+ def default_list_class=(value)
32
+ @@default_list_class = value
33
+ end
26
34
  end
27
35
  end
@@ -1,15 +1,36 @@
1
1
  require 'active_support/core_ext/class/attribute'
2
2
 
3
3
  module GQL
4
- class Connection < Node
5
- class_attribute :node_class, instance_accessor: false, instance_predicate: false
4
+ class Connection < Field
5
+ class_attribute :list_class, instance_accessor: false, instance_predicate: false
6
+ class_attribute :item_class, instance_accessor: false, instance_predicate: false
6
7
 
7
8
  class << self
8
- def build_class(node_class)
9
- node_class ||= self.node_class
9
+ def build_class(id, method, options = {})
10
+ list_class = options[:list_class] || self.list_class || GQL.default_list_class
11
+ item_class = options[:item_class] || self.item_class
10
12
 
11
- Class.new(self).tap do |connection_class|
12
- connection_class.array :edges, node_class: node_class do
13
+ if list_class.nil?
14
+ raise Errors::UndefinedNodeClass.new(self, 'list')
15
+ end
16
+
17
+ unless list_class <= Connection
18
+ raise Errors::InvalidNodeClass.new(list_class, Connection)
19
+ end
20
+
21
+ if item_class.nil?
22
+ raise Errors::UndefinedNodeClass.new(self, 'item')
23
+ end
24
+
25
+ unless item_class <= Node
26
+ raise Errors::InvalidNodeClass.new(item_class, Node)
27
+ end
28
+
29
+ Class.new(list_class).tap do |field_class|
30
+ field_class.id = id.to_s
31
+ field_class.method = method
32
+
33
+ field_class.array :edges, item_class: item_class do
13
34
  target
14
35
  end
15
36
  end
data/lib/gql/field.rb CHANGED
@@ -24,9 +24,5 @@ module GQL
24
24
  end
25
25
  end
26
26
  end
27
-
28
- def raw_value
29
- target
30
- end
31
27
  end
32
28
  end
data/lib/gql/node.rb CHANGED
@@ -29,13 +29,13 @@ module GQL
29
29
 
30
30
  ids.each do |id|
31
31
  method = block || lambda { target.public_send(id) }
32
- field_type_class = options.delete(:field_type_class) || Field
32
+ field_type = options.delete(:type) || Field
33
33
 
34
- unless field_type_class <= Field
35
- raise Errors::InvalidNodeClass.new(field_type_class, Field)
34
+ unless field_type <= Field
35
+ raise Errors::InvalidNodeClass.new(field_type, Field)
36
36
  end
37
37
 
38
- field_class = field_type_class.build_class(id, method, options)
38
+ field_class = field_type.build_class(id, method, options)
39
39
 
40
40
  self.const_set "#{id.to_s.camelize}Field", field_class
41
41
  self.fields = fields.merge(id.to_sym => field_class)
@@ -43,18 +43,15 @@ module GQL
43
43
  end
44
44
 
45
45
  def cursor(id = nil, &block)
46
- if id
47
- field :cursor, &-> { target.public_send(id) }
48
- elsif block_given?
49
- field :cursor, &block
50
- end
46
+ body = id ? -> { target.public_send(id) } : block
47
+ field :cursor, { type: Simple }, &body
51
48
  end
52
49
 
53
50
  def method_missing(method, *ids, &block)
54
- if field_type_class = GQL.field_types[method]
51
+ if field_type = GQL.field_types[method]
55
52
  options = ids.extract_options!
56
53
 
57
- field(*ids, options.merge(field_type_class: field_type_class), &block)
54
+ field(*ids, options.merge(type: field_type), &block)
58
55
  else
59
56
  super
60
57
  end
data/lib/gql/number.rb ADDED
@@ -0,0 +1,6 @@
1
+ module GQL
2
+ class Number < Simple
3
+ # This is just an example call. Monkeypatch class to add your own.
4
+ call :is_zero, Boolean, -> { target.zero? }
5
+ end
6
+ end
data/lib/gql/object.rb ADDED
@@ -0,0 +1,32 @@
1
+ require 'active_support/core_ext/class/attribute'
2
+
3
+ module GQL
4
+ class Object < Field
5
+ class_attribute :node_class, instance_accessor: false, instance_predicate: false
6
+
7
+ class << self
8
+ def build_class(id, method, options = {})
9
+ node_class = options[:node_class] || self.node_class
10
+
11
+ if node_class.nil?
12
+ raise Errors::UndefinedNodeClass.new(self, 'node')
13
+ end
14
+
15
+ unless node_class <= Node
16
+ raise Errors::InvalidNodeClass.new(node_class, Node)
17
+ end
18
+
19
+ Class.new(self).tap do |field_class|
20
+ field_class.id = id.to_s
21
+ field_class.method = method
22
+ field_class.node_class = node_class
23
+ end
24
+ end
25
+ end
26
+
27
+ def value
28
+ node = self.class.node_class.new(ast_node, target, variables, context)
29
+ node.value
30
+ end
31
+ end
32
+ end
@@ -4,7 +4,7 @@ module GQL
4
4
  cursor :id
5
5
  string :id
6
6
 
7
- array :parameters, :node_class => Parameter do
7
+ array :parameters, :item_class => Parameter do
8
8
  target.method.parameters
9
9
  end
10
10
 
@@ -8,11 +8,11 @@ module GQL
8
8
  target.name
9
9
  end
10
10
 
11
- connection :calls, :connection_class => Connection, :node_class => Call do
11
+ connection :calls, :list_class => List, :item_class => Call do
12
12
  target.calls.values
13
13
  end
14
14
 
15
- connection :fields, :connection_class => Connection, :node_class => Field do
15
+ connection :fields, :list_class => List, :item_class => Field do
16
16
  target.fields.values
17
17
  end
18
18
  end
@@ -0,0 +1,10 @@
1
+ module GQL
2
+ module Schema
3
+ class List < GQL::Connection
4
+ number :count
5
+
6
+ call :reverse
7
+ call :first, -> size { target.first(size) }
8
+ end
9
+ end
10
+ end
@@ -5,11 +5,11 @@ module GQL
5
5
  target.name
6
6
  end
7
7
 
8
- connection :calls, :connection_class => Connection, :node_class => Call do
8
+ connection :calls, :list_class => List, :item_class => Call do
9
9
  target.calls.values
10
10
  end
11
11
 
12
- connection :fields, :connection_class => Connection, :node_class => Field do
12
+ connection :fields, :list_class => List, :item_class => Field do
13
13
  target.fields.values
14
14
  end
15
15
  end
data/lib/gql/simple.rb ADDED
@@ -0,0 +1,7 @@
1
+ module GQL
2
+ class Simple < Field
3
+ def raw_value
4
+ target
5
+ end
6
+ end
7
+ end
data/lib/gql/string.rb ADDED
@@ -0,0 +1,8 @@
1
+ module GQL
2
+ class String < Simple
3
+ # These are just example calls. Monkeypatch class to add your own.
4
+ call :upcase, -> { target.upcase }
5
+ call :downcase, -> { target.downcase }
6
+ call :length, Number, -> { target.size }
7
+ end
8
+ end
data/lib/gql/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module GQL
2
- VERSION = '0.0.5'
2
+ VERSION = '0.0.6'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gql
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Martin Andert
@@ -110,28 +110,27 @@ files:
110
110
  - bin/setup
111
111
  - gql.gemspec
112
112
  - lib/gql.rb
113
+ - lib/gql/array.rb
114
+ - lib/gql/boolean.rb
113
115
  - lib/gql/call.rb
114
116
  - lib/gql/config.rb
115
117
  - lib/gql/connection.rb
116
118
  - lib/gql/errors.rb
117
119
  - lib/gql/executor.rb
118
120
  - lib/gql/field.rb
119
- - lib/gql/fields/array.rb
120
- - lib/gql/fields/boolean.rb
121
- - lib/gql/fields/connection.rb
122
- - lib/gql/fields/float.rb
123
- - lib/gql/fields/integer.rb
124
- - lib/gql/fields/object.rb
125
- - lib/gql/fields/string.rb
126
121
  - lib/gql/node.rb
122
+ - lib/gql/number.rb
123
+ - lib/gql/object.rb
127
124
  - lib/gql/parser.rb
128
125
  - lib/gql/parser.y
129
126
  - lib/gql/schema/call.rb
130
- - lib/gql/schema/connection.rb
131
127
  - lib/gql/schema/field.rb
128
+ - lib/gql/schema/list.rb
132
129
  - lib/gql/schema/node.rb
133
130
  - lib/gql/schema/parameter.rb
134
131
  - lib/gql/schema/placeholder.rb
132
+ - lib/gql/simple.rb
133
+ - lib/gql/string.rb
135
134
  - lib/gql/tokenizer.rb
136
135
  - lib/gql/tokenizer.rex
137
136
  - lib/gql/version.rb
@@ -1,42 +0,0 @@
1
- require 'active_support/core_ext/class/attribute'
2
-
3
- module GQL
4
- module Fields
5
- class Array < Field
6
- class_attribute :node_class, instance_accessor: false, instance_predicate: false
7
-
8
- class << self
9
- def build_class(id, method, options = {})
10
- node_class = options[:node_class] || self.node_class
11
-
12
- if node_class.nil?
13
- raise Errors::UndefinedNodeClass.new(self, 'node')
14
- end
15
-
16
- unless node_class <= GQL::Node
17
- raise Errors::InvalidNodeClass.new(node_class, GQL::Node)
18
- end
19
-
20
- Class.new(self).tap do |field_class|
21
- field_class.id = id.to_s
22
- field_class.method = method
23
- field_class.node_class = node_class
24
- end
25
- end
26
- end
27
-
28
- call :size, Integer, -> { target.size }
29
-
30
- def value
31
- target.map do |item|
32
- node = self.class.node_class.new(ast_node, item, variables, context)
33
- node.value
34
- end
35
- end
36
-
37
- def raw_value
38
- nil
39
- end
40
- end
41
- end
42
- end
@@ -1,6 +0,0 @@
1
- module GQL
2
- module Fields
3
- class Boolean < Field
4
- end
5
- end
6
- end
@@ -1,39 +0,0 @@
1
- require 'active_support/core_ext/class/attribute'
2
-
3
- module GQL
4
- module Fields
5
- class Connection < Field
6
- class_attribute :connection_class, instance_accessor: false, instance_predicate: false
7
- self.connection_class = GQL::Connection
8
-
9
- class << self
10
- def build_class(id, method, options = {})
11
- connection_class = options[:connection_class] || self.connection_class
12
-
13
- if connection_class.nil?
14
- raise Errors::UndefinedNodeClass.new(self, 'connection')
15
- end
16
-
17
- unless connection_class <= GQL::Connection
18
- raise Errors::InvalidNodeClass.new(connection_class, GQL::Connection)
19
- end
20
-
21
- Class.new(self).tap do |field_class|
22
- field_class.id = id.to_s
23
- field_class.method = method
24
- field_class.connection_class = connection_class.build_class(options[:node_class])
25
- end
26
- end
27
- end
28
-
29
- def value_of_fields(*)
30
- connection = self.class.connection_class.new(ast_node, target, variables, context)
31
- connection.value
32
- end
33
-
34
- def raw_value
35
- nil
36
- end
37
- end
38
- end
39
- end
@@ -1,6 +0,0 @@
1
- module GQL
2
- module Fields
3
- class Float < Field
4
- end
5
- end
6
- end
@@ -1,8 +0,0 @@
1
- module GQL
2
- module Fields
3
- class Integer < Field
4
- # This is just an example call, monkeypatch to add your own.
5
- call :is_zero, Boolean, -> { target.zero? }
6
- end
7
- end
8
- end
@@ -1,38 +0,0 @@
1
- require 'active_support/core_ext/class/attribute'
2
-
3
- module GQL
4
- module Fields
5
- class Object < Field
6
- class_attribute :node_class, instance_accessor: false, instance_predicate: false
7
-
8
- class << self
9
- def build_class(id, method, options = {})
10
- node_class = options[:node_class] || self.node_class
11
-
12
- if node_class.nil?
13
- raise Errors::UndefinedNodeClass.new(self, 'node')
14
- end
15
-
16
- unless node_class <= GQL::Node
17
- raise Errors::InvalidNodeClass.new(node_class, GQL::Node)
18
- end
19
-
20
- Class.new(self).tap do |field_class|
21
- field_class.id = id.to_s
22
- field_class.method = method
23
- field_class.node_class = node_class
24
- end
25
- end
26
- end
27
-
28
- def value
29
- node = self.class.node_class.new(ast_node, target, variables, context)
30
- node.value
31
- end
32
-
33
- def raw_value
34
- nil
35
- end
36
- end
37
- end
38
- end
@@ -1,11 +0,0 @@
1
- module GQL
2
- module Fields
3
- class String < Field
4
- call :upcase, -> { target.upcase }
5
- call :downcase, -> { target.downcase }
6
- call :length, Integer, -> { target.size }
7
-
8
- # These are just example calls, monkeypatch to add your own.
9
- end
10
- end
11
- end
@@ -1,7 +0,0 @@
1
- module GQL
2
- module Schema
3
- class Connection < GQL::Connection
4
- integer :count
5
- end
6
- end
7
- end