graphlient 0.0.8 → 0.0.9

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: 0da18e4b9370c65fb4e6add0b644c89b55149b32
4
- data.tar.gz: 39a55d067517d0be0f609ea3fcea3836cc0c4e9c
3
+ metadata.gz: 3243a1525db30d3bf16a4bc9fc699e07c829a3d5
4
+ data.tar.gz: 37d6d5fc32ce427967b06d3975b80d393af333a8
5
5
  SHA512:
6
- metadata.gz: d443d7ca4023923bce79d474e59bab4e7f85fb2b4f270fd8466435997c89dfb8bb763821728b428d7e8dde0f196b4f2e4ae8e9747c805e11dbae301707b7326f
7
- data.tar.gz: 3bf4e9568f17b0a26566a1d79f422395ce545a6273ca37c8d177a858734e78dcee1d3a5706678e0613998a67d63d7b309dec0be14b9edef0342759b9943d5917
6
+ metadata.gz: 29a39b7c44150ad3e9182b98e14ed407a688fffefb6a2fa1a62644130cb6c4c04bc694584d6cd6dab5b0b9321911d905b5a902a50aa11fcf75ac6747b1c15d7b
7
+ data.tar.gz: d665ba561261fa682ec71888810d9767f3f9a1a6ad4d8186832454d0220fbcfc1ad9957a9ca1af8136019c6848f20cae849e94c2a57812d73cd822d97fd15f76
@@ -1,4 +1,19 @@
1
1
  Style/FrozenStringLiteralComment:
2
2
  Enabled: false
3
3
 
4
+ Style/Documentation:
5
+ Enabled: false
6
+
7
+ Metrics/LineLength:
8
+ Enabled: false
9
+
10
+ Metrics/MethodLength:
11
+ Enabled: false
12
+
13
+ Metrics/BlockLength:
14
+ Enabled: false
15
+
16
+ Metrics/AbcSize:
17
+ Enabled: false
18
+
4
19
  inherit_from: .rubocop_todo.yml
@@ -1,6 +1,6 @@
1
1
  # This configuration was generated by
2
2
  # `rubocop --auto-gen-config`
3
- # on 2017-10-25 13:18:43 -0400 using RuboCop version 0.47.1.
3
+ # on 2017-10-26 13:24:56 -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
@@ -8,27 +8,10 @@
8
8
 
9
9
  # Offense count: 1
10
10
  # Cop supports --auto-correct.
11
- # Configuration parameters: EnforcedStyleAlignWith, SupportedStylesAlignWith, AutoCorrect.
12
- # SupportedStylesAlignWith: keyword, variable, start_of_line
13
- Lint/EndAlignment:
11
+ # Configuration parameters: AllowUnusedKeywordArguments, IgnoreEmptyMethods.
12
+ Lint/UnusedMethodArgument:
14
13
  Exclude:
15
- - 'lib/graphlient/client.rb'
16
-
17
- # Offense count: 11
18
- # Configuration parameters: CountComments, ExcludedMethods.
19
- Metrics/BlockLength:
20
- Max: 178
21
-
22
- # Offense count: 19
23
- # Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns.
24
- # URISchemes: http, https
25
- Metrics/LineLength:
26
- Max: 177
27
-
28
- # Offense count: 2
29
- # Configuration parameters: CountComments.
30
- Metrics/MethodLength:
31
- Max: 12
14
+ - 'lib/graphlient/adapters/http/http_adapter.rb'
32
15
 
33
16
  # Offense count: 1
34
17
  # Configuration parameters: EnforcedStyle, SupportedStyles.
@@ -37,18 +20,6 @@ Style/ClassAndModuleChildren:
37
20
  Exclude:
38
21
  - 'spec/graphlient/static_client_query_spec.rb'
39
22
 
