relay-rb 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,106 @@
1
+ require 'relay'
2
+ require_relative 'data'
3
+
4
+ RSpec.describe 'Relay Node' do
5
+
6
+ def schema
7
+ Relay::Node::Data::Schema
8
+ end
9
+
10
+ def user_q1
11
+ %Q{{
12
+ node(id: "1") {
13
+ id
14
+ }
15
+ }}
16
+ end
17
+
18
+ def user_q2
19
+ %Q{{
20
+ node(id: "1") {
21
+ id
22
+ ... on User {
23
+ name
24
+ }
25
+ }
26
+ }}
27
+ end
28
+
29
+ def user_q3
30
+ %Q{{
31
+ node(id: "1") {
32
+ id
33
+ ... on Photo {
34
+ width
35
+ }
36
+ }
37
+ }}
38
+ end
39
+
40
+ def photo_q1
41
+ %Q{{
42
+ node(id: "4") {
43
+ id
44
+ }
45
+ }}
46
+ end
47
+
48
+ def photo_q2
49
+ %Q{{
50
+ node(id: "4") {
51
+ id
52
+ ... on Photo {
53
+ width
54
+ }
55
+ }
56
+ }}
57
+ end
58
+
59
+ def common_q1
60
+ %Q{{
61
+ node(id: "5") {
62
+ id
63
+ }
64
+ }}
65
+ end
66
+
67
+ it 'Should get correct id for users' do
68
+ user_expect = { node: { id: '1' }}
69
+ document = GraphQL::Language.parse(user_q1)
70
+ executor = GraphQL::Executor.new(document, schema)
71
+ expect(executor.execute({})).to eq(user_expect)
72
+ end
73
+
74
+ it 'Should get correct id for photos' do
75
+ photo_expect = { node: { id: '4' }}
76
+ document = GraphQL::Language.parse(photo_q1)
77
+ executor = GraphQL::Executor.new(document, schema)
78
+ expect(executor.execute({})).to eq(photo_expect)
79
+ end
80
+
81
+ it 'Should get correct name for users' do
82
+ user_expect = { node: { id: '1', name: 'John Doe' }}
83
+ document = GraphQL::Language.parse(user_q2)
84
+ executor = GraphQL::Executor.new(document, schema)
85
+ expect(executor.execute({})).to eq(user_expect)
86
+ end
87
+
88
+ it 'Should get correct width for photos' do
89
+ expectation = { data: { node: { id: '4', width: 400 }}}
90
+ result = GraphQL::graphql(schema, photo_q2, {}, {})
91
+ expect(result).to eq(expectation)
92
+ end
93
+
94
+ it 'Should ignore photo fragment on user' do
95
+ expectation = { data: { node: { id: '1' }}}
96
+ result = GraphQL::graphql(schema, user_q3, {}, {})
97
+ expect(result).to eq(expectation)
98
+ end
99
+
100
+ it 'Should return null for bad IDs' do
101
+ nil_expect = { node: nil }
102
+ document = GraphQL::Language.parse(common_q1)
103
+ executor = GraphQL::Executor.new(document, schema)
104
+ expect(executor.execute({})).to eq(nil_expect)
105
+ end
106
+ end
@@ -0,0 +1,96 @@
1
+ # This file was generated by the `rspec --init` command. Conventionally, all
2
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
3
+ # The generated `.rspec` file contains `--require spec_helper` which will cause
4
+ # this file to always be loaded, without a need to explicitly require it in any
5
+ # files.
6
+ #
7
+ # Given that it is always loaded, you are encouraged to keep this file as
8
+ # light-weight as possible. Requiring heavyweight dependencies from this file
9
+ # will add to the boot time of your test suite on EVERY test run, even for an
10
+ # individual file that may not need all of that loaded. Instead, consider making
11
+ # a separate helper file that requires the additional dependencies and performs
12
+ # the additional setup, and require it from the spec files that actually need
13
+ # it.
14
+ #
15
+ # The `.rspec` file also contains a few flags that are not defaults but that
16
+ # users commonly want.
17
+ #
18
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
19
+ RSpec.configure do |config|
20
+ # rspec-expectations config goes here. You can use an alternate
21
+ # assertion/expectation library such as wrong or the stdlib/minitest
22
+ # assertions if you prefer.
23
+ config.expect_with :rspec do |expectations|
24
+ # This option will default to `true` in RSpec 4. It makes the `description`
25
+ # and `failure_message` of custom matchers include text for helper methods
26
+ # defined using `chain`, e.g.:
27
+ # be_bigger_than(2).and_smaller_than(4).description
28
+ # # => 'be bigger than 2 and smaller than 4'
29
+ # ...rather than:
30
+ # # => 'be bigger than 2'
31
+ expectations.include_chain_clauses_in_custom_matcher_descriptions = true
32
+ end
33
+
34
+ # rspec-mocks config goes here. You can use an alternate test double
35
+ # library (such as bogus or mocha) by changing the `mock_with` option here.
36
+ config.mock_with :rspec do |mocks|
37
+ # Prevents you from mocking or stubbing a method that does not exist on
38
+ # a real object. This is generally recommended, and will default to
39
+ # `true` in RSpec 4.
40
+ mocks.verify_partial_doubles = true
41
+ end
42
+
43
+ # The settings below are suggested to provide a good initial experience
44
+ # with RSpec, but feel free to customize to your heart's content.
45
+ =begin
46
+ # These two settings work together to allow you to limit a spec run
47
+ # to individual examples or groups you care about by tagging them with
48
+ # `:focus` metadata. When nothing is tagged with `:focus`, all examples
49
+ # get run.
50
+ config.filter_run :focus
51
+ config.run_all_when_everything_filtered = true
52
+
53
+ # Allows RSpec to persist some state between runs in order to support
54
+ # the `--only-failures` and `--next-failure` CLI options. We recommend
55
+ # you configure your source control system to ignore this file.
56
+ config.example_status_persistence_file_path = 'spec/examples.txt'
57
+
58
+ # Limits the available syntax to the non-monkey patched syntax that is
59
+ # recommended. For more details, see:
60
+ # - http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax
61
+ # - http://www.teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
62
+ # - http://myronmars.to/n/dev-blog/2014/05/notable-changes-in-rspec-3#new__config_option_to_disable_rspeccore_monkey_patching
63
+ config.disable_monkey_patching!
64
+
65
+ # This setting enables warnings. It's recommended, but in some cases may
66
+ # be too noisy due to issues in dependencies.
67
+ config.warnings = true
68
+
69
+ # Many RSpec users commonly either run the entire suite or an individual
70
+ # file, and it's useful to allow more verbose output when running an
71
+ # individual spec file.
72
+ if config.files_to_run.one?
73
+ # Use the documentation formatter for detailed output,
74
+ # unless a formatter has already been configured
75
+ # (e.g. via a command-line flag).
76
+ config.default_formatter = 'doc'
77
+ end
78
+
79
+ # Print the 10 slowest examples and example groups at the
80
+ # end of the spec run, to help surface which specs are running
81
+ # particularly slow.
82
+ config.profile_examples = 10
83
+
84
+ # Run specs in random order to surface order dependencies. If you find an
85
+ # order dependency and want to debug it, you can fix the order by providing
86
+ # the seed, which is printed after each run.
87
+ # --seed 1234
88
+ config.order = :random
89
+
90
+ # Seed global randomization in this process using the `--seed` CLI option.
91
+ # Setting this allows you to use `--seed` to deterministically reproduce
92
+ # test failures related to randomization by passing the same `--seed` value
93
+ # as the one that triggered the failure.
94
+ Kernel.srand config.seed
95
+ =end
96
+ end
@@ -0,0 +1,251 @@
1
+ require_relative 'data'
2
+ require_relative 'schema'
3
+
4
+ RSpec.describe "Star Wars connection" do
5
+
6
+ it "Should correctly fetch first ship of the rebels" do
7
+ query = %Q(
8
+ query RebelShipQuery {
9
+ rebels {
10
+ name
11
+ ships(first: 1) {
12
+ edges {
13
+ node {
14
+ name
15
+ }
16
+ }
17
+ }
18
+ }
19
+ }
20
+ )
21
+
22
+ expectation = {
23
+ data: {
24
+ rebels: {
25
+ name: 'Alliance to Restore the Republic',
26
+ ships: {
27
+ edges: [
28
+ {
29
+ node: {
30
+ name: 'X-Wing'
31
+ }
32
+ }
33
+ ]
34
+ }
35
+ }
36
+ }
37
+ }
38
+
39
+ result = GraphQL::graphql(StarWars::Schema, query, {}, {})
40
+
41
+ expect(result).to eql(expectation)
42
+ end
43
+
44
+ it "Should correctly fetch first 2 ships of the rebels with a cursor" do
45
+ query = %Q(
46
+ query RebelShipQuery {
47
+ rebels {
48
+ name
49
+ ships(first: 2) {
50
+ edges {
51
+ cursor
52
+ node {
53
+ name
54
+ }
55
+ }
56
+ }
57
+ }
58
+ }
59
+ )
60
+
61
+ expectation = {
62
+ data: {
63
+ rebels: {
64
+ name: 'Alliance to Restore the Republic',
65
+ ships: {
66
+ edges: [
67
+ {
68
+ cursor: 'YXJyYXljb25uZWN0aW9uOjA=',
69
+ node: {
70
+ name: 'X-Wing'
71
+ }
72
+ },
73
+ {
74
+ cursor: 'YXJyYXljb25uZWN0aW9uOjE=',
75
+ node: {
76
+ name: 'Y-Wing'
77
+ }
78
+ }
79
+ ]
80
+ }
81
+ }
82
+ }
83
+ }
84
+
85
+ result = GraphQL::graphql(StarWars::Schema, query, {}, {})
86
+
87
+ expect(result).to eql(expectation)
88
+ end
89
+
90
+ it "Should correctly fetch next 3 ships of the rebels with a cursor" do
91
+ query = %Q(
92
+ query RebelShipQuery {
93
+ rebels {
94
+ name
95
+ ships(first: 3, after: "YXJyYXljb25uZWN0aW9uOjE=") {
96
+ edges {
97
+ cursor
98
+ node {
99
+ name
100
+ }
101
+ }
102
+ }
103
+ }
104
+ }
105
+ )
106
+
107
+ expectation = {
108
+ data: {
109
+ rebels: {
110
+ name: 'Alliance to Restore the Republic',
111
+ ships: {
112
+ edges: [
113
+ {
114
+ cursor: 'YXJyYXljb25uZWN0aW9uOjI=',
115
+ node: {
116
+ name: 'A-Wing'
117
+ }
118
+ }, {
119
+ cursor: 'YXJyYXljb25uZWN0aW9uOjM=',
120
+ node: {
121
+ name: 'Millenium Falcon'
122
+ }
123
+ }, {
124
+ cursor: 'YXJyYXljb25uZWN0aW9uOjQ=',
125
+ node: {
126
+ name: 'Home One'
127
+ }
128
+ }
129
+ ]
130
+ }
131
+ }
132
+ }
133
+ }
134
+
135
+ result = GraphQL::graphql(StarWars::Schema, query, {}, {})
136
+
137
+ expect(result).to eql(expectation)
138
+ end
139
+
140
+ it "Should correctly fetch no ships at the end of the connection" do
141
+ query = %Q(
142
+ query RebelShipQuery {
143
+ rebels {
144
+ name
145
+ ships(first: 3, after: "YXJyYXljb25uZWN0aW9uOjQ=") {
146
+ edges {
147
+ cursor
148
+ node {
149
+ name
150
+ }
151
+ }
152
+ }
153
+ }
154
+ }
155
+ )
156
+
157
+ expectation = {
158
+ data: {
159
+ rebels: {
160
+ name: 'Alliance to Restore the Republic',
161
+ ships: {
162
+ edges: []
163
+ }
164
+ }
165
+ }
166
+ }
167
+
168
+ result = GraphQL::graphql(StarWars::Schema, query, {}, {})
169
+
170
+ expect(result).to eql(expectation)
171
+ end
172
+
173
+
174
+ it "Should correctly identify the end of the list" do
175
+ query = %Q(
176
+ query RebelShipQuery {
177
+ rebels {
178
+ name
179
+ originalShips: ships(first: 2) {
180
+ edges {
181
+ node {
182
+ name
183
+ }
184
+ }
185
+ pageInfo {
186
+ hasNextPage
187
+ }
188
+ }
189
+ moreShips: ships(first: 3, after: "YXJyYXljb25uZWN0aW9uOjE=") {
190
+ edges {
191
+ node {
192
+ name
193
+ }
194
+ }
195
+ pageInfo {
196
+ hasNextPage
197
+ }
198
+ }
199
+ }
200
+ }
201
+ )
202
+
203
+ expectation = {
204
+ data: {
205
+ rebels: {
206
+ name:'Alliance to Restore the Republic',
207
+ originalShips: {
208
+ edges: [
209
+ {
210
+ node: {
211
+ name: 'X-Wing'
212
+ }
213
+ }, {
214
+ node: {
215
+ name: 'Y-Wing'
216
+ }
217
+ }
218
+ ],
219
+ pageInfo: {
220
+ hasNextPage: true
221
+ }
222
+ },
223
+ moreShips: {
224
+ edges: [
225
+ {
226
+ node: {
227
+ name: 'A-Wing'
228
+ }
229
+ }, {
230
+ node: {
231
+ name: 'Millenium Falcon'
232
+ }
233
+ },{
234
+ node: {
235
+ name: 'Home One'
236
+ }
237
+ }
238
+ ],
239
+ pageInfo: {
240
+ hasNextPage: false
241
+ }
242
+ }
243
+ }
244
+ }
245
+ }
246
+
247
+ result = GraphQL::graphql(StarWars::Schema, query, {}, {})
248
+
249
+ expect(result).to eql(expectation)
250
+ end
251
+ end
@@ -0,0 +1,54 @@
1
+ module StarWars
2
+ module Data
3
+
4
+ Ship = Struct.new('Ship', :id, :name)
5
+ Faction = Struct.new('Faction', :id, :name, :ships)
6
+
7
+ XWing = Ship.new(1, 'X-Wing')
8
+ YWing = Ship.new(2, 'Y-Wing')
9
+ AWing = Ship.new(3, 'A-Wing')
10
+ Falcon = Ship.new(4, 'Millenium Falcon')
11
+ HomeOne = Ship.new(5, 'Home One')
12
+
13
+ TieFighter = Ship.new(6, 'Tie Fighter')
14
+ TieInterceptor = Ship.new(7, 'Tie Interceptor')
15
+ Executor = Ship.new(8, 'Executor')
16
+
17
+ Rebels = Faction.new(1, 'Alliance to Restore the Republic', [1, 2, 3, 4, 5])
18
+ Empire = Faction.new(2, 'Galactic Empire', [6, 7, 8])
19
+
20
+ Data = {
21
+ faction: {
22
+ 1 => Rebels,
23
+ 2 => Empire
24
+ },
25
+ ship: {
26
+ 1 => XWing,
27
+ 2 => YWing,
28
+ 3 => AWing,
29
+ 4 => Falcon,
30
+ 5 => HomeOne,
31
+ 6 => TieFighter,
32
+ 7 => TieInterceptor,
33
+ 8 => Executor
34
+ }
35
+ }
36
+
37
+ def self.ship(id)
38
+ Data[:ship][id]
39
+ end
40
+
41
+ def self.faction(id)
42
+ Data[:faction][id]
43
+ end
44
+
45
+ def self.rebels
46
+ Rebels
47
+ end
48
+
49
+ def self.empire
50
+ Empire
51
+ end
52
+
53
+ end
54
+ end
@@ -0,0 +1,139 @@
1
+ require_relative 'data'
2
+ require_relative 'schema'
3
+
4
+ RSpec.describe "Star Wars object identification" do
5
+
6
+ it "Should correctly fetch the ID and name of the rebels" do
7
+
8
+ query = %Q(
9
+ query rebelsQuery {
10
+ rebels {
11
+ id
12
+ name
13
+ }
14
+ }
15
+ )
16
+
17
+ expectation = {
18
+ data: {
19
+ rebels: {
20
+ id: 'RmFjdGlvbjox',
21
+ name: 'Alliance to Restore the Republic'
22
+ }
23
+ }
24
+ }
25
+
26
+ result = GraphQL::graphql(StarWars::Schema, query, {}, {})
27
+
28
+ expect(result).to eql(expectation)
29
+ end
30
+
31
+ it "Should correctly refetch the rebels" do
32
+
33
+ query = %Q(
34
+ query rebelsRefetchQuery {
35
+ node(id: "RmFjdGlvbjox") {
36
+ id
37
+ ... on Faction {
38
+ name
39
+ }
40
+ }
41
+ }
42
+ )
43
+
44
+ expectation = {
45
+ data: {
46
+ node: {
47
+ id: 'RmFjdGlvbjox',
48
+ name: 'Alliance to Restore the Republic'
49
+ }
50
+ }
51
+ }
52
+
53
+ result = GraphQL::graphql(StarWars::Schema, query, {}, {})
54
+
55
+ expect(result).to eql(expectation)
56
+ end
57
+
58
+
59
+ it "Should correctly fetch the ID and name of the empire" do
60
+
61
+ query = %Q(
62
+ query empireQuery {
63
+ empire {
64
+ id
65
+ name
66
+ }
67
+ }
68
+ )
69
+
70
+ expectation = {
71
+ data: {
72
+ empire: {
73
+ id: 'RmFjdGlvbjoy',
74
+ name: 'Galactic Empire'
75
+ }
76
+ }
77
+ }
78
+
79
+ result = GraphQL::graphql(StarWars::Schema, query, {}, {})
80
+
81
+ expect(result).to eql(expectation)
82
+ end
83
+
84
+
85
+ it "Should correctly refetch the empire" do
86
+
87
+ query = %Q(
88
+ query empireRefetchQuery {
89
+ node(id: "RmFjdGlvbjoy") {
90
+ id
91
+ ... on Faction {
92
+ name
93
+ }
94
+ }
95
+ }
96
+ )
97
+
98
+ expectation = {
99
+ data: {
100
+ node: {
101
+ id: 'RmFjdGlvbjoy',
102
+ name: 'Galactic Empire'
103
+ }
104
+ }
105
+ }
106
+
107
+ result = GraphQL::graphql(StarWars::Schema, query, {}, {})
108
+
109
+ expect(result).to eql(expectation)
110
+ end
111
+
112
+ it "Should correctly refetch the X-Wing" do
113
+
114
+ query = %Q(
115
+ query XWingRefetchQuery {
116
+ node(id: "U2hpcDox") {
117
+ id
118
+ ... on Ship {
119
+ name
120
+ }
121
+ }
122
+ }
123
+ )
124
+
125
+ expectation = {
126
+ data: {
127
+ node: {
128
+ id: 'U2hpcDox',
129
+ name: 'X-Wing'
130
+ }
131
+ }
132
+ }
133
+
134
+ result = GraphQL::graphql(StarWars::Schema, query, {}, {})
135
+
136
+ expect(result).to eql(expectation)
137
+ end
138
+
139
+ end