graphlient 0.8.0 → 0.9.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.github/ISSUE_TEMPLATE/bug_report.md +38 -38
- data/.github/ISSUE_TEMPLATE/feature_request.md +20 -20
- data/.github/workflows/ci.yml +33 -30
- data/.github/workflows/danger-comment.yml +10 -0
- data/.github/workflows/danger.yml +13 -22
- data/.github/workflows/rubocop.yml +19 -19
- data/.gitignore +59 -52
- data/.rspec +1 -1
- data/.rubocop.yml +26 -28
- data/.rubocop_todo.yml +72 -48
- data/CHANGELOG.md +128 -113
- data/CONTRIBUTING.md +125 -125
- data/Dangerfile +24 -24
- data/Gemfile +35 -27
- data/LICENSE +21 -21
- data/README.md +800 -533
- data/RELEASING.md +63 -63
- data/Rakefile +15 -15
- data/UPGRADING.md +23 -23
- data/graphlient.gemspec +19 -19
- data/lib/graphlient/adapters/http/adapter.rb +41 -41
- data/lib/graphlient/adapters/http/faraday_adapter.rb +79 -79
- data/lib/graphlient/adapters/http/http_adapter.rb +40 -39
- data/lib/graphlient/adapters/http.rb +3 -3
- data/lib/graphlient/adapters.rb +1 -1
- data/lib/graphlient/client.rb +98 -80
- data/lib/graphlient/errors/client_error.rb +6 -6
- data/lib/graphlient/errors/connection_failed_error.rb +6 -6
- data/lib/graphlient/errors/error.rb +13 -12
- data/lib/graphlient/errors/execution_error.rb +29 -29
- data/lib/graphlient/errors/faraday_server_error.rb +12 -12
- data/lib/graphlient/errors/graphql_error.rb +53 -52
- data/lib/graphlient/errors/http_options_error.rb +6 -6
- data/lib/graphlient/errors/http_server_error.rb +13 -13
- data/lib/graphlient/errors/server_error.rb +7 -7
- data/lib/graphlient/errors/timeout_error.rb +6 -6
- data/lib/graphlient/errors.rb +10 -10
- data/lib/graphlient/extensions/query.rb +15 -15
- data/lib/graphlient/extensions.rb +1 -1
- data/lib/graphlient/query/directive.rb +47 -0
- data/lib/graphlient/query/serializer/arguments.rb +67 -0
- data/lib/graphlient/query/serializer/directives.rb +17 -0
- data/lib/graphlient/query/serializer/evaluator.rb +15 -0
- data/lib/graphlient/query/serializer/fragments.rb +28 -0
- data/lib/graphlient/query/serializer/scalars.rb +63 -0
- data/lib/graphlient/query/serializer.rb +153 -0
- data/lib/graphlient/query.rb +29 -128
- data/lib/graphlient/schema.rb +27 -26
- data/lib/graphlient/version.rb +3 -3
- data/lib/graphlient.rb +8 -8
- data/spec/graphlient/adapters/http/faraday_adapter_spec.rb +172 -167
- data/spec/graphlient/adapters/http/http_adapter_spec.rb +60 -60
- data/spec/graphlient/client_dsl_spec.rb +153 -0
- data/spec/graphlient/client_query_spec.rb +369 -346
- data/spec/graphlient/client_schema_spec.rb +75 -75
- data/spec/graphlient/extensions/query_spec.rb +16 -16
- data/spec/graphlient/github_query_spec.rb +32 -32
- data/spec/graphlient/query_dsl_spec.rb +231 -0
- data/spec/graphlient/query_spec.rb +105 -105
- data/spec/graphlient/schema_spec.rb +56 -56
- data/spec/graphlient/static_client_query_spec.rb +75 -68
- data/spec/graphlient/webmock_client_query_spec.rb +41 -41
- data/spec/spec_helper.rb +26 -14
- data/spec/support/context/dummy_client.rb +33 -26
- data/spec/support/context/github_client.rb +18 -18
- data/spec/support/dummy_app.rb +21 -19
- data/spec/support/dummy_schema.rb +17 -17
- data/spec/support/fixtures/github/schema.yml +14282 -14282
- data/spec/support/fixtures/github/user.yml +76 -76
- data/spec/support/fixtures/github/viewer.yml +76 -76
- data/spec/support/fixtures/invoice_api.json +1288 -1288
- data/spec/support/mutations/create_invoice.rb +18 -18
- data/spec/support/queries/query.rb +48 -47
- data/spec/support/schema/github.json +44479 -44479
- data/spec/support/types/invoice_type.rb +13 -13
- data/spec/support/types/mutation_type.rb +5 -5
- data/spec/support/vcr.rb +9 -9
- metadata +13 -8
- data/Gemfile.danger +0 -5
data/RELEASING.md
CHANGED
|
@@ -1,63 +1,63 @@
|
|
|
1
|
-
# Releasing Graphlient
|
|
2
|
-
|
|
3
|
-
There're no hard rules about when to release graphlient. Release bug fixes frequently, features not so frequently and breaking API changes rarely.
|
|
4
|
-
|
|
5
|
-
### Release
|
|
6
|
-
|
|
7
|
-
Run tests, check that all tests succeed locally.
|
|
8
|
-
|
|
9
|
-
```
|
|
10
|
-
bundle install
|
|
11
|
-
rake
|
|
12
|
-
```
|
|
13
|
-
|
|
14
|
-
Check that the last build succeeded in [Travis CI](https://travis-ci.org/dblock/graphlient) for all supported platforms.
|
|
15
|
-
|
|
16
|
-
Change next release in [CHANGELOG.md](CHANGELOG.md) to the new version.
|
|
17
|
-
|
|
18
|
-
```
|
|
19
|
-
### 0.2.2 (7/10/2015)
|
|
20
|
-
```
|
|
21
|
-
|
|
22
|
-
Remove the line with "Your contribution here.", since there will be no more contributions to this release.
|
|
23
|
-
|
|
24
|
-
Update the version in `lib/graphlient/version.rb`.
|
|
25
|
-
|
|
26
|
-
Commit your changes.
|
|
27
|
-
|
|
28
|
-
```
|
|
29
|
-
git add CHANGELOG.md
|
|
30
|
-
git commit -m "Preparing for release, 0.2.2."
|
|
31
|
-
git push origin master
|
|
32
|
-
```
|
|
33
|
-
|
|
34
|
-
Release.
|
|
35
|
-
|
|
36
|
-
```
|
|
37
|
-
$ rake release
|
|
38
|
-
|
|
39
|
-
graphlient 0.2.2 built to pkg/graphlient-0.2.2.gem.
|
|
40
|
-
Tagged v0.2.2.
|
|
41
|
-
Pushed git commits and tags.
|
|
42
|
-
Pushed graphlient 0.2.2 to rubygems.org.
|
|
43
|
-
```
|
|
44
|
-
|
|
45
|
-
### Prepare for the Next Version
|
|
46
|
-
|
|
47
|
-
Add the next release to [CHANGELOG.md](CHANGELOG.md).
|
|
48
|
-
|
|
49
|
-
```
|
|
50
|
-
### 0.2.3 (Next)
|
|
51
|
-
|
|
52
|
-
* Your contribution here.
|
|
53
|
-
```
|
|
54
|
-
|
|
55
|
-
Increment the third version number in [lib/graphlient/version.rb](lib/graphlient/version.rb). Usually the major version is incremented during the development cycle when the release contains major features or breaking API changes (eg. change `0.2.1` to `0.3.0`).
|
|
56
|
-
|
|
57
|
-
Commit your changes.
|
|
58
|
-
|
|
59
|
-
```
|
|
60
|
-
git add CHANGELOG.md lib/graphlient/version.rb
|
|
61
|
-
git commit -m "Preparing for next development iteration, 0.2.3."
|
|
62
|
-
git push origin master
|
|
63
|
-
```
|
|
1
|
+
# Releasing Graphlient
|
|
2
|
+
|
|
3
|
+
There're no hard rules about when to release graphlient. Release bug fixes frequently, features not so frequently and breaking API changes rarely.
|
|
4
|
+
|
|
5
|
+
### Release
|
|
6
|
+
|
|
7
|
+
Run tests, check that all tests succeed locally.
|
|
8
|
+
|
|
9
|
+
```
|
|
10
|
+
bundle install
|
|
11
|
+
rake
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
Check that the last build succeeded in [Travis CI](https://travis-ci.org/dblock/graphlient) for all supported platforms.
|
|
15
|
+
|
|
16
|
+
Change next release in [CHANGELOG.md](CHANGELOG.md) to the new version.
|
|
17
|
+
|
|
18
|
+
```
|
|
19
|
+
### 0.2.2 (7/10/2015)
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
Remove the line with "Your contribution here.", since there will be no more contributions to this release.
|
|
23
|
+
|
|
24
|
+
Update the version in `lib/graphlient/version.rb`.
|
|
25
|
+
|
|
26
|
+
Commit your changes.
|
|
27
|
+
|
|
28
|
+
```
|
|
29
|
+
git add CHANGELOG.md
|
|
30
|
+
git commit -m "Preparing for release, 0.2.2."
|
|
31
|
+
git push origin master
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
Release.
|
|
35
|
+
|
|
36
|
+
```
|
|
37
|
+
$ rake release
|
|
38
|
+
|
|
39
|
+
graphlient 0.2.2 built to pkg/graphlient-0.2.2.gem.
|
|
40
|
+
Tagged v0.2.2.
|
|
41
|
+
Pushed git commits and tags.
|
|
42
|
+
Pushed graphlient 0.2.2 to rubygems.org.
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
### Prepare for the Next Version
|
|
46
|
+
|
|
47
|
+
Add the next release to [CHANGELOG.md](CHANGELOG.md).
|
|
48
|
+
|
|
49
|
+
```
|
|
50
|
+
### 0.2.3 (Next)
|
|
51
|
+
|
|
52
|
+
* Your contribution here.
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
Increment the third version number in [lib/graphlient/version.rb](lib/graphlient/version.rb). Usually the major version is incremented during the development cycle when the release contains major features or breaking API changes (eg. change `0.2.1` to `0.3.0`).
|
|
56
|
+
|
|
57
|
+
Commit your changes.
|
|
58
|
+
|
|
59
|
+
```
|
|
60
|
+
git add CHANGELOG.md lib/graphlient/version.rb
|
|
61
|
+
git commit -m "Preparing for next development iteration, 0.2.3."
|
|
62
|
+
git push origin master
|
|
63
|
+
```
|
data/Rakefile
CHANGED
|
@@ -1,15 +1,15 @@
|
|
|
1
|
-
require 'rubygems'
|
|
2
|
-
require 'bundler/gem_tasks'
|
|
3
|
-
Bundler.setup(:default, :development)
|
|
4
|
-
|
|
5
|
-
require 'rspec/core'
|
|
6
|
-
require 'rspec/core/rake_task'
|
|
7
|
-
|
|
8
|
-
RSpec::Core::RakeTask.new(:spec) do |spec|
|
|
9
|
-
spec.pattern = FileList['spec/**/*_spec.rb']
|
|
10
|
-
end
|
|
11
|
-
|
|
12
|
-
require 'rubocop/rake_task'
|
|
13
|
-
RuboCop::RakeTask.new(:rubocop)
|
|
14
|
-
|
|
15
|
-
task default: %i[rubocop spec]
|
|
1
|
+
require 'rubygems'
|
|
2
|
+
require 'bundler/gem_tasks'
|
|
3
|
+
Bundler.setup(:default, :development)
|
|
4
|
+
|
|
5
|
+
require 'rspec/core'
|
|
6
|
+
require 'rspec/core/rake_task'
|
|
7
|
+
|
|
8
|
+
RSpec::Core::RakeTask.new(:spec) do |spec|
|
|
9
|
+
spec.pattern = FileList['spec/**/*_spec.rb']
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
require 'rubocop/rake_task'
|
|
13
|
+
RuboCop::RakeTask.new(:rubocop)
|
|
14
|
+
|
|
15
|
+
task default: %i[rubocop spec]
|
data/UPGRADING.md
CHANGED
|
@@ -1,23 +1,23 @@
|
|
|
1
|
-
Upgrading Graphlient
|
|
2
|
-
===========================
|
|
3
|
-
|
|
4
|
-
### Upgrading to >= 0.4.0
|
|
5
|
-
|
|
6
|
-
#### Requires Faraday >= 1.0
|
|
7
|
-
|
|
8
|
-
See [#75](https://github.com/ashkan18/graphlient/pull/75).
|
|
9
|
-
|
|
10
|
-
#### Changes in error handling of connection refused error
|
|
11
|
-
|
|
12
|
-
When the GraphQL request was failing, we were receiving a `Faraday::ServerError`. After 0.4.0, Graphlient
|
|
13
|
-
will return `Graphlient::Errors::FaradayServerError` instead.
|
|
14
|
-
|
|
15
|
-
### Upgrading to >= 0.3.7
|
|
16
|
-
|
|
17
|
-
#### Changes in error handling of connection refused error
|
|
18
|
-
|
|
19
|
-
Prior to 0.3.7, Graphlient would return `NoMethodError: undefined method []' for nil:NilClass` error if connection is
|
|
20
|
-
refused/failed when connecting to a remote host. After 0.3.7, Graphlient will return a new
|
|
21
|
-
`Graphlient::Errors::ConnectionFailedError` instead.
|
|
22
|
-
|
|
23
|
-
See [#68](https://github.com/ashkan18/graphlient/pull/68) for more information.
|
|
1
|
+
Upgrading Graphlient
|
|
2
|
+
===========================
|
|
3
|
+
|
|
4
|
+
### Upgrading to >= 0.4.0
|
|
5
|
+
|
|
6
|
+
#### Requires Faraday >= 1.0
|
|
7
|
+
|
|
8
|
+
See [#75](https://github.com/ashkan18/graphlient/pull/75).
|
|
9
|
+
|
|
10
|
+
#### Changes in error handling of connection refused error
|
|
11
|
+
|
|
12
|
+
When the GraphQL request was failing, we were receiving a `Faraday::ServerError`. After 0.4.0, Graphlient
|
|
13
|
+
will return `Graphlient::Errors::FaradayServerError` instead.
|
|
14
|
+
|
|
15
|
+
### Upgrading to >= 0.3.7
|
|
16
|
+
|
|
17
|
+
#### Changes in error handling of connection refused error
|
|
18
|
+
|
|
19
|
+
Prior to 0.3.7, Graphlient would return `NoMethodError: undefined method []' for nil:NilClass` error if connection is
|
|
20
|
+
refused/failed when connecting to a remote host. After 0.3.7, Graphlient will return a new
|
|
21
|
+
`Graphlient::Errors::ConnectionFailedError` instead.
|
|
22
|
+
|
|
23
|
+
See [#68](https://github.com/ashkan18/graphlient/pull/68) for more information.
|
data/graphlient.gemspec
CHANGED
|
@@ -1,19 +1,19 @@
|
|
|
1
|
-
$LOAD_PATH.push File.expand_path('lib', __dir__)
|
|
2
|
-
|
|
3
|
-
require 'graphlient/version'
|
|
4
|
-
|
|
5
|
-
Gem::Specification.new do |s|
|
|
6
|
-
s.name = 'graphlient'
|
|
7
|
-
s.version = Graphlient::VERSION
|
|
8
|
-
s.authors = ['Ashkan Nasseri']
|
|
9
|
-
s.email = 'ashkan.nasseri@gmail.com'
|
|
10
|
-
s.platform = Gem::Platform::RUBY
|
|
11
|
-
s.required_rubygems_version = '>= 1.3.6'
|
|
12
|
-
s.files = `git ls-files`.split("\n")
|
|
13
|
-
s.require_paths = ['lib']
|
|
14
|
-
s.homepage = 'http://github.com/ashkan18/graphlient'
|
|
15
|
-
s.licenses = ['MIT']
|
|
16
|
-
s.summary = 'A friendlier Ruby client for consuming GraphQL-based APIs.'
|
|
17
|
-
s.add_dependency 'faraday', '~> 2.0'
|
|
18
|
-
s.add_dependency 'graphql-client'
|
|
19
|
-
end
|
|
1
|
+
$LOAD_PATH.push File.expand_path('lib', __dir__)
|
|
2
|
+
|
|
3
|
+
require 'graphlient/version'
|
|
4
|
+
|
|
5
|
+
Gem::Specification.new do |s|
|
|
6
|
+
s.name = 'graphlient'
|
|
7
|
+
s.version = Graphlient::VERSION
|
|
8
|
+
s.authors = ['Ashkan Nasseri']
|
|
9
|
+
s.email = 'ashkan.nasseri@gmail.com'
|
|
10
|
+
s.platform = Gem::Platform::RUBY
|
|
11
|
+
s.required_rubygems_version = '>= 1.3.6'
|
|
12
|
+
s.files = `git ls-files`.split("\n")
|
|
13
|
+
s.require_paths = ['lib']
|
|
14
|
+
s.homepage = 'http://github.com/ashkan18/graphlient'
|
|
15
|
+
s.licenses = ['MIT']
|
|
16
|
+
s.summary = 'A friendlier Ruby client for consuming GraphQL-based APIs.'
|
|
17
|
+
s.add_dependency 'faraday', '~> 2.0'
|
|
18
|
+
s.add_dependency 'graphql-client'
|
|
19
|
+
end
|
|
@@ -1,41 +1,41 @@
|
|
|
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 http_options
|
|
18
|
-
return {} unless options
|
|
19
|
-
|
|
20
|
-
options[:http_options] || {}
|
|
21
|
-
end
|
|
22
|
-
|
|
23
|
-
def execute(*)
|
|
24
|
-
raise NotImplementedError
|
|
25
|
-
end
|
|
26
|
-
|
|
27
|
-
private
|
|
28
|
-
|
|
29
|
-
def configure_http_options(client_options)
|
|
30
|
-
http_options.each do |k, v|
|
|
31
|
-
begin
|
|
32
|
-
client_options.send("#{k}=", v)
|
|
33
|
-
rescue NoMethodError => e
|
|
34
|
-
raise Graphlient::Errors::HttpOptionsError, e.message
|
|
35
|
-
end
|
|
36
|
-
end
|
|
37
|
-
end
|
|
38
|
-
end
|
|
39
|
-
end
|
|
40
|
-
end
|
|
41
|
-
end
|
|
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 http_options
|
|
18
|
+
return {} unless options
|
|
19
|
+
|
|
20
|
+
options[:http_options] || {}
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def execute(*)
|
|
24
|
+
raise NotImplementedError
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
private
|
|
28
|
+
|
|
29
|
+
def configure_http_options(client_options)
|
|
30
|
+
http_options.each do |k, v|
|
|
31
|
+
begin
|
|
32
|
+
client_options.send("#{k}=", v)
|
|
33
|
+
rescue NoMethodError => e
|
|
34
|
+
raise Graphlient::Errors::HttpOptionsError, e.message
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
end
|
|
@@ -1,79 +1,79 @@
|
|
|
1
|
-
require 'faraday'
|
|
2
|
-
require 'json'
|
|
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
|
|
15
|
-
}.to_json
|
|
16
|
-
end
|
|
17
|
-
|
|
18
|
-
parse_body(response.body)
|
|
19
|
-
rescue Faraday::ConnectionFailed => e
|
|
20
|
-
raise Graphlient::Errors::ConnectionFailedError, e
|
|
21
|
-
rescue Faraday::TimeoutError => e
|
|
22
|
-
raise Graphlient::Errors::TimeoutError, e
|
|
23
|
-
rescue Faraday::ClientError => e
|
|
24
|
-
raise Graphlient::Errors::FaradayServerError, e
|
|
25
|
-
rescue Faraday::ServerError => e
|
|
26
|
-
raise Graphlient::Errors::FaradayServerError, 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
|
-
|
|
35
|
-
configure_http_options(c.options)
|
|
36
|
-
|
|
37
|
-
if block_given?
|
|
38
|
-
yield c
|
|
39
|
-
else
|
|
40
|
-
c.adapter Faraday::Adapter::NetHttp
|
|
41
|
-
end
|
|
42
|
-
end
|
|
43
|
-
end
|
|
44
|
-
|
|
45
|
-
private
|
|
46
|
-
|
|
47
|
-
# Faraday 2.x's JSON response middleware will only parse a JSON
|
|
48
|
-
# response body into a Hash (or Array) object if the response headers
|
|
49
|
-
# match a specific content type regex. See Faraday's response JSON
|
|
50
|
-
# middleware definition for specifics on what the datatype of the
|
|
51
|
-
# response body will be. This method will handle the situation where
|
|
52
|
-
# the response header is not set appropriately, but contains JSON
|
|
53
|
-
# anyways. If the body cannot be parsed as JSON, an exception will be
|
|
54
|
-
# raised.
|
|
55
|
-
def parse_body(body)
|
|
56
|
-
case body
|
|
57
|
-
when Hash, Array
|
|
58
|
-
body
|
|
59
|
-
when String
|
|
60
|
-
begin
|
|
61
|
-
JSON.parse(body)
|
|
62
|
-
rescue JSON::ParserError
|
|
63
|
-
raise Graphlient::Errors::ServerError, 'Failed to parse response body as JSON'
|
|
64
|
-
end
|
|
65
|
-
else
|
|
66
|
-
inner_exception = StandardError.new <<~ERR.strip.tr("\n", ' ')
|
|
67
|
-
Unexpected response body type '#{body.class}'. Graphlient doesn't
|
|
68
|
-
know how to handle a response body of this type, but Faraday is
|
|
69
|
-
returning it. Please open an issue, particularly if the response
|
|
70
|
-
body does actually contain valid JSON.
|
|
71
|
-
ERR
|
|
72
|
-
|
|
73
|
-
raise Graphlient::Errors::ClientError, inner_exception
|
|
74
|
-
end
|
|
75
|
-
end
|
|
76
|
-
end
|
|
77
|
-
end
|
|
78
|
-
end
|
|
79
|
-
end
|
|
1
|
+
require 'faraday'
|
|
2
|
+
require 'json'
|
|
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
|
|
15
|
+
}.to_json
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
parse_body(response.body)
|
|
19
|
+
rescue Faraday::ConnectionFailed => e
|
|
20
|
+
raise Graphlient::Errors::ConnectionFailedError, e
|
|
21
|
+
rescue Faraday::TimeoutError => e
|
|
22
|
+
raise Graphlient::Errors::TimeoutError, e
|
|
23
|
+
rescue Faraday::ClientError => e
|
|
24
|
+
raise Graphlient::Errors::FaradayServerError, e
|
|
25
|
+
rescue Faraday::ServerError => e
|
|
26
|
+
raise Graphlient::Errors::FaradayServerError, 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
|
+
|
|
35
|
+
configure_http_options(c.options)
|
|
36
|
+
|
|
37
|
+
if block_given?
|
|
38
|
+
yield c
|
|
39
|
+
else
|
|
40
|
+
c.adapter Faraday::Adapter::NetHttp
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
private
|
|
46
|
+
|
|
47
|
+
# Faraday 2.x's JSON response middleware will only parse a JSON
|
|
48
|
+
# response body into a Hash (or Array) object if the response headers
|
|
49
|
+
# match a specific content type regex. See Faraday's response JSON
|
|
50
|
+
# middleware definition for specifics on what the datatype of the
|
|
51
|
+
# response body will be. This method will handle the situation where
|
|
52
|
+
# the response header is not set appropriately, but contains JSON
|
|
53
|
+
# anyways. If the body cannot be parsed as JSON, an exception will be
|
|
54
|
+
# raised.
|
|
55
|
+
def parse_body(body)
|
|
56
|
+
case body
|
|
57
|
+
when Hash, Array
|
|
58
|
+
body
|
|
59
|
+
when String
|
|
60
|
+
begin
|
|
61
|
+
JSON.parse(body)
|
|
62
|
+
rescue JSON::ParserError
|
|
63
|
+
raise Graphlient::Errors::ServerError, 'Failed to parse response body as JSON'
|
|
64
|
+
end
|
|
65
|
+
else
|
|
66
|
+
inner_exception = StandardError.new <<~ERR.strip.tr("\n", ' ')
|
|
67
|
+
Unexpected response body type '#{body.class}'. Graphlient doesn't
|
|
68
|
+
know how to handle a response body of this type, but Faraday is
|
|
69
|
+
returning it. Please open an issue, particularly if the response
|
|
70
|
+
body does actually contain valid JSON.
|
|
71
|
+
ERR
|
|
72
|
+
|
|
73
|
+
raise Graphlient::Errors::ClientError, inner_exception
|
|
74
|
+
end
|
|
75
|
+
end
|
|
76
|
+
end
|
|
77
|
+
end
|
|
78
|
+
end
|
|
79
|
+
end
|
|
@@ -1,39 +1,40 @@
|
|
|
1
|
-
require 'graphql/client/http'
|
|
2
|
-
|
|
3
|
-
module Graphlient
|
|
4
|
-
module Adapters
|
|
5
|
-
module HTTP
|
|
6
|
-
class HTTPAdapter < Adapter
|
|
7
|
-
def execute(document:, operation_name: nil, variables: {}, context: {})
|
|
8
|
-
request = Net::HTTP::Post.new(url)
|
|
9
|
-
|
|
10
|
-
request['Accept'] = 'application/json'
|
|
11
|
-
request['Content-Type'] = 'application/json'
|
|
12
|
-
headers && headers.each { |name, value| request[name] = value }
|
|
13
|
-
|
|
14
|
-
body = {}
|
|
15
|
-
body['query'] = document.to_query_string
|
|
16
|
-
body['variables'] = variables if variables.any?
|
|
17
|
-
body['operationName'] = operation_name if operation_name
|
|
18
|
-
request.body = JSON.generate(body)
|
|
19
|
-
|
|
20
|
-
response = connection.request(request)
|
|
21
|
-
raise Graphlient::Errors::HttpServerError, response unless response.is_a?(Net::HTTPOK)
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
end
|
|
1
|
+
require 'graphql/client/http'
|
|
2
|
+
|
|
3
|
+
module Graphlient
|
|
4
|
+
module Adapters
|
|
5
|
+
module HTTP
|
|
6
|
+
class HTTPAdapter < Adapter
|
|
7
|
+
def execute(document:, operation_name: nil, variables: {}, context: {})
|
|
8
|
+
request = Net::HTTP::Post.new(url)
|
|
9
|
+
|
|
10
|
+
request['Accept'] = 'application/json'
|
|
11
|
+
request['Content-Type'] = 'application/json'
|
|
12
|
+
headers && headers.each { |name, value| request[name] = value }
|
|
13
|
+
|
|
14
|
+
body = {}
|
|
15
|
+
body['query'] = document.to_query_string
|
|
16
|
+
body['variables'] = variables if variables.any?
|
|
17
|
+
body['operationName'] = operation_name if operation_name
|
|
18
|
+
request.body = JSON.generate(body)
|
|
19
|
+
|
|
20
|
+
response = connection.request(request)
|
|
21
|
+
raise Graphlient::Errors::HttpServerError, response unless response.is_a?(Net::HTTPOK)
|
|
22
|
+
|
|
23
|
+
JSON.parse(response.body)
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def uri
|
|
27
|
+
@uri ||= URI(url)
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def connection
|
|
31
|
+
Net::HTTP.new(uri.host, uri.port).tap do |client|
|
|
32
|
+
client.use_ssl = uri.scheme == 'https'
|
|
33
|
+
|
|
34
|
+
configure_http_options(client)
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
end
|
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
require_relative 'http/adapter'
|
|
2
|
-
require_relative 'http/faraday_adapter'
|
|
3
|
-
require_relative 'http/http_adapter'
|
|
1
|
+
require_relative 'http/adapter'
|
|
2
|
+
require_relative 'http/faraday_adapter'
|
|
3
|
+
require_relative 'http/http_adapter'
|
data/lib/graphlient/adapters.rb
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
require_relative 'adapters/http'
|
|
1
|
+
require_relative 'adapters/http'
|