graphql-rb 0.0.2 → 0.0.3
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/graphql/execution/pool.rb +34 -0
- data/lib/graphql/executor.rb +13 -45
- data/lib/graphql/language/selection_set.rb +47 -22
- data/lib/graphql/version.rb +1 -1
- data/lib/graphql.rb +6 -5
- metadata +5 -4
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA1:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: e7b3163d3f0025e46cd6b51b90b2e4816e15ee36
|
|
4
|
+
data.tar.gz: 7fb736049261de9c5051281f51c2088cbc217076
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: ce7bd2ef2e8c9cf05288df3258f05f5d2dc73ac50cb4f4bf152fab41d643c7d4e021be3ea62de19618bcd92566760f223c846eb7f06d3b6265b7d7cc445ceddf
|
|
7
|
+
data.tar.gz: d54360b65c917072a7eca4e2b7982da20075102b34b9b9a6636873cd6e68b764b2fd3e0896d17273228a60f4adbd46b171456e408b3ca536c579bd84b05ed05d
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
require 'celluloid/current'
|
|
2
|
+
|
|
3
|
+
module GraphQL
|
|
4
|
+
module Execution
|
|
5
|
+
|
|
6
|
+
class Pool
|
|
7
|
+
def self.future(&block)
|
|
8
|
+
_pool.future.perform(block)
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
protected
|
|
12
|
+
|
|
13
|
+
class Worker
|
|
14
|
+
include Celluloid
|
|
15
|
+
|
|
16
|
+
def perform(block)
|
|
17
|
+
value = block.call
|
|
18
|
+
value = value.value if value.is_a?(Celluloid::Future)
|
|
19
|
+
value
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def self._pool
|
|
24
|
+
Celluloid::Actor[pool_id] ||= Worker.pool
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def self.pool_id
|
|
28
|
+
@pool_id ||= SecureRandom.uuid
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
end
|
|
34
|
+
end
|
data/lib/graphql/executor.rb
CHANGED
|
@@ -1,42 +1,6 @@
|
|
|
1
|
-
require 'celluloid/current'
|
|
2
|
-
require 'singleton'
|
|
3
|
-
|
|
4
1
|
module GraphQL
|
|
5
2
|
class Executor
|
|
6
3
|
|
|
7
|
-
class FutureCompleter
|
|
8
|
-
include Celluloid
|
|
9
|
-
include Singleton
|
|
10
|
-
|
|
11
|
-
def self.complete_value(context, field_type, resolved_object, selection_set)
|
|
12
|
-
completer = resolved_object.is_a?(Celluloid::Future) ? instance.future : instance
|
|
13
|
-
completer.complete_value(context, field_type, resolved_object, selection_set)
|
|
14
|
-
end
|
|
15
|
-
|
|
16
|
-
def complete_value(context, field_type, resolved_object, selection_set)
|
|
17
|
-
return nil if resolved_object.nil?
|
|
18
|
-
|
|
19
|
-
resolved_object = resolved_object.value if resolved_object.is_a?(Celluloid::Future)
|
|
20
|
-
|
|
21
|
-
case field_type
|
|
22
|
-
when GraphQLNonNull
|
|
23
|
-
completed_object = complete_value(context, field_type.of_type, resolved_object, selection_set)
|
|
24
|
-
raise "Field error: expecting non null value" if completed_object.nil?
|
|
25
|
-
completed_object
|
|
26
|
-
when GraphQLList
|
|
27
|
-
resolved_object.map do |item|
|
|
28
|
-
complete_value(context, field_type.of_type, item, selection_set)
|
|
29
|
-
end
|
|
30
|
-
when GraphQLScalarType, GraphQLEnumType
|
|
31
|
-
field_type.serialize(resolved_object)
|
|
32
|
-
when GraphQLObjectType, GraphQLInterfaceType, GraphQLUnionType
|
|
33
|
-
field_type = field_type.resolve_type(resolved_object) if field_type.is_a?(GraphQLAbstractType)
|
|
34
|
-
selection_set.evaluate(context, field_type, resolved_object)
|
|
35
|
-
end
|
|
36
|
-
|
|
37
|
-
end
|
|
38
|
-
end
|
|
39
|
-
|
|
40
4
|
attr_reader :document, :schema, :context
|
|
41
5
|
|
|
42
6
|
def initialize(document, schema)
|
|
@@ -44,7 +8,8 @@ module GraphQL
|
|
|
44
8
|
@schema = schema
|
|
45
9
|
@context = {
|
|
46
10
|
document: document,
|
|
47
|
-
schema: schema
|
|
11
|
+
schema: schema,
|
|
12
|
+
errors: []
|
|
48
13
|
}
|
|
49
14
|
end
|
|
50
15
|
|
|
@@ -60,23 +25,26 @@ module GraphQL
|
|
|
60
25
|
context[:root] = root
|
|
61
26
|
context[:params] = params
|
|
62
27
|
|
|
63
|
-
materialize(operation.evaluate(context))
|
|
28
|
+
return materialize(operation.evaluate(context)), context[:errors]
|
|
64
29
|
end
|
|
65
30
|
|
|
66
31
|
def materialize(data)
|
|
67
32
|
case data
|
|
33
|
+
when Celluloid::Future
|
|
34
|
+
materialize(data.value)
|
|
68
35
|
when Hash
|
|
69
|
-
data.
|
|
70
|
-
|
|
71
|
-
|
|
36
|
+
data.reduce({}) do |memo, pair|
|
|
37
|
+
memo[pair.first] = materialize(pair.last)
|
|
38
|
+
memo
|
|
72
39
|
end
|
|
73
40
|
when Array
|
|
74
|
-
data.each_with_index do |
|
|
75
|
-
|
|
76
|
-
|
|
41
|
+
data.each_with_index.reduce([]) do |memo, pair|
|
|
42
|
+
memo[pair.last] = materialize(pair.first)
|
|
43
|
+
memo
|
|
77
44
|
end
|
|
45
|
+
else
|
|
46
|
+
data
|
|
78
47
|
end
|
|
79
|
-
data
|
|
80
48
|
end
|
|
81
49
|
|
|
82
50
|
end
|
|
@@ -12,33 +12,32 @@ module GraphQL
|
|
|
12
12
|
# objectType, object, - fields
|
|
13
13
|
# + context[schema, document]
|
|
14
14
|
#
|
|
15
|
-
# TODO: think of way to have error accessor at this point. Executor?
|
|
16
15
|
#
|
|
17
16
|
def evaluate(context, object_type, object)
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
#
|
|
21
|
-
# TODO: Merge equal fields
|
|
22
|
-
#
|
|
23
|
-
|
|
24
|
-
grouped_fields.reduce({}) do |memo, (key, fields)|
|
|
25
|
-
field = fields.first
|
|
26
|
-
field_definition = case
|
|
27
|
-
when GraphQL::Introspection.meta_field?(field.name)
|
|
28
|
-
GraphQL::Introspection.meta_field(field.name)
|
|
29
|
-
else
|
|
30
|
-
object_type.field(field.name)
|
|
31
|
-
end
|
|
17
|
+
Execution::Pool.future do
|
|
32
18
|
|
|
33
|
-
|
|
34
|
-
|
|
19
|
+
collect_fields(context, object_type).reduce({}) do |memo, (key, fields)|
|
|
20
|
+
field = fields.first
|
|
21
|
+
field_definition = case
|
|
35
22
|
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
23
|
+
when GraphQL::Introspection.meta_field?(field.name)
|
|
24
|
+
GraphQL::Introspection.meta_field(field.name)
|
|
25
|
+
else
|
|
26
|
+
object_type.field(field.name)
|
|
27
|
+
end
|
|
40
28
|
|
|
41
|
-
|
|
29
|
+
|
|
30
|
+
memo[key.to_sym] = begin
|
|
31
|
+
resolution_context = context.merge({ parent_type: object_type })
|
|
32
|
+
resolved_object = field.resolve(resolution_context, object_type, object)
|
|
33
|
+
complete_value(context, field_definition.type, resolved_object, merge_selection_sets(fields))
|
|
34
|
+
rescue Celluloid::TaskTerminated => e
|
|
35
|
+
context[:errors] << "Field '#{field.name}' of '#{object_type}' error: #{e}"
|
|
36
|
+
nil
|
|
37
|
+
end unless field_definition.nil?
|
|
38
|
+
|
|
39
|
+
memo
|
|
40
|
+
end
|
|
42
41
|
end
|
|
43
42
|
end
|
|
44
43
|
|
|
@@ -102,6 +101,32 @@ module GraphQL
|
|
|
102
101
|
SelectionSet.new(selections)
|
|
103
102
|
end
|
|
104
103
|
|
|
104
|
+
|
|
105
|
+
def complete_value(context, field_type, resolved_object, selection_set)
|
|
106
|
+
return nil if resolved_object.nil?
|
|
107
|
+
|
|
108
|
+
return Execution::Pool.future do
|
|
109
|
+
complete_value(context, field_type, resolved_object.value, selection_set)
|
|
110
|
+
end if resolved_object.is_a?(Celluloid::Future)
|
|
111
|
+
|
|
112
|
+
case field_type
|
|
113
|
+
when GraphQLNonNull
|
|
114
|
+
completed_object = complete_value(context, field_type.of_type, resolved_object, selection_set)
|
|
115
|
+
raise "Field error: expecting non null value" if completed_object.nil?
|
|
116
|
+
completed_object
|
|
117
|
+
when GraphQLList
|
|
118
|
+
resolved_object.map do |item|
|
|
119
|
+
complete_value(context, field_type.of_type, item, selection_set)
|
|
120
|
+
end
|
|
121
|
+
when GraphQLScalarType, GraphQLEnumType
|
|
122
|
+
field_type.serialize(resolved_object)
|
|
123
|
+
when GraphQLObjectType, GraphQLInterfaceType, GraphQLUnionType
|
|
124
|
+
field_type = field_type.resolve_type(resolved_object) if field_type.is_a?(GraphQLAbstractType)
|
|
125
|
+
selection_set.evaluate(context, field_type, resolved_object)
|
|
126
|
+
end
|
|
127
|
+
|
|
128
|
+
end
|
|
129
|
+
|
|
105
130
|
end
|
|
106
131
|
end
|
|
107
132
|
end
|
data/lib/graphql/version.rb
CHANGED
data/lib/graphql.rb
CHANGED
|
@@ -6,14 +6,15 @@ require 'graphql/language'
|
|
|
6
6
|
require 'graphql/version'
|
|
7
7
|
require 'graphql/executor'
|
|
8
8
|
require 'graphql/validator'
|
|
9
|
+
require 'graphql/execution/pool'
|
|
9
10
|
|
|
10
11
|
module GraphQL
|
|
11
12
|
|
|
12
|
-
def self.graphql(schema, query, root, params, operation = nil)
|
|
13
|
-
document
|
|
14
|
-
executor
|
|
15
|
-
result
|
|
16
|
-
{ data: result }
|
|
13
|
+
def self.graphql(schema, query, root = nil, params = {}, operation = nil)
|
|
14
|
+
document = GraphQL::Language.parse(query)
|
|
15
|
+
executor = GraphQL::Executor.new(document, schema)
|
|
16
|
+
result, errors = executor.execute(root, params, operation)
|
|
17
|
+
{ data: result }.tap { |result| result[:errors] = errors unless errors.empty? }
|
|
17
18
|
rescue StandardError => e
|
|
18
19
|
{ errors: [e] }
|
|
19
20
|
end
|
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: graphql-rb
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.0.
|
|
4
|
+
version: 0.0.3
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Eugene Kovalev
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2015-08-
|
|
11
|
+
date: 2015-08-20 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: rake
|
|
@@ -109,6 +109,7 @@ files:
|
|
|
109
109
|
- lib/graphql/configuration/slot.rb
|
|
110
110
|
- lib/graphql/errors.rb
|
|
111
111
|
- lib/graphql/errors/error.rb
|
|
112
|
+
- lib/graphql/execution/pool.rb
|
|
112
113
|
- lib/graphql/executor.rb
|
|
113
114
|
- lib/graphql/introspection.rb
|
|
114
115
|
- lib/graphql/introspection/meta_fields.rb
|
|
@@ -172,9 +173,9 @@ require_paths:
|
|
|
172
173
|
- lib
|
|
173
174
|
required_ruby_version: !ruby/object:Gem::Requirement
|
|
174
175
|
requirements:
|
|
175
|
-
- - "
|
|
176
|
+
- - "~>"
|
|
176
177
|
- !ruby/object:Gem::Version
|
|
177
|
-
version: '0'
|
|
178
|
+
version: '2.0'
|
|
178
179
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
179
180
|
requirements:
|
|
180
181
|
- - ">="
|