relay-rb 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/relay/node/composite.rb +42 -0
- data/lib/relay/node.rb +2 -31
- data/lib/relay/version.rb +1 -1
- data/spec/node/data.rb +4 -4
- data/spec/node/data_global.rb +5 -5
- data/spec/star_wars/schema.rb +13 -32
- 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: 5a49741538c409a6e3ee34d698b4f2622dad3ce1
|
4
|
+
data.tar.gz: d0d793be807f2827b2d269bd91159e912cfa1d05
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: dd78e0f67fb94f95b719fb629061969e8ee4fa0601d2176d4e54c5501cd16afc7ce973ea48c0a69d9c04823b074b3f53c6eaf365b594f014fc190b14c079155d
|
7
|
+
data.tar.gz: a5bad862afe77abb1584245ac014a661e74d48cf0801a7d8ece33f7201aba9322dfe7924b00305b786e9d7eab9c175cdb24bd823d14c275dc0ba1151f8c9643f
|
@@ -0,0 +1,42 @@
|
|
1
|
+
module Relay
|
2
|
+
module Node
|
3
|
+
|
4
|
+
class CompositeTypeConfiguration < GraphQL::Configuration::Base
|
5
|
+
slot :fetch_object, Proc
|
6
|
+
slot :resolve_type, Proc
|
7
|
+
end
|
8
|
+
|
9
|
+
class CompositeType < GraphQL::Configuration::Configurable
|
10
|
+
configure_with CompositeTypeConfiguration
|
11
|
+
|
12
|
+
def fetch_object
|
13
|
+
@fetch_object ||= lambda { |object, params, context|
|
14
|
+
@configuration.fetch_object.call(params[:id], context)
|
15
|
+
}
|
16
|
+
end
|
17
|
+
|
18
|
+
def resolve_type
|
19
|
+
@configuration.resolve_type
|
20
|
+
end
|
21
|
+
|
22
|
+
def field
|
23
|
+
@field ||= GraphQL::GraphQLField.new(type: interface, resolve: fetch_object) do
|
24
|
+
name 'node'
|
25
|
+
description 'An object with id.'
|
26
|
+
|
27
|
+
arg :id, !GraphQL::GraphQLID
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def interface
|
32
|
+
@interface ||= GraphQL::GraphQLInterfaceType.new(resolve_type: resolve_type) do
|
33
|
+
name 'NodeInterface'
|
34
|
+
description 'A node with id.'
|
35
|
+
|
36
|
+
field :id, ! GraphQL::GraphQLID
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
end
|
42
|
+
end
|
data/lib/relay/node.rb
CHANGED
@@ -1,42 +1,13 @@
|
|
1
|
-
require 'graphql'
|
2
1
|
require 'base64'
|
2
|
+
|
3
3
|
require_relative 'node/global_id_field'
|
4
4
|
require_relative 'node/plural_identifying_root_field'
|
5
|
+
require_relative 'node/composite'
|
5
6
|
|
6
7
|
module Relay
|
7
8
|
|
8
9
|
module Node
|
9
10
|
|
10
|
-
def self.definitions(fetcher, resolver)
|
11
|
-
|
12
|
-
interface = GraphQL::GraphQLInterfaceType.new do
|
13
|
-
name 'Node'
|
14
|
-
description 'An object with ID'
|
15
|
-
|
16
|
-
field :id, ! GraphQL::GraphQLID do
|
17
|
-
description 'The id of the object'
|
18
|
-
end
|
19
|
-
|
20
|
-
resolve_type resolver
|
21
|
-
end
|
22
|
-
|
23
|
-
field = GraphQL::GraphQLField.new do
|
24
|
-
name 'node'
|
25
|
-
description 'Fetches an object given its ID'
|
26
|
-
type interface
|
27
|
-
|
28
|
-
arg :id, ! GraphQL::GraphQLID do
|
29
|
-
description 'The ID of an object'
|
30
|
-
end
|
31
|
-
|
32
|
-
resolve lambda { |root, params, info, *args|
|
33
|
-
fetcher.call(params[:id], info)
|
34
|
-
}
|
35
|
-
end
|
36
|
-
|
37
|
-
{ interface: interface, field: field }
|
38
|
-
end
|
39
|
-
|
40
11
|
def self.to_global_id(type, id)
|
41
12
|
Base64.strict_encode64([type, id].join(':'))
|
42
13
|
end
|
data/lib/relay/version.rb
CHANGED
data/spec/node/data.rb
CHANGED
@@ -35,7 +35,7 @@ module Relay
|
|
35
35
|
end
|
36
36
|
}
|
37
37
|
|
38
|
-
definitions = Relay::Node.
|
38
|
+
definitions = Relay::Node::CompositeType.new(fetch_object: fetcher, resolve_type: resolver)
|
39
39
|
|
40
40
|
UserType = GraphQL::GraphQLObjectType.new do
|
41
41
|
name 'User'
|
@@ -43,7 +43,7 @@ module Relay
|
|
43
43
|
field :id, ! GraphQL::GraphQLID
|
44
44
|
field :name, GraphQL::GraphQLString
|
45
45
|
|
46
|
-
interface definitions
|
46
|
+
interface definitions.interface
|
47
47
|
end
|
48
48
|
|
49
49
|
PhotoType = GraphQL::GraphQLObjectType.new do
|
@@ -52,13 +52,13 @@ module Relay
|
|
52
52
|
field :id, ! GraphQL::GraphQLID
|
53
53
|
field :width, GraphQL::GraphQLInt
|
54
54
|
|
55
|
-
interface definitions
|
55
|
+
interface definitions.interface
|
56
56
|
end
|
57
57
|
|
58
58
|
QueryType = GraphQL::GraphQLObjectType.new do
|
59
59
|
name 'Query'
|
60
60
|
|
61
|
-
field definitions
|
61
|
+
field definitions.field
|
62
62
|
end
|
63
63
|
|
64
64
|
Schema = GraphQL::GraphQLSchema.new do
|
data/spec/node/data_global.rb
CHANGED
@@ -36,7 +36,7 @@ module Relay
|
|
36
36
|
end
|
37
37
|
}
|
38
38
|
|
39
|
-
definitions = Relay::Node.
|
39
|
+
definitions = Relay::Node::CompositeType.new(fetch_object: fetcher, resolve_type: resolver)
|
40
40
|
|
41
41
|
UserType = GraphQL::GraphQLObjectType.new do
|
42
42
|
name 'User'
|
@@ -45,7 +45,7 @@ module Relay
|
|
45
45
|
|
46
46
|
field :name, GraphQL::GraphQLString
|
47
47
|
|
48
|
-
interface definitions
|
48
|
+
interface definitions.interface
|
49
49
|
end
|
50
50
|
|
51
51
|
PhotoType = GraphQL::GraphQLObjectType.new do
|
@@ -55,15 +55,15 @@ module Relay
|
|
55
55
|
|
56
56
|
field :width, GraphQL::GraphQLInt
|
57
57
|
|
58
|
-
interface definitions
|
58
|
+
interface definitions.interface
|
59
59
|
end
|
60
60
|
|
61
61
|
QueryType = GraphQL::GraphQLObjectType.new do
|
62
62
|
name 'Query'
|
63
63
|
|
64
|
-
field definitions
|
64
|
+
field definitions.field
|
65
65
|
|
66
|
-
field :all, + definitions
|
66
|
+
field :all, + definitions.interface do
|
67
67
|
resolve lambda { |*args|
|
68
68
|
Users.values.concat(Photos.values)
|
69
69
|
}
|
data/spec/star_wars/schema.rb
CHANGED
@@ -4,12 +4,17 @@ require_relative 'data'
|
|
4
4
|
|
5
5
|
module StarWars
|
6
6
|
|
7
|
+
NodeComposite = Relay::Node::CompositeType.new do
|
7
8
|
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
9
|
+
fetch_object lambda { |global_id, context|
|
10
|
+
type, id = Relay::Node.from_global_id(global_id)
|
11
|
+
case type
|
12
|
+
when 'Faction'
|
13
|
+
Data.faction(id.to_i)
|
14
|
+
when 'Ship'
|
15
|
+
Data.ship(id.to_i)
|
16
|
+
end
|
17
|
+
}
|
13
18
|
|
14
19
|
resolve_type lambda { |object|
|
15
20
|
case object
|
@@ -17,33 +22,9 @@ module StarWars
|
|
17
22
|
FactionType
|
18
23
|
when Data::Ship
|
19
24
|
ShipType
|
20
|
-
else
|
21
|
-
nil
|
22
25
|
end
|
23
26
|
}
|
24
|
-
end
|
25
|
-
|
26
27
|
|
27
|
-
NodeField = GraphQL::GraphQLField.new do
|
28
|
-
name 'node'
|
29
|
-
description 'Fetches an object given its global id.'
|
30
|
-
type NodeInterface
|
31
|
-
|
32
|
-
arg :id, ! GraphQL::GraphQLID, description: 'The global id of the object.'
|
33
|
-
|
34
|
-
resolve lambda { |object, params, context|
|
35
|
-
type, id = Relay::Node.from_global_id(params[:id])
|
36
|
-
id = id.to_i
|
37
|
-
|
38
|
-
case type
|
39
|
-
when 'Faction'
|
40
|
-
Data.faction(id)
|
41
|
-
when 'Ship'
|
42
|
-
Data.ship(id)
|
43
|
-
else
|
44
|
-
nil
|
45
|
-
end
|
46
|
-
}
|
47
28
|
end
|
48
29
|
|
49
30
|
|
@@ -73,7 +54,7 @@ module StarWars
|
|
73
54
|
|
74
55
|
field :name, GraphQL::GraphQLString, description: 'The name of the ship.'
|
75
56
|
|
76
|
-
interface
|
57
|
+
interface NodeComposite.interface
|
77
58
|
end
|
78
59
|
|
79
60
|
|
@@ -93,7 +74,7 @@ module StarWars
|
|
93
74
|
}
|
94
75
|
end
|
95
76
|
|
96
|
-
interface
|
77
|
+
interface NodeComposite.interface
|
97
78
|
end
|
98
79
|
|
99
80
|
|
@@ -104,7 +85,7 @@ module StarWars
|
|
104
85
|
|
105
86
|
field :empire, FactionType, resolve: -> { Data.empire }
|
106
87
|
|
107
|
-
field
|
88
|
+
field NodeComposite.field
|
108
89
|
end
|
109
90
|
|
110
91
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: relay-rb
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
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-19 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rake
|
@@ -64,6 +64,7 @@ files:
|
|
64
64
|
- lib/relay/connection/array.rb
|
65
65
|
- lib/relay/connection/connection.rb
|
66
66
|
- lib/relay/node.rb
|
67
|
+
- lib/relay/node/composite.rb
|
67
68
|
- lib/relay/node/global_id_field.rb
|
68
69
|
- lib/relay/node/plural_identifying_root_field.rb
|
69
70
|
- lib/relay/version.rb
|
@@ -88,9 +89,9 @@ require_paths:
|
|
88
89
|
- lib
|
89
90
|
required_ruby_version: !ruby/object:Gem::Requirement
|
90
91
|
requirements:
|
91
|
-
- - "
|
92
|
+
- - "~>"
|
92
93
|
- !ruby/object:Gem::Version
|
93
|
-
version: '0'
|
94
|
+
version: '2.0'
|
94
95
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
95
96
|
requirements:
|
96
97
|
- - ">="
|