graphlient 0.0.5 → 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 74aeacc568a790dea8eb57f285af9d0d4399e32c
4
- data.tar.gz: 92634c7c7715b0e79d5b947180de4a8ace1f6811
3
+ metadata.gz: 70d3714ce3a596ab5742337e24610d2af3b81f36
4
+ data.tar.gz: 9098b09df8009c12031995bf79451a66d4969486
5
5
  SHA512:
6
- metadata.gz: 9d98e5fe0835525a64865f9fffb75608dd4ee435bd8f83dfc1d9a56e47a789884bf868b56f54c98fe581a0017538b64c487cb4b93fd3cdb684f884b5ebc8af78
7
- data.tar.gz: 8d1cf6ad272318317058e7b704c4f38a896293a3729ebb7b32e3874d4d5b76d64bfba15a504b03a1d6c380b4d07a7dd721267edc08e15a9bdc146242e0fe52c0
6
+ metadata.gz: 2b3eb5c108cb8cc1a32958e9bf8524957a848ac25829aaa3f00b09ac69ca2faca56ffd35d8e533990d6e868f0f4d39b8bcbd3849c8e23a8d3270d18cbc2ff268
7
+ data.tar.gz: 782ae549c2377b76b9f3452e8086124407bd2228a6fcbe1fce2c54a20ba4663f2155d2349a139c9f2725990ce7c6a6d5ae883b3780fe12a1df74794c654a519f
data/.gitignore CHANGED
@@ -9,7 +9,8 @@
9
9
  /test/tmp/
10
10
  /test/version_tmp/
11
11
  /tmp/
12
-
12
+ .byebug_history
13
+ .DS_Store
13
14
  # Used by dotenv library to load environment variables.
14
15
  # .env
15
16
 
@@ -1,47 +1,62 @@
1
1
  # This configuration was generated by
2
2
  # `rubocop --auto-gen-config`
3
- # on 2017-10-04 21:00:57 -0400 using RuboCop version 0.47.1.
3
+ # on 2017-10-19 15:16:01 -0400 using RuboCop version 0.47.1.
4
4
  # The point is for the user to remove these configuration records
5
5
  # one by one as the offenses are removed from the code base.
6
6
  # Note that changes in the inspected code, or installation of new
7
7
  # versions of RuboCop, may require this file to be generated again.
8
8
 
9
- # Offense count: 4
9
+ # Offense count: 11
10
10
  # Configuration parameters: CountComments, ExcludedMethods.
11
11
  Metrics/BlockLength:
12
- Max: 38
12
+ Max: 136
13
13
 
14
- # Offense count: 7
14
+ # Offense count: 17
15
15
  # Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns.
16
16
  # URISchemes: http, https
17
17
  Metrics/LineLength:
18
- Max: 143
18
+ Max: 177
19
19
 
20
- # Offense count: 6
20
+ # Offense count: 2
21
+ # Configuration parameters: CountComments.
22
+ Metrics/MethodLength:
23
+ Max: 12
24
+
25
+ # Offense count: 1
26
+ # Configuration parameters: EnforcedStyle, SupportedStyles.
27
+ # SupportedStyles: nested, compact
28
+ Style/ClassAndModuleChildren:
29
+ Exclude:
30
+ - 'spec/graphlient/static_client_query_spec.rb'
31
+
32
+ # Offense count: 5
21
33
  Style/Documentation:
22
34
  Exclude:
23
35
  - 'spec/**/*'
24
36
  - 'test/**/*'
37
+ - 'lib/graphlient/adapters/faraday_adapter.rb'
25
38
  - 'lib/graphlient/client.rb'
26
- - 'lib/graphlient/config.rb'
27
- - 'lib/graphlient/errors/http.rb'
39
+ - 'lib/graphlient/errors/error.rb'
28
40
  - 'lib/graphlient/extensions/query.rb'
29
41
  - 'lib/graphlient/query.rb'
30
42
 
43
+ # Offense count: 2
44
+ # Cop supports --auto-correct.
45
+ # Configuration parameters: EnforcedStyle, SupportedStyles.
46
+ # SupportedStyles: line_count_dependent, lambda, literal
47
+ Style/Lambda:
48
+ Exclude:
49
+ - 'spec/support/mutations/create_invoice_mutation.rb'
50
+ - 'spec/support/queries/query.rb'
51
+
31
52
  # Offense count: 2
32
53
  Style/MethodMissing:
33
54
  Exclude:
34
55
  - 'lib/graphlient/extensions/query.rb'
35
56
  - 'lib/graphlient/query.rb'
36
57
 
