artemis 0.8.0 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (37) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/ruby.yml +21 -6
  3. data/Appraisals +21 -3
  4. data/CHANGELOG.md +35 -4
  5. data/artemis.gemspec +1 -1
  6. data/bin/console +3 -3
  7. data/gemfiles/graphql_2_0.gemfile +15 -0
  8. data/gemfiles/rails_60.gemfile +3 -3
  9. data/gemfiles/rails_71.gemfile +14 -0
  10. data/gemfiles/rails_edge.gemfile +1 -0
  11. data/lib/artemis/adapters/net_http_persistent_adapter.rb +6 -0
  12. data/lib/artemis/railtie.rb +1 -1
  13. data/lib/artemis/version.rb +1 -1
  14. data/spec/adapters_spec.rb +9 -2
  15. data/spec/autoloading_spec.rb +55 -49
  16. data/spec/callbacks_spec.rb +6 -6
  17. data/spec/client_spec.rb +84 -78
  18. data/spec/endpoint_spec.rb +11 -7
  19. data/spec/fixtures/github/_repository_fields.graphql +12 -0
  20. data/spec/fixtures/github/repository.graphql +6 -0
  21. data/spec/fixtures/github/schema.json +165225 -0
  22. data/spec/fixtures/github/user.graphql +6 -0
  23. data/spec/fixtures/github/user_repositories.graphql +13 -0
  24. data/spec/fixtures/github.rb +2 -0
  25. data/spec/fixtures/responses/github/repository.yml +17 -0
  26. data/spec/fixtures/responses/github/user.json +10 -0
  27. data/spec/spec_helper.rb +3 -3
  28. data/spec/test_helper_spec.rb +32 -32
  29. metadata +15 -13
  30. data/spec/fixtures/metaphysics/_artist_fields.graphql +0 -4
  31. data/spec/fixtures/metaphysics/artist.graphql +0 -7
  32. data/spec/fixtures/metaphysics/artists.graphql +0 -8
  33. data/spec/fixtures/metaphysics/artwork.graphql +0 -8
  34. data/spec/fixtures/metaphysics/schema.json +0 -49811
  35. data/spec/fixtures/metaphysics.rb +0 -2
  36. data/spec/fixtures/responses/metaphysics/artist.yml +0 -24
  37. data/spec/fixtures/responses/metaphysics/artwork.json +0 -12
data/spec/client_spec.rb CHANGED
@@ -5,119 +5,128 @@ describe GraphQL::Client do
5
5
 
6
6
  describe ".lookup_graphql_file" do
7
7
  it "returns the path to the matching graph file" do
8
- expect(Metaphysics.resolve_graphql_file_path("artist")).to eq("#{PROJECT_DIR}/spec/fixtures/metaphysics/artist.graphql")
8
+ expect(Github.resolve_graphql_file_path("user")).to eq("#{PROJECT_DIR}/spec/fixtures/github/user.graphql")
9
9
  end
10
10
 
11
11
  it "returns nil if the file is missing" do
12
- expect(Metaphysics.resolve_graphql_file_path("does_not_exist")).to be_nil
12
+ expect(Github.resolve_graphql_file_path("does_not_exist")).to be_nil
13
13
  end
14
14
  end
15
15
 
16
16
  describe ".graphql_file_paths" do
17
17
  it "returns a list of GraphQL files (*.graphql) in the query_paths" do
18
- Metaphysics.instance_variable_set :@graphql_file_paths, nil
19
- original = Metaphysics.query_paths
18
+ Github.instance_variable_set :@graphql_file_paths, nil
19
+ original = Github.query_paths
20
20
 
21
- Metaphysics.query_paths = [File.join(PROJECT_DIR, 'tmp')]
21
+ Github.query_paths = [File.join(PROJECT_DIR, 'tmp')]
22
22
 
23
23
  begin
24
- FileUtils.mkdir "./tmp/metaphysics" if !Dir.exist?("./tmp/metaphysics")
24
+ FileUtils.mkdir "./tmp/github" if !Dir.exist?("./tmp/github")
25
25
 
26
- with_files "./tmp/metaphysics/text.txt", "./tmp/metaphysics/sale.graphql" do
27
- expect(Metaphysics.graphql_file_paths).to eq(["#{PROJECT_DIR}/tmp/metaphysics/sale.graphql"])
26
+ with_files "./tmp/github/text.txt", "./tmp/github/sale.graphql" do
27
+ expect(Github.graphql_file_paths).to eq(["#{PROJECT_DIR}/tmp/github/sale.graphql"])
28
28
  end
29
29
  ensure
30
- Metaphysics.instance_variable_set :@graphql_file_paths, nil
31
- Metaphysics.query_paths = original
30
+ Github.instance_variable_set :@graphql_file_paths, nil
31
+ Github.query_paths = original
32
32
  end