40
- # Offense count: 6
41
- Style/Documentation:
42
- Exclude:
43
- - 'spec/**/*'
44
- - 'test/**/*'
45
- - 'lib/graphlient/adapters/faraday_adapter.rb'
46
- - 'lib/graphlient/client.rb'
47
- - 'lib/graphlient/errors/error.rb'
48
- - 'lib/graphlient/errors/graphql.rb'
49
- - 'lib/graphlient/extensions/query.rb'
50
- - 'lib/graphlient/query.rb'
51
-
52
23
  # Offense count: 2
53
24
  # Cop supports --auto-correct.
54
25
  # Configuration parameters: EnforcedStyle, SupportedStyles.
@@ -64,7 +35,7 @@ Style/MethodMissing:
64
35
  - 'lib/graphlient/extensions/query.rb'
65
36
  - 'lib/graphlient/query.rb'
66
37
 
67
- # Offense count: 2
38
+ # Offense count: 3
68
39
  Style/MultilineBlockChain:
69
40
  Exclude:
70
41
  - 'spec/graphlient/client_query_spec.rb'
@@ -1,3 +1,8 @@
1
+ ### 0.0.9 (10/26/2017)
2
+
3
+ * [#28](https://github.com/ashkan18/graphlient/pull/28): Raise errors in `execute`, not only `query` - [@dblock](https://github.com/dblock).
4
+ * [#29](https://github.com/ashkan18/graphlient/pull/29): Added `Graphlient::Adapters::HTTP::HTTPAdapter` that replaces Faraday with `Net::HTTP` - [@dblock](https://github.com/dblock).
5
+
1
6
  ### 0.0.8 (10/26/2017)
2
7
 
3
8
  * [#27](https://github.com/ashkan18/graphlient/pull/27): Always raise an exception unless a query has succeeded - [@dblock](https://github.com/dblock).
data/README.md CHANGED
@@ -265,9 +265,19 @@ query.to_s
265
265
  # "\nquery{\n invoice(id: 10){\n line_items\n }\n }\n"
266
266
  ```
267
267
 
268
+ ### Swapping the HTTP Stack
269
+
270
+ You can swap the default Faraday adapter for `Net::HTTP`.
271
+
272
+ ```ruby
273
+ client = Graphlient::Client.new('https://test-graphql.biz/graphql',
274
+ http: Graphlient::Adapters::HTTP::HTTPAdapter
275
+ )
276
+ ```
277
+
268
278
  ### Testing with Graphlient and RSpec
269
279
 
270
- Use Graphlient inside your RSpec tests in a Rails application or with `Rack::Test`, no more messy HTTP POSTs.
280
+ Use Graphlient inside your RSpec tests in a Rails application or with `Rack::Test` against your actual application.
271
281
 
272
282
  ```ruby
273
283
  require 'spec_helper'
@@ -307,6 +317,26 @@ describe App do
307
317
  end
308
318
  ```
309
319
 
320
+ Alternately you can `stub_request` with Webmock.
321
+
322
+ ```ruby
323
+ describe App do
324
+ let(:url) { 'http://example.com/graphql' }
325
+ let(:client) { Graphlient::Client.new(url) }
326
+
327
+ before do
328
+ stub_request(:post, url).to_return(
329
+ status: 200,
330
+ body: DummySchema.execute(GraphQL::Introspection::INTROSPECTION_QUERY).to_json
331
+ )
332
+ end
333
+
334
+ it 'retrieves schema' do
335
+ expect(client.schema).to be_a GraphQL::Schema
336
+ end
337
+ end
338
+ ```
339
+
310
340
  ## License
311
341
 
312
342
  MIT License, see [LICENSE](LICENSE)
@@ -1,5 +1,7 @@
1
+ require 'graphql/client'
1
2
  require 'graphlient/version'
2
3
  require 'graphlient/extensions'
3
4
  require 'graphlient/errors'
4
5
  require 'graphlient/query'
6
+ require 'graphlient/adapters'
5
7
  require 'graphlient/client'
@@ -0,0 +1 @@
1
+ require_relative 'adapters/http'
@@ -0,0 +1,3 @@
1
+ require_relative 'http/adapter'
2
+ require_relative 'http/faraday_adapter'
3
+ require_relative 'http/http_adapter'
@@ -0,0 +1,23 @@
1
+ module Graphlient
2
+ module Adapters
3
+ module HTTP
4
+ class Adapter
5
+ attr_accessor :url, :options
6
+
7
+ def initialize(url, options = {}, &_block)
8
+ @url = url
9
+ @options = options.dup if options
10
+ yield self if block_given?
11
+ end
12
+
13
+ def headers
14
+ options[:headers] if options
15
+ end
16
+
17
+ def execute(*)
18
+ raise NotImplementedError
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,37 @@
1
+ require 'faraday'
2
+ require 'faraday_middleware'
3
+
4
+ module Graphlient
5
+ module Adapters
6
+ module HTTP
7
+ class FaradayAdapter < Adapter
8
+ def execute(document:, operation_name:, variables:, context:)
9
+ response = connection.post do |req|
10
+ req.headers.merge!(context[:headers] || {})
11
+ req.body = {
12
+ query: document.to_query_string,
13
+ operationName: operation_name,
14
+ variables: variables.to_json
15
+ }.to_json
16
+ end
17
+ response.body
18
+ rescue Faraday::ClientError => e
19
+ raise Graphlient::Errors::Server.new(e.message, e)
20
+ end
21
+
22
+ def connection
23
+ @connection ||= Faraday.new(url: url, headers: headers) do |c|
24
+ c.use Faraday::Response::RaiseError
25
+ c.request :json
26
+ c.response :json
27
+ if block_given?
28
+ yield c
29
+ else
30
+ c.use Faraday::Adapter::NetHttp
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,39 @@
1
+ require 'graphql/client/http'
2
+
3
+ module Graphlient
4
+ module Adapters
5
+ module HTTP
6
+ class HTTPAdapter < Adapter
7
+ attr_reader :uri
8
+
9
+ def execute(document:, operation_name: nil, variables: {}, context: {})
10
+ request = Net::HTTP::Post.new(url)
11
+
12
+ request['Accept'] = 'application/json'
13
+ request['Content-Type'] = 'application/json'
14
+ headers&.each { |name, value| request[name] = value }
15
+
16
+ body = {}
17
+ body['query'] = document.to_query_string
18
+ body['variables'] = variables if variables.any?
19
+ body['operationName'] = operation_name if operation_name
20
+ request.body = JSON.generate(body)
21
+
22
+ response = connection.request(request)
23
+ raise Graphlient::Errors::Server.new("the server responded with status #{response.code}", response) unless response.is_a?(Net::HTTPOK)
24
+ JSON.parse(response.body)
25
+ end
26
+
27
+ def uri
28
+ @uri ||= URI(url)
29
+ end
30
+
31
+ def connection
32
+ Net::HTTP.new(uri.host, uri.port).tap do |client|
33
+ client.use_ssl = uri.scheme == 'https'
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
@@ -1,6 +1,3 @@
1
- require 'graphql/client'
2
- require 'graphlient/adapters/faraday_adapter'
3
-
4
1
  module Graphlient
5
2
  class Client
6
3
  attr_accessor :uri, :options
@@ -16,6 +13,8 @@ module Graphlient
16
13
  instance_eval(&block)
17
14
  end
18
15
  client.parse(query_str.to_s)
16
+ rescue GraphQL::Client::Error => e
17
+ raise Graphlient::Errors::Client.new(e.message, e)
19
18
  end
20
19
 
21
20
  def execute(query, variables = nil)
@@ -23,25 +22,27 @@ module Graphlient
23
22
  query_params[:context] = @options if @options
24
23
  query_params[:variables] = variables if variables
25
24
  query = client.parse(query) if query.is_a?(String)
26
- client.query(query, query_params)
25
+ rc = client.query(query, query_params)
26
+ raise Graphlient::Errors::GraphQL, rc if rc.errors.any?
27
+ rc
27
28
  rescue GraphQL::Client::Error => e
28
29
  raise Graphlient::Errors::Client.new(e.message, e)
29
30
  end
30
31
 
31
32
  def query(query_or_variables = nil, variables = nil, &block)
32
- rc = if block_given?
33
- execute(parse(&block), query_or_variables)
34
- else
35
- execute(query_or_variables, variables)
33
+ if block_given?
34
+ execute(parse(&block), query_or_variables)
35
+ else
36
+ execute(query_or_variables, variables)
36
37
  end
37
- raise Graphlient::Errors::GraphQL, rc if rc.errors.any?
38
- rc
39
- rescue GraphQL::Client::Error => e
40
- raise Graphlient::Errors::Client.new(e.message, e)
38
+ end
39
+
40
+ def http_adapter_class
41
+ options[:http] || Adapters::HTTP::FaradayAdapter
41
42
  end
42
43
 
43
44
  def http(&block)
44
- @http ||= Adapters::FaradayAdapter.new(@url, headers: @options[:headers], &block)
45
+ @http ||= http_adapter_class.new(@url, headers: @options[:headers], &block)
45
46
  end
46
47
 
47
48
  def schema
@@ -1,3 +1,3 @@
1
1
  module Graphlient
2
- VERSION = '0.0.8'.freeze
2
+ VERSION = '0.0.9'.freeze
3
3
  end
@@ -1,6 +1,6 @@
1
1
  require 'spec_helper'
2
2
 
3
- describe Graphlient::Adapters::FaradayAdapter do
3
+ describe Graphlient::Adapters::HTTP::FaradayAdapter do
4
4
  let(:app) { Object.new }
5
5
 
6
6
  context 'with a custom middleware' do
@@ -41,4 +41,20 @@ describe Graphlient::Adapters::FaradayAdapter do
41
41
  expect(client.http.headers).to eq headers
42
42
  end
43
43
  end
44
+
45
+ context 'default' do
46
+ let(:url) { 'http://example.com/graphql' }
47
+ let(:client) { Graphlient::Client.new(url) }
48
+
49
+ before do
50
+ stub_request(:post, url).to_return(
51
+ status: 200,
52
+ body: DummySchema.execute(GraphQL::Introspection::INTROSPECTION_QUERY).to_json
53
+ )
54
+ end
55
+
56
+ it 'retrieves schema' do
57
+ expect(client.schema).to be_a GraphQL::Schema
58
+ end
59
+ end
44
60
  end
@@ -0,0 +1,41 @@
1
+ require 'spec_helper'
2
+
3
+ describe Graphlient::Adapters::HTTP::HTTPAdapter do
4
+ let(:app) { Object.new }
5
+
6
+ context 'with custom url and headers' do
7
+ let(:url) { 'http://example.com/graphql' }
8
+ let(:headers) { { 'Foo' => 'bar' } }
9
+ let(:client) do
10
+ Graphlient::Client.new(url, headers: headers, http: Graphlient::Adapters::HTTP::HTTPAdapter)
11
+ end
12
+
13
+ it 'sets adapter' do
14
+ expect(client.http).to be_a Graphlient::Adapters::HTTP::HTTPAdapter
15
+ end
16
+
17
+ it 'sets url' do
18
+ expect(client.http.url).to eq url
19
+ end
20
+
21
+ it 'sets headers' do
22
+ expect(client.http.headers).to eq headers
23
+ end
24
+ end
25
+
26
+ context 'default' do
27
+ let(:url) { 'http://example.com/graphql' }
28
+ let(:client) { Graphlient::Client.new(url, http: Graphlient::Adapters::HTTP::HTTPAdapter) }
29
+
30
+ before do
31
+ stub_request(:post, url).to_return(
32
+ status: 200,
33
+ body: DummySchema.execute(GraphQL::Introspection::INTROSPECTION_QUERY).to_json
34
+ )
35
+ end
36
+
37
+ it 'retrieves schema' do
38
+ expect(client.schema).to be_a GraphQL::Schema
39
+ end
40
+ end
41
+ end
@@ -75,6 +75,14 @@ describe Graphlient::Client do
75
75
  expect(invoices.first.id).to eq 42
76
76
  expect(invoices.first.fee_in_cents).to eq 20_000
77
77
  end
78
+
79
+ it 'fails when wrong input type' do
80
+ expect do
81
+ client.execute(query, ids: ['42'])
82
+ end.to raise_error Graphlient::Errors::GraphQL do |e|
83
+ expect(e.to_s).to eq "Variable ids of type [Int] was provided invalid value\n 0: Could not coerce value \"42\" to Int"
84
+ end
85
+ end
78
86
  end
79
87
  end
80
88
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: graphlient
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.8
4
+ version: 0.0.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ashkan Nasseri
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-10-26 00:00:00.000000000 Z
11
+ date: 2017-10-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: graphql-client
@@ -74,7 +74,11 @@ files:
74
74
  - Rakefile
75
75
  - graphlient.gemspec
76
76
  - lib/graphlient.rb
77
- - lib/graphlient/adapters/faraday_adapter.rb
77
+ - lib/graphlient/adapters.rb
78
+ - lib/graphlient/adapters/http.rb
79
+ - lib/graphlient/adapters/http/adapter.rb
80
+ - lib/graphlient/adapters/http/faraday_adapter.rb
81
+ - lib/graphlient/adapters/http/http_adapter.rb
78
82
  - lib/graphlient/client.rb
79
83
  - lib/graphlient/errors.rb
80
84
  - lib/graphlient/errors/client.rb
@@ -85,7 +89,8 @@ files:
85
89
  - lib/graphlient/extensions/query.rb
86
90
  - lib/graphlient/query.rb
87
91
  - lib/graphlient/version.rb
88
- - spec/graphlient/adapters/faraday_adapter_spec.rb
92
+ - spec/graphlient/adapters/http/faraday_adapter_spec.rb
93
+ - spec/graphlient/adapters/http/http_adapter_spec.rb
89
94
  - spec/graphlient/client_query_spec.rb
90
95
  - spec/graphlient/client_schema_spec.rb
91
96
  - spec/graphlient/extensions/query_spec.rb
@@ -119,7 +124,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
119
124
  version: 1.3.6
120
125
  requirements: []
121
126
  rubyforge_project:
122
- rubygems_version: 2.5.1
127
+ rubygems_version: 2.6.12
123
128
  signing_key:
124
129
  specification_version: 4
125
130
  summary: Ruby Gem for consuming GraphQL endpoints
@@ -1,43 +0,0 @@
1
- require 'faraday'
2
- require 'faraday_middleware'
3
-
4
- module Graphlient
5
- module Adapters
6
- class FaradayAdapter
7
- attr_accessor :url, :headers
8
-
9
- def initialize(url, headers:, &_block)
10
- @url = url
11
- @headers = headers.dup if headers
12
- yield self if block_given?
13
- end
14
-
15
- def execute(document:, operation_name:, variables:, context:)
16
- response = connection.post do |req|
17
- req.headers.merge!(context[:headers] || {})
18
- req.body = {
19
- query: document.to_query_string,
20
- operationName: operation_name,
21
- variables: variables.to_json
22
- }.to_json
23
- end
24
- response.body
25
- rescue Faraday::ClientError => e
26
- raise Graphlient::Errors::Server.new(e.message, e)
27
- end
28
-
29
- def connection
30
- @connection ||= Faraday.new(url: url, headers: headers) do |c|
31
- c.use Faraday::Response::RaiseError
32
- c.request :json
33
- c.response :json
34
- if block_given?
35
- yield c
36
- else
37
- c.use Faraday::Adapter::NetHttp
38
- end
39
- end
40
- end
41
- end
42
- end
43
- end