37
- # Offense count: 1
38
- # Configuration parameters: EnforcedStyle, SupportedStyles.
39
- # SupportedStyles: module_function, extend_self
40
- Style/ModuleFunction:
41
- Exclude:
42
- - 'lib/graphlient/config.rb'
43
-
44
- # Offense count: 1
58
+ # Offense count: 2
45
59
  Style/MultilineBlockChain:
46
60
  Exclude:
47
- - 'spec/graphlient/client_spec.rb'
61
+ - 'spec/graphlient/client_query_spec.rb'
62
+ - 'spec/graphlient/client_schema_spec.rb'
@@ -3,5 +3,11 @@ language: ruby
3
3
  cache: bundler
4
4
 
5
5
  rvm:
6
- - 2.3.0
7
- - 2.4.1
6
+ - 2.3.1
7
+ - 2.4.1
8
+
9
+ matrix:
10
+ include:
11
+ - rvm: 2.3.1
12
+ script:
13
+ - bundle exec danger
@@ -1,3 +1,14 @@
1
+ ### 0.0.6 (10/20/2017)
2
+
3
+ * [#14](https://github.com/ashkan18/graphlient/pull/14): Switch to `graphql-client` for network calls and schema validation - [@ashkan18](https://github.com/ashkan18).
4
+ * [#17](https://github.com/ashkan18/graphlient/pull/17): Specialize server errors as `Graphlient::Errors::Server` - [@dblock](https://github.com/dblock).
5
+ * [#13](https://github.com/ashkan18/graphlient/pull/13): Support named queries and make sure, this is braking change where we no longer support queries that don't start with `query` - [@ashkan18](https://github.com/ashkan18).
6
+ * [#21](https://github.com/ashkan18/graphlient/pull/21): Added danger, PR linter - [@dblock](https://github.com/dblock).
7
+ * [#17](https://github.com/ashkan18/graphlient/pull/17): Enable customizing of Faraday middleware - [@dblock](https://github.com/dblock).
8
+ * [#19](https://github.com/ashkan18/graphlient/pull/19): Expose `client.schema` - [@dblock](https://github.com/dblock).
9
+ * [#20](https://github.com/ashkan18/graphlient/pull/20): Added support for parameterized queries and mutations - [@dblock](https://github.com/dblock).
10
+ * [#25](https://github.com/ashkan18/graphlient/pull/25): Added `client.parse` and `client.execute` to parse and execute queries separately - [@dblock](https://github.com/dblock).
11
+
1
12
  ### 0.0.5 (10/5/2017)
2
13
 
3
14
  * [#11](https://github.com/ashkan18/graphlient/pull/11): Fixed query argument types - [@ashkan18](https://github.com/ashkan18).
@@ -0,0 +1 @@
1
+ changelog.check
data/Gemfile CHANGED
@@ -6,11 +6,16 @@ gem 'rake'
6
6
 
7
7
  group :development do
8
8
  gem 'byebug'
9
+ gem 'danger-changelog', '~> 0.2.1'
9
10
  gem 'rubocop', '~> 0.47.1', require: false
10
11
  end
11
12
 
12
13
  group :test do
14
+ gem 'graphql'
15
+ gem 'rack-parser'
16
+ gem 'rack-test'
13
17
  gem 'rspec'
14
18
  gem 'rspec-mocks'
19
+ gem 'sinatra'
15
20
  gem 'webmock'
16
21
  end
data/README.md CHANGED
@@ -3,7 +3,7 @@
3
3
  [![Gem Version](https://badge.fury.io/rb/graphlient.svg)](https://badge.fury.io/rb/graphlient)
4
4
  [![Build Status](https://travis-ci.org/ashkan18/graphlient.svg?branch=master)](https://travis-ci.org/ashkan18/graphlient)
5
5
 
6
- A Ruby Client for consuming GraphQL-based APIs.
6
+ A much friendlier Ruby client for consuming GraphQL-based APIs, without all the messy strings. Built on top of your usual [graphql-client](https://github.com/github/graphql-client).
7
7
 
8
8
  ## Installation
9
9
 
@@ -15,11 +15,7 @@ gem 'graphlient'
15
15
 
16
16
  ## Usage
17
17
 
18
- There are 3 different ways to use this library.
19
-
20
- ### Graphlient::Client
21
-
22
- Create a new instance of `Graphlient::Client` and pass the query into a block.
18
+ Create a new instance of `Graphlient::Client` with a URL and optional headers.
23
19
 
24
20
  ```ruby
25
21
  client = Graphlient::Client.new('https://test-graphql.biz/graphql',
@@ -27,39 +23,172 @@ client = Graphlient::Client.new('https://test-graphql.biz/graphql',
27
23
  'Authorization' => 'Bearer 123'
28
24
  }
29
25
  )
26
+ ```
27
+
28
+ The schema is available automatically via `.schema`.
29
+
30
+ ```ruby
31
+ client.schema # GraphQL::Schema
32
+ ```
30
33
 
34
+ Make queries with `query`, which takes a block for the query definition.
35
+
36
+ ```ruby
31
37
  response = client.query do
32
- invoice(id: 10) do
38
+ query do
39
+ invoice(id: 10) do
40
+ id
41
+ total
42
+ line_items do
43
+ price
44
+ item_type
45
+ end
46
+ end
47
+ end
48
+ end
49
+ ```
50
+
51
+ This will call the endpoint setup in the configuration with `POST`, the `Authorization` header and `query` as follows.
52
+
53
+ ```graphql
54
+ query {
55
+ invoice(id: 10) {
33
56
  id
34
57
  total
35
- line_items do
58
+ line_items {
36
59
  price
37
60
  item_type
61
+ }
62
+ }
63
+ }
64
+ ```
65
+
66
+ Graphlient validates the query based on current schema. In case of validation errors or any other connection related issues you'll get `Graphlient::Errors::Client` describing the error and in case of transport errors, `Graphlient::Errors::Server`. Both inherit from `Graphlient::Errors::Error` if you need to handle them in bulk.
67
+
68
+ A successful response object always contains data which can be iterated upon. The following example returns the first line item's price.
69
+
70
+ ```ruby
71
+ response.data.invoice.line_items.first.price
72
+ ```
73
+
74
+ You can also execute mutations the same way.
75
+
76
+ ```ruby
77
+ response = client.query do
78
+ mutation do
79
+ createInvoice(input: { fee_in_cents: 12_345 }) do
80
+ id
81
+ fee_in_cents
38
82
  end
39
83
  end
40
84
  end
41
85
  ```
42
86
 
43
- This will call the endpoint setup in the configuration with `POST`, the `Authorization` header and `query` as
87
+ The successful response contains data in `response.data`. The following example returns the newly created invoice's ID.
44
88
 
45
- ```graphql
46
- invoice(id: 10) {
47
- id
48
- total
49
- line_items {
50
- price
51
- item_type
52
- }
53
- }
89
+ ```ruby
90
+ response.data.create_invoice.first.id
91
+ ```
92
+
93
+ ### Executing Parameterized Queries and Mutations
94
+
95
+ Graphlient can execute parameterized queries and mutations by providing variables as query parameters.
96
+
97
+ The following query accepts an array of IDs.
98
+
99
+ ```ruby
100
+ client.query(ids: [42]) do
101
+ query(:$ids => :'[Int]') do
102
+ invoices(ids: :$ids) do
103
+ id
104
+ fee_in_cents
105
+ end
106
+ end
107
+ end
54
108
  ```
55
109
 
56
- A successful response is a JSON object.
110
+ The following mutation accepts a custom type that requires `fee_in_cents`.
57
111
 
58
- On failure the client raises `Graphlient::Errors::HTTP` which contains the `response` object.
112
+ ```ruby
113
+ client.query(input: { fee_in_cents: 12_345 }) do
114
+ mutation(:$input => :createInvoiceInput!) do
115
+ createInvoice(input: :$input) do
116
+ id
117
+ fee_in_cents
118
+ end
119
+ end
120
+ end
121
+ ```
59
122
 
60
- ### Use Graphlient::Query directly
123
+ ### Parse and Execute Queries Separately
124
+
125
+ You can `parse` and `execute` queries separately with optional variables. This is highly recommended as parsing a query and validating a query on every request adds performance overhead. Parsing queries early allows validation errors to be discovered before request time and avoids many potential security issues.
61
126
 
62
- You can directly use `Graphlient::Query` to generate GraphQL queries. Example:
127
+
128
+ ```ruby
129
+ # parse a query, returns a GraphQL::Client::OperationDefinition
130
+ query = client.parse do
131
+ query(:$ids => :'[Int]') do
132
+ invoices(ids: :$ids) do
133
+ id
134
+ fee_in_cents
135
+ end
136
+ end
137
+ end
138
+
139
+ # execute a query, returns a GraphQL::Client::Response
140
+ client.execute query, ids: [42]
141
+ ```
142
+
143
+ ### Dynamic vs. Static Queries
144
+
145
+ Graphlient uses [graphql-client](https://github.com/github/graphql-client), which [recommends](https://github.com/github/graphql-client/blob/master/guides/dynamic-query-error.md) building queries as static module members along with dynamic variables during execution. This can be accomplished with graphlient the same way.
146
+
147
+ Create a new instance of `Graphlient::Client` with a URL and optional headers.
148
+
149
+ ```ruby
150
+ module SWAPI
151
+ Client = Graphlient::Client.new('https://test-graphql.biz/graphql',
152
+ headers: {
153
+ 'Authorization' => 'Bearer 123'
154
+ },
155
+ allow_dynamic_queries: false
156
+ )
157
+ end
158
+ ```
159
+
160
+ The schema is available automatically via `.schema`.
161
+
162
+ ```ruby
163
+ SWAPI::Client.schema # GraphQL::Schema
164
+ ```
165
+
166
+ Define a query.
167
+
168
+ ```ruby
169
+ module SWAPI
170
+ InvoiceQuery = Client.parse do
171
+ query(:$id => :Int) do
172
+ invoice(id: :$id) do
173
+ id
174
+ fee_in_cents
175
+ end
176
+ end
177
+ end
178
+ end
179
+ ```
180
+
181
+ Execute the query.
182
+
183
+ ```ruby
184
+ response = SWAPI::Client.execute(SWAPI::InvoiceQuery, id: 42)
185
+ ```
186
+
187
+ Note that in the example above the client is created with `allow_dynamic_queries: false` (only allow static queries), while graphlient defaults to `allow_dynamic_queries: true` (allow dynamic queries). This option is marked deprecated, but we're proposing to remove it and default it to `true` in [graphql-client#128](https://github.com/github/graphql-client/issues/128).
188
+
189
+ ### Generate Queries with Graphlient::Query
190
+
191
+ You can directly use `Graphlient::Query` to generate raw GraphQL queries.
63
192
 
64
193
  ```ruby
65
194
  query = Graphlient::Query.new do
@@ -71,24 +200,69 @@ query = Graphlient::Query.new do
71
200
  end
72
201
 
73
202
  query.to_s
74
- # "\nquery{\n invoice(id: 10){\n line_items\n }\n }\n"
203
+ # "\nquery {\n invoice(id: 10){\n line_items\n }\n }\n"
75
204
  ```
76
205
 
77
- ### Use Graphlient::Extension::Query
206
+ ### Create API Client Classes with Graphlient::Extension::Query
78
207
 
79
208
  You can include `Graphlient::Extensions::Query` in your class. This will add a new `method_missing` method to your context which will be used to generate GraphQL queries.
80
209
 
81
210
  ```ruby
82
211
  include Graphlient::Extensions::Query
83
212
 
84
- query = invoice(id: 10) do
85
- line_items
213
+ query = query do
214
+ invoice(id: 10) do
215
+ line_items
216
+ end
86
217
  end
87
218
 
88
219
  query.to_s
89
220
  # "\nquery{\n invoice(id: 10){\n line_items\n }\n }\n"
90
221
  ```
91
222
 
223
+ ### Testing with Graphlient and RSpec
224
+
225
+ Use Graphlient inside your RSpec tests in a Rails application or with `Rack::Test`, no more messy HTTP POSTs.
226
+
227
+ ```ruby
228
+ require 'spec_helper'
229
+
230
+ describe App do
231
+ include Rack::Test::Methods
232
+
233
+ def app
234
+ # ...
235
+ end
236
+
237
+ let(:client) do
238
+ Graphlient::Client.new('http://test-graphql.biz/graphql') do |client|
239
+ client.http do |h|
240
+ h.connection do |c|
241
+ c.use Faraday::Adapter::Rack, app
242
+ end
243
+ end
244
+ end
245
+ end
246
+
247
+ context 'an invoice' do
248
+ let(:result) do
249
+ client.query do
250
+ query do
251
+ invoice(id: 10) do
252
+ id
253
+ end
254
+ end
255
+ end
256
+ end
257
+
258
+ it 'can be retrieved' do
259
+ expect(result.data.invoice.id).to eq 10
260
+ end
261
+ end
262
+ end
263
+ ```
264
+
92
265
  ## License
93
266
 
94
267
  MIT License, see [LICENSE](LICENSE)
268
+
@@ -24,7 +24,7 @@ Remove the line with "Your contribution here.", since there will be no more cont
24
24
  Commit your changes.
25
25
 
26
26
  ```
27
- git add README.md CHANGELOG.md
27
+ git add CHANGELOG.md
28
28
  git commit -m "Preparing for release, 0.2.2."
29
29
  git push origin master
30
30
  ```
data/Rakefile CHANGED
@@ -12,4 +12,4 @@ end
12
12
  require 'rubocop/rake_task'
13
13
  RuboCop::RakeTask.new(:rubocop)
14
14
 
15
- task default: [:rubocop, :spec]
15
+ task default: %i(rubocop spec)