33
33
  end
34
34
  end
35
35
 
36
36
  it "can make a GraphQL request without variables" do
37
- Metaphysics.artwork
37
+ Github.user
38
38
 
39
39
  request = requests[0]
40
40
 
41
- expect(request.operation_name).to eq('Metaphysics__Artwork')
41
+ expect(request.operation_name).to eq('Github__User')
42
42
  expect(request.variables).to be_empty
43
43
  expect(request.context).to eq({})
44
44
  expect(request.document.to_query_string).to eq(<<~GRAPHQL.strip)
45
- query Metaphysics__Artwork {
46
- artwork(id: "yayoi-kusama-pumpkin-yellow-and-black") {
47
- title
48
- artist {
49
- name
50
- }
45
+ query Github__User {
46
+ user(login: "yuki24") {
47
+ id
48
+ name
51
49
  }
52
50
  }
53
51
  GRAPHQL
54
52
  end
55
53
 
56
54
  it "can make a GraphQL request with variables" do
57
- Metaphysics.artist(id: "yayoi-kusama")
55
+ Github.repository(owner: "yuki24", name: "artemis")
58
56
 
59
57
  request = requests[0]
60
58
 
61
- expect(request.operation_name).to eq('Metaphysics__Artist')
62
- expect(request.variables).to eq('id' => 'yayoi-kusama')
59
+ expect(request.operation_name).to eq('Github__Repository')
60
+ expect(request.variables).to eq("owner" => "yuki24", "name" => "artemis")
63
61
  expect(request.context).to eq({})
64
62
  expect(request.document.to_query_string).to eq(<<~GRAPHQL.strip)
65
- query Metaphysics__Artist($id: String!) {
66
- artist(id: $id) {
63
+ query Github__Repository($owner: String!, $name: String!) {
64
+ repository(owner: $owner, name: $name) {
67
65
  name
68
- bio
69
- birthday
66
+ nameWithOwner
70
67
  }
71
68
  }
72
69
  GRAPHQL
73
70
  end
74
71
 
75
72
  it "can make a GraphQL request with a query that contains fragments" do
76
- Metaphysics.artists(size: 10)
73
+ Github.user_repositories(login: "yuki24", size: 10)
77
74
 
78
75
  request = requests[0]
79
76
 
80
- expect(request.operation_name).to eq('Metaphysics__Artists')
81
- expect(request.variables).to eq('size' => 10)
77
+ expect(request.operation_name).to eq('Github__UserRepositories')
78
+ expect(request.variables).to eq('login' => 'yuki24', 'size' => 10)
82
79
  expect(request.context).to eq({})
83
80
  expect(request.document.to_query_string).to eq(<<~GRAPHQL.strip)
84
- query Metaphysics__Artists($size: Int!) {
85
- artists(size: $size) {
81
+ query Github__UserRepositories($login: String!, $size: Int!) {
82
+ user(login: $login) {
83
+ id
86
84
  name
87
- bio
88
- birthday
89
- ...Metaphysics__ArtistFields
85
+ repositories(first: $size) {
86
+ nodes {
87
+ name
88
+ description
89
+ ...Github__RepositoryFields
90
+ }
91
+ }
90
92
  }
91
93
  }
92
94
 
93
- fragment Metaphysics__ArtistFields on Artist {
94
- hometown
95
- deathday
95
+ fragment Github__RepositoryFields on Repository {
96
+ name
97
+ nameWithOwner
98
+ url
99
+ updatedAt
100
+ languages(first: 1) {
101
+ nodes {
102
+ name
103
+ color
104
+ }
105
+ }
96
106
  }
97
107
  GRAPHQL
98
108
  end
99
109
 
100
110
  it "can make a GraphQL request with #execute" do
101
- Metaphysics.execute(:artist, id: "yayoi-kusama")
111
+ Github.execute(:repository, owner: "yuki24", name: "artemis")
102
112
 
103
113
  request = requests[0]
104
114
 
105
- expect(request.operation_name).to eq('Metaphysics__Artist')
106
- expect(request.variables).to eq('id' => 'yayoi-kusama')
115
+ expect(request.operation_name).to eq('Github__Repository')
116
+ expect(request.variables).to eq("owner" => "yuki24", "name" => "artemis")
107
117
  expect(request.context).to eq({})
108
118
  expect(request.document.to_query_string).to eq(<<~GRAPHQL.strip)
109
- query Metaphysics__Artist($id: String!) {
110
- artist(id: $id) {
119
+ query Github__Repository($owner: String!, $name: String!) {
120
+ repository(owner: $owner, name: $name) {
111
121
  name
112
- bio
113
- birthday
122
+ nameWithOwner
114
123
  }
115
124
  }
116
125
  GRAPHQL
117
126
  end
118
127
 
119
- it "can make a GraphQL request with #execute" do
120
- expect { Metaphysics.execute(:does_not_exist) }
128
+ it "raises an error when the specified graphql file does not exist" do
129
+ expect { Github.execute(:does_not_exist) }
121
130
  .to raise_error(Artemis::GraphQLFileNotFound)
122
131
  .with_message(/Query does_not_exist\.graphql not found/)
123
132
  end
@@ -125,17 +134,17 @@ describe GraphQL::Client do
125
134
  it "assigns context to the request when provided as an argument" do
126
135
  context = { headers: { Authorization: 'bearer ...' } }
127
136
 
128
- Metaphysics.artist(id: "yayoi-kusama", context: context)
137
+ Github.repository(owner: "yuki24", name: "artemis", context: context)
129
138
 
130
139
  expect(requests[0].context).to eq(context)
131
140
  end
132
141
 
133
142
  it "can create a client that always assigns the provided context to the request" do
134
143
  context = { headers: { Authorization: 'bearer ...' } }
135
- client = Metaphysics.with_context(context)
144
+ client = Github.with_context(context)
136
145
 
137
- client.artist(id: "yayoi-kusama")
138
- client.artist(id: "yayoi-kusama")
146
+ client.repository(owner: "yuki24", name: "artemis")
147
+ client.repository(owner: "yuki24", name: "artemis")
139
148
 
140
149
  expect(requests[0].context).to eq(context)
141
150
  expect(requests[1].context).to eq(context)
@@ -143,21 +152,21 @@ describe GraphQL::Client do
143
152
 
144
153
  it "assigns the default context to a GraphQL request if present" do
145
154
  begin
146
- Metaphysics.default_context = { headers: { Authorization: 'bearer ...' } }
147
- Metaphysics.artist(id: "yayoi-kusama")
155
+ Github.default_context = { headers: { Authorization: 'bearer ...' } }
156
+ Github.repository(owner: "yuki24", name: "artemis")
148
157
 
149
158
  expect(requests[0].context).to eq(headers: { Authorization: 'bearer ...' })
150
159
  ensure
151
- Metaphysics.default_context = { }
160
+ Github.default_context = { }
152
161
  end
153
162
  end
154
163
 
155
164
  it "can make a GraphQL request with all of .default_context, with_context(...) and the :context argument" do
156
165
  begin
157
- Metaphysics.default_context = { headers: { 'User-Agent': 'Artemis', 'X-key': 'value', Authorization: 'token ...' } }
158
- Metaphysics
166
+ Github.default_context = { headers: { 'User-Agent': 'Artemis', 'X-key': 'value', Authorization: 'token ...' } }
167
+ Github
159
168
  .with_context({ headers: { 'X-key': 'overridden' } })
160
- .artist(id: "yayoi-kusama", context: { headers: { Authorization: 'bearer ...' } })
169
+ .repository(owner: "yuki24", name: "artemis", context: { headers: { Authorization: 'bearer ...' } })
161
170
 
162
171
  expect(requests[0].context).to eq(
163
172
  headers: {
@@ -167,41 +176,38 @@ describe GraphQL::Client do
167
176
  }
168
177
  )
169
178
  ensure
170
- Metaphysics.default_context = { }
179
+ Github.default_context = { }
171
180
  end
172
181
  end
173
182
 
174
183
  it "can batch multiple requests using Multiplex" do
175
- responses = Metaphysics.multiplex do |queue|
176
- queue.artist(id: "yayoi-kusama", context: { headers: { Authorization: 'bearer ...' } })
177
- queue.artwork
184
+ Github.multiplex do |queue|
185
+ queue.repository(owner: "yuki24", name: "artemis", context: { headers: { Authorization: 'bearer ...' } })
186
+ queue.user
178
187
  end
179
188
 
180
- artist_query, artwork_query = requests[0].queries
189
+ repository_query, user_query = requests[0].queries
181
190
 
182
- expect(artist_query[:operationName]).to eq('Metaphysics__Artist')
183
- expect(artist_query[:variables]).to eq('id' => 'yayoi-kusama')
184
- expect(artist_query[:context]).to eq({ headers: { Authorization: 'bearer ...' } })
185
- expect(artist_query[:query]).to eq(<<~GRAPHQL.strip)
186
- query Metaphysics__Artist($id: String!) {
187
- artist(id: $id) {
191
+ expect(repository_query[:operationName]).to eq('Github__Repository')
192
+ expect(repository_query[:variables]).to eq("owner" => "yuki24", "name" => "artemis")
193
+ expect(repository_query[:context]).to eq({ headers: { Authorization: 'bearer ...' } })
194
+ expect(repository_query[:query]).to eq(<<~GRAPHQL.strip)
195
+ query Github__Repository($owner: String!, $name: String!) {
196
+ repository(owner: $owner, name: $name) {
188
197
  name
189
- bio
190
- birthday
198
+ nameWithOwner
191
199
  }
192
200
  }
193
201
  GRAPHQL
194
202
 
195
- expect(artwork_query[:operationName]).to eq('Metaphysics__Artwork')
196
- expect(artwork_query[:variables]).to be_empty
197
- expect(artwork_query[:context]).to eq({})
198
- expect(artwork_query[:query]).to eq(<<~GRAPHQL.strip)
199
- query Metaphysics__Artwork {
200
- artwork(id: "yayoi-kusama-pumpkin-yellow-and-black") {
201
- title
202
- artist {
203
- name
204
- }
203
+ expect(user_query[:operationName]).to eq('Github__User')
204
+ expect(user_query[:variables]).to be_empty
205
+ expect(user_query[:context]).to eq({})
206
+ expect(user_query[:query]).to eq(<<~GRAPHQL.strip)
207
+ query Github__User {
208
+ user(login: "yuki24") {
209
+ id
210
+ name
205
211
  }
206
212
  }
207
213
  GRAPHQL
@@ -1,4 +1,8 @@
1
1
  describe Artemis::GraphQLEndpoint do
2
+ after do
3
+ Artemis::GraphQLEndpoint.const_get(:ENDPOINT_INSTANCES).delete("gitlab")
4
+ end
5
+
2
6
  describe ".lookup" do
3
7
  it "raises an exception when the service is missing" do
4
8
  expect { Artemis::GraphQLEndpoint.lookup(:does_not_exit) }.to raise_error(Artemis::EndpointNotFound)
@@ -6,18 +10,18 @@ describe Artemis::GraphQLEndpoint do
6
10
  end
7
11
 
8
12
  it "can register an endpoint" do
9
- endpoint = Artemis::GraphQLEndpoint.register!(:github, url: "https://api.github.com/graphql")
13
+ endpoint = Artemis::GraphQLEndpoint.register!(:gitlab, url: "https://api.gitlab.com/graphql")
10
14
 
11
- expect(endpoint.url).to eq("https://api.github.com/graphql")
15
+ expect(endpoint.url).to eq("https://api.gitlab.com/graphql")
12
16
  expect(endpoint.connection).to be_instance_of(Artemis::Adapters::NetHttpAdapter)
13
17
  end
14
18
 
15
19
  it "can look up a registered endpoint" do
16
- Artemis::GraphQLEndpoint.register!(:github, url: "https://api.github.com/graphql")
20
+ Artemis::GraphQLEndpoint.register!(:gitlab, url: "https://api.gitlab.com/graphql")
17
21
 
18
- endpoint = Artemis::GraphQLEndpoint.lookup(:github)
22
+ endpoint = Artemis::GraphQLEndpoint.lookup(:gitlab)
19
23
 
20
- expect(endpoint.url).to eq("https://api.github.com/graphql")
24
+ expect(endpoint.url).to eq("https://api.gitlab.com/graphql")
21
25
  expect(endpoint.connection).to be_instance_of(Artemis::Adapters::NetHttpAdapter) # Not a fan of this test but for now
22
26
 
23
27
  # FIXME: This #schema method makes a network call.
@@ -32,9 +36,9 @@ describe Artemis::GraphQLEndpoint do
32
36
  pool_size: 25,
33
37
  }
34
38
 
35
- endpoint = Artemis::GraphQLEndpoint.register!(:github, url: "https://api.github.com/graphql", **options)
39
+ endpoint = Artemis::GraphQLEndpoint.register!(:gitlab, url: "https://api.gitlab.com/graphql", **options)
36
40
 
37
- expect(endpoint.url).to eq("https://api.github.com/graphql")
41
+ expect(endpoint.url).to eq("https://api.gitlab.com/graphql")
38
42
  expect(endpoint.timeout).to eq(10)
39
43
  expect(endpoint.pool_size).to eq(25)
40
44
  expect(endpoint.connection).to be_instance_of(Artemis::Adapters::TestAdapter) # Not a fan of this test but for now
@@ -0,0 +1,12 @@
1
+ fragment on Repository {
2
+ name
3
+ nameWithOwner
4
+ url
5
+ updatedAt
6
+ languages(first: 1) {
7
+ nodes {
8
+ name
9
+ color
10
+ }
11
+ }
12
+ }
@@ -0,0 +1,6 @@
1
+ query($owner: String!, $name: String!) {
2
+ repository(owner: $owner, name: $name) {
3
+ name
4
+ nameWithOwner
5
+ }
6
+ }