artemis 1.0.0 → 1.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/spec/client_spec.rb DELETED
@@ -1,228 +0,0 @@
1
- describe GraphQL::Client do
2
- before do
3
- requests.clear
4
- end
5
-
6
- describe ".lookup_graphql_file" do
7
- it "returns the path to the matching graph file" do
8
- expect(Github.resolve_graphql_file_path("user")).to eq("#{PROJECT_DIR}/spec/fixtures/github/user.graphql")
9
- end
10
-
11
- it "returns nil if the file is missing" do
12
- expect(Github.resolve_graphql_file_path("does_not_exist")).to be_nil
13
- end
14
- end
15
-
16
- describe ".graphql_file_paths" do
17
- it "returns a list of GraphQL files (*.graphql) in the query_paths" do
18
- Github.instance_variable_set :@graphql_file_paths, nil
19
- original = Github.query_paths
20
-
21
- Github.query_paths = [File.join(PROJECT_DIR, 'tmp')]
22
-
23
- begin
24
- FileUtils.mkdir "./tmp/github" if !Dir.exist?("./tmp/github")
25
-
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
- end
29
- ensure
30
- Github.instance_variable_set :@graphql_file_paths, nil
31
- Github.query_paths = original
32
- end
33
- end
34
- end
35
-
36
- it "can make a GraphQL request without variables" do
37
- Github.user
38
-
39
- request = requests[0]
40
-
41
- expect(request.operation_name).to eq('Github__User')
42
- expect(request.variables).to be_empty
43
- expect(request.context).to eq({})
44
- expect(request.document.to_query_string).to eq(<<~GRAPHQL.strip)
45
- query Github__User {
46
- user(login: "yuki24") {
47
- id
48
- name
49
- }
50
- }
51
- GRAPHQL
52
- end
53
-
54
- it "can make a GraphQL request with variables" do
55
- Github.repository(owner: "yuki24", name: "artemis")
56
-
57
- request = requests[0]
58
-
59
- expect(request.operation_name).to eq('Github__Repository')
60
- expect(request.variables).to eq("owner" => "yuki24", "name" => "artemis")
61
- expect(request.context).to eq({})
62
- expect(request.document.to_query_string).to eq(<<~GRAPHQL.strip)
63
- query Github__Repository($owner: String!, $name: String!) {
64
- repository(owner: $owner, name: $name) {
65
- name
66
- nameWithOwner
67
- }
68
- }
69
- GRAPHQL
70
- end
71
-
72
- it "can make a GraphQL request with a query that contains fragments" do
73
- Github.user_repositories(login: "yuki24", size: 10)
74
-
75
- request = requests[0]
76
-
77
- expect(request.operation_name).to eq('Github__UserRepositories')
78
- expect(request.variables).to eq('login' => 'yuki24', 'size' => 10)
79
- expect(request.context).to eq({})
80
- expect(request.document.to_query_string).to eq(<<~GRAPHQL.strip)
81
- query Github__UserRepositories($login: String!, $size: Int!) {
82
- user(login: $login) {
83
- id
84
- name
85
- repositories(first: $size) {
86
- nodes {
87
- name
88
- description
89
- ...Github__RepositoryFields
90
- }
91
- }
92
- }
93
- }
94
-
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
- }
106
- }
107
- GRAPHQL
108
- end
109
-
110
- it "can make a GraphQL request with #execute" do
111
- Github.execute(:repository, owner: "yuki24", name: "artemis")
112
-
113
- request = requests[0]
114
-
115
- expect(request.operation_name).to eq('Github__Repository')
116
- expect(request.variables).to eq("owner" => "yuki24", "name" => "artemis")
117
- expect(request.context).to eq({})
118
- expect(request.document.to_query_string).to eq(<<~GRAPHQL.strip)
119
- query Github__Repository($owner: String!, $name: String!) {
120
- repository(owner: $owner, name: $name) {
121
- name
122
- nameWithOwner
123
- }
124
- }
125
- GRAPHQL
126
- end
127
-
128
- it "raises an error when the specified graphql file does not exist" do
129
- expect { Github.execute(:does_not_exist) }
130
- .to raise_error(Artemis::GraphQLFileNotFound)
131
- .with_message(/Query does_not_exist\.graphql not found/)
132
- end
133
-
134
- it "assigns context to the request when provided as an argument" do
135
- context = { headers: { Authorization: 'bearer ...' } }
136
-
137
- Github.repository(owner: "yuki24", name: "artemis", context: context)
138
-
139
- expect(requests[0].context).to eq(context)
140
- end
141
-
142
- it "can create a client that always assigns the provided context to the request" do
143
- context = { headers: { Authorization: 'bearer ...' } }
144
- client = Github.with_context(context)
145
-
146
- client.repository(owner: "yuki24", name: "artemis")
147
- client.repository(owner: "yuki24", name: "artemis")
148
-
149
- expect(requests[0].context).to eq(context)
150
- expect(requests[1].context).to eq(context)
151
- end
152
-
153
- it "assigns the default context to a GraphQL request if present" do
154
- begin
155
- Github.default_context = { headers: { Authorization: 'bearer ...' } }
156
- Github.repository(owner: "yuki24", name: "artemis")
157
-
158
- expect(requests[0].context).to eq(headers: { Authorization: 'bearer ...' })
159
- ensure
160
- Github.default_context = { }
161
- end
162
- end
163
-
164
- it "can make a GraphQL request with all of .default_context, with_context(...) and the :context argument" do
165
- begin
166
- Github.default_context = { headers: { 'User-Agent': 'Artemis', 'X-key': 'value', Authorization: 'token ...' } }
167
- Github
168
- .with_context({ headers: { 'X-key': 'overridden' } })
169
- .repository(owner: "yuki24", name: "artemis", context: { headers: { Authorization: 'bearer ...' } })
170
-
171
- expect(requests[0].context).to eq(
172
- headers: {
173
- 'User-Agent': 'Artemis',
174
- 'X-key': 'overridden',
175
- Authorization: 'bearer ...',
176
- }
177
- )
178
- ensure
179
- Github.default_context = { }
180
- end
181
- end
182
-
183
- it "can batch multiple requests using Multiplex" do
184
- Github.multiplex do |queue|
185
- queue.repository(owner: "yuki24", name: "artemis", context: { headers: { Authorization: 'bearer ...' } })
186
- queue.user
187
- end
188
-
189
- repository_query, user_query = requests[0].queries
190
-
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) {
197
- name
198
- nameWithOwner
199
- }
200
- }
201
- GRAPHQL
202
-
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
211
- }
212
- }
213
- GRAPHQL
214
- end
215
-
216
- private
217
-
218
- def requests
219
- Artemis::Adapters::TestAdapter.requests
220
- end
221
-
222
- def with_files(*files)
223
- files.each {|file| FileUtils.touch(file) }
224
- yield
225
- ensure
226
- files.each {|file| File.delete(file) }
227
- end
228
- end
@@ -1,49 +0,0 @@
1
- describe Artemis::GraphQLEndpoint do
2
- after do
3
- Artemis::GraphQLEndpoint.const_get(:ENDPOINT_INSTANCES).delete("gitlab")
4
- end
5
-
6
- describe ".lookup" do
7
- it "raises an exception when the service is missing" do
8
- expect { Artemis::GraphQLEndpoint.lookup(:does_not_exit) }.to raise_error(Artemis::EndpointNotFound)
9
- end
10
- end
11
-
12
- it "can register an endpoint" do
13
- endpoint = Artemis::GraphQLEndpoint.register!(:gitlab, url: "https://api.gitlab.com/graphql")
14
-
15
- expect(endpoint.url).to eq("https://api.gitlab.com/graphql")
16
- expect(endpoint.connection).to be_instance_of(Artemis::Adapters::NetHttpAdapter)
17
- end
18
-
19
- it "can look up a registered endpoint" do
20
- Artemis::GraphQLEndpoint.register!(:gitlab, url: "https://api.gitlab.com/graphql")
21
-
22
- endpoint = Artemis::GraphQLEndpoint.lookup(:gitlab)
23
-
24
- expect(endpoint.url).to eq("https://api.gitlab.com/graphql")
25
- expect(endpoint.connection).to be_instance_of(Artemis::Adapters::NetHttpAdapter) # Not a fan of this test but for now
26
-
27
- # FIXME: This #schema method makes a network call.
28
- # expect(endpoint.schema).to eq(...)
29
- end
30
-
31
- it "can register an endpoint with options" do
32
- options = {
33
- adapter: :test,
34
- timeout: 10,
35
- # schema_path: nil,
36
- pool_size: 25,
37
- }
38
-
39
- endpoint = Artemis::GraphQLEndpoint.register!(:gitlab, url: "https://api.gitlab.com/graphql", **options)
40
-
41
- expect(endpoint.url).to eq("https://api.gitlab.com/graphql")
42
- expect(endpoint.timeout).to eq(10)
43
- expect(endpoint.pool_size).to eq(25)
44
- expect(endpoint.connection).to be_instance_of(Artemis::Adapters::TestAdapter) # Not a fan of this test but for now
45
-
46
- # FIXME: needs an example schema (and specify the :schema_path option) to test this.
47
- # expect(endpoint.schema).to eq(...)
48
- end
49
- end
@@ -1,12 +0,0 @@
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
- }
@@ -1,6 +0,0 @@
1
- query($owner: String!, $name: String!) {
2
- repository(owner: $owner, name: $name) {
3
- name
4
- nameWithOwner
5
- }
6
- }