graphlient 0.5.0 → 0.8.0

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
  SHA256:
3
- metadata.gz: c049e978a0c48e45fe84d49470bc890ae3b6ea86b86aab8158322616c62968dd
4
- data.tar.gz: c7307c2b5c6ac6ac2b55ee4d416bbfffee1e29ed0826916254077add10370ee9
3
+ metadata.gz: ed5f47ad98345af440481f9dca800767524807bc667eace999d7ba36826e59fa
4
+ data.tar.gz: 22ec20f5b725b3bb85bc9c8e8371693824f7fd85b2db43336ce7f6a6be31543f
5
5
  SHA512:
6
- metadata.gz: 627d0d04d5ecea5e4c33ea0e59b15ac46fc458f76f179ac624d3519036865ad29267f629b1b3fda7592a50c846ea2cf06cc58a8f188a06caee44715268c18d07
7
- data.tar.gz: 9742adef6879fbd08b00350674dced4b4886e476b45388d6d1a1afaaebba440153632e878649ff68beb223b158f365dd180b6db76e8463a13dbbeb4cdf8aa88d
6
+ metadata.gz: 1c5ddbaafe24dfbdd6e99535bedb7a021eba55a956b4b716661c9c8778018a96118afc5a6def029aad14da883de8d47a747b4a85ead2cee0868d5ddcfb298359
7
+ data.tar.gz: 3fd8b40976687fd83b00b865f62d514a87571de0bf68015486328d526a2d9cc76a8c4454a5d78f86acba6480860ac1abde3ff2edda1e92c26b03c58f60b067e2
@@ -0,0 +1,38 @@
1
+ ---
2
+ name: Bug report
3
+ about: Create a report to help us improve
4
+ title: ''
5
+ labels: ''
6
+ assignees: ''
7
+
8
+ ---
9
+
10
+ **Describe the bug**
11
+ A clear and concise description of what the bug is.
12
+
13
+ **To Reproduce**
14
+ Steps to reproduce the behavior:
15
+ 1. Go to '...'
16
+ 2. Click on '....'
17
+ 3. Scroll down to '....'
18
+ 4. See error
19
+
20
+ **Expected behavior**
21
+ A clear and concise description of what you expected to happen.
22
+
23
+ **Screenshots**
24
+ If applicable, add screenshots to help explain your problem.
25
+
26
+ **Desktop (please complete the following information):**
27
+ - OS: [e.g. iOS]
28
+ - Browser [e.g. chrome, safari]
29
+ - Version [e.g. 22]
30
+
31
+ **Smartphone (please complete the following information):**
32
+ - Device: [e.g. iPhone6]
33
+ - OS: [e.g. iOS8.1]
34
+ - Browser [e.g. stock browser, safari]
35
+ - Version [e.g. 22]
36
+
37
+ **Additional context**
38
+ Add any other context about the problem here.
@@ -0,0 +1,20 @@
1
+ ---
2
+ name: Feature request
3
+ about: Suggest an idea for this project
4
+ title: ''
5
+ labels: ''
6
+ assignees: ''
7
+
8
+ ---
9
+
10
+ **Is your feature request related to a problem? Please describe.**
11
+ A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]
12
+
13
+ **Describe the solution you'd like**
14
+ A clear and concise description of what you want to happen.
15
+
16
+ **Describe alternatives you've considered**
17
+ A clear and concise description of any alternative solutions or features you've considered.
18
+
19
+ **Additional context**
20
+ Add any other context or screenshots about the feature request here.
@@ -0,0 +1,30 @@
1
+ name: CI
2
+
3
+ on: [push, pull_request]
4
+
5
+ permissions:
6
+ contents: read
7
+
8
+ jobs:
9
+ test:
10
+ runs-on: ubuntu-latest
11
+ strategy:
12
+ matrix:
13
+ entry:
14
+ - { ruby: 2.7.2 }
15
+ - { ruby: 3.0.0 }
16
+ - { ruby: 3.1.2 }
17
+ - { ruby: 3.2.2 }
18
+ - { ruby: "ruby-head", ignore: true }
19
+ - { ruby: "jruby-9.3.9.0", ignore: true }
20
+ - { ruby: "jruby-head", ignore: true }
21
+ steps:
22
+ - uses: actions/checkout@v3
23
+ - name: Set up Ruby
24
+ uses: ruby/setup-ruby@v1
25
+ with:
26
+ ruby-version: ${{ matrix.entry.ruby }}
27
+ bundler-cache: true
28
+ - name: Run tests
29
+ continue-on-error: ${{ matrix.entry.ignore || false }}
30
+ run: bundle exec rspec spec/
@@ -0,0 +1,22 @@
1
+ name: Danger
2
+
3
+ on: pull_request
4
+
5
+ jobs:
6
+ danger:
7
+ runs-on: ubuntu-latest
8
+ env:
9
+ BUNDLE_GEMFILE: ${{ github.workspace }}/Gemfile.danger
10
+ steps:
11
+ - uses: actions/checkout@v3
12
+ with:
13
+ fetch-depth: 0
14
+ - name: Set up Ruby
15
+ uses: ruby/setup-ruby@v1
16
+ with:
17
+ ruby-version: 2.7.2
18
+ bundler-cache: true
19
+ - run: |
20
+ # the personal token is public, this is ok, base64 encode to avoid tripping Github
21
+ TOKEN=$(echo -n Z2hwX0xNQ3VmanBFeTBvYkZVTWh6NVNqVFFBOEUxU25abzBqRUVuaAo= | base64 --decode)
22
+ DANGER_GITHUB_API_TOKEN=$TOKEN bundle exec danger --verbose
@@ -0,0 +1,19 @@
1
+ name: Rubocop
2
+
3
+ on: [push, pull_request]
4
+
5
+ permissions:
6
+ contents: read
7
+
8
+ jobs:
9
+ rubocop:
10
+ runs-on: ubuntu-latest
11
+ steps:
12
+ - uses: actions/checkout@v3
13
+ - name: Set up Ruby
14
+ uses: ruby/setup-ruby@v1
15
+ with:
16
+ ruby-version: 2.7.2
17
+ bundler-cache: true
18
+ - name: Run rubocop
19
+ run: bundle exec rubocop
data/CHANGELOG.md CHANGED
@@ -1,74 +1,93 @@
1
- ### 0.6.0 (Next)
1
+ ### (Next)
2
2
  * Your contribution here.
3
3
 
4
- ### 0.5.0 (12/28/2020)
4
+ ### 0.8.0 (2024/01/06)
5
+ * [#110](https://github.com/ashkan18/graphlient/pull/110): Ensure correct Faraday JSON response body parsing with invalid response header - [@taylorthurlow](https://github.com/taylorthurlow).
6
+ * [#107](https://github.com/ashkan18/graphlient/pull/107): Pass in initialized schema as an option - [@kbaum](https://github.com/kbaum).
7
+ * [#106](https://github.com/ashkan18/graphlient/pull/106): Add 3.2 to the list of ruby ci versions - [@igor-drozdov](https://github.com/igor-drozdov).
8
+ * [#102](https://github.com/ashkan18/graphlient/pull/102): Update ci to test latest jruby - [@ashkan18](https://github.com/ashkan18).
9
+
10
+ ### 0.7.0 (2022/10/11)
11
+ * [#98](https://github.com/ashkan18/graphlient/pull/98): Bring back danger checks and improve them - [@ashkan18](https://github.com/ashkan18).
12
+ * [#94](https://github.com/ashkan18/graphlient/pull/94): Enabled fragments - [@rellampec](https://github.com/rellampec).
13
+ * [#95](https://github.com/ashkan18/graphlient/pull/95): Upgrade faraday dependency to version 2 - [@kirillkaiumov](https://github.com/kirillkaiumov).
14
+
15
+ ### 0.6.0 (2022/06/11)
16
+
17
+ * [#87](https://github.com/ashkan18/graphlient/pull/87): Raised `ExecutionError` with partial error response - [@QQism](https://github.com/QQism).
18
+ * [#90](https://github.com/ashkan18/graphlient/pull/90): Added support for Ruby 3.1 - [@QQism](https://github.com/QQism).
19
+ * [#90](https://github.com/ashkan18/graphlient/pull/90): Dropped support for Ruby 2.5 - [@QQism](https://github.com/QQism).
20
+ * [#91](https://github.com/ashkan18/graphlient/pull/91): Update GHA for `danger` with right permissions - [@QQism](https://github.com/QQism).
21
+ * [#89](https://github.com/ashkan18/graphlient/pull/89): Replace Travis CI with Github Actions - [@QQism](https://github.com/QQism).
22
+
23
+ ### 0.5.0 (2020/12/28)
5
24
 
6
25
  * [#81](https://github.com/ashkan18/graphlient/pull/81): Make graphlient run on ruby 3.0 - [@Burgestrand](https://github.com/Burgestrand).
7
26
  * [#79](https://github.com/ashkan18/graphlient/pull/79): Added client testing docs - [@GabrielDzul](https://github.com/GabrielDzul).
8
27
 
9
- ### 0.4.0 (5/22/2020)
28
+ ### 0.4.0 (2020/05/22)
10
29
 
11
30
  * [#72](https://github.com/ashkan18/graphlient/pull/72): Add http_options - [@neroleung](https://github.com/neroleung).
12
31
  * [#71](https://github.com/ashkan18/graphlient/issues/70): Add `Graphlient::Errors::TimeoutError` - [@BenDrozdoff](https://github.com/BenDrozdoff).
13
32
  * [#75](https://github.com/ashkan18/graphlient/pull/75): Support Faraday 1.x - [@jfhinchcliffe](https://github.com/jfhinchcliffe).
14
33
  * [#78](https://github.com/ashkan18/graphlient/pull/78): Add description of timeout values - [@sap1enza](https://github.com/sap1enza).
15
34
 
16
- ### 0.3.7 (11/14/2019)
35
+ ### 0.3.7 (2019/11/14)
17
36
 
18
37
  * [#68](https://github.com/ashkan18/graphlient/pull/68): Add `Graphlient::Errors::ConnectionFailedError` - [@neroleung](https://github.com/neroleung).
19
38
 
20
- ### 0.3.6 (07/23/2019)
39
+ ### 0.3.6 (2019/07/23)
21
40
 
22
41
  * [#63](https://github.com/ashkan18/graphlient/pull/63): Remove unused method for attribute with typo - [@ashkan18](https://github.com/ashkan18).
23
42
  * [#62](https://github.com/ashkan18/graphlient/pull/62): Fix typo preventing access to response object on error - [@jmondo](https://github.com/jmondo).
24
43
 
25
- ### 0.3.4 (01/31/2019)
44
+ ### 0.3.4 (2019/01/31)
26
45
 
27
46
  * [#56](https://github.com/ashkan18/graphlient/pull/56): Remove safe navigation usage to retain support for Ruby 2.2 - [@avinoth](https://github.com/avinoth).
28
47
  * [#57](https://github.com/ashkan18/graphlient/pull/57): Add support for parsing queries from a String - [@ateamlunchbox](https://github.com/ateamlunchbox).
29
48
 
30
- ### 0.3.3 (09/23/2018)
49
+ ### 0.3.3 (2018/09/23)
31
50
 
32
51
  * [#50](https://github.com/ashkan18/graphlient/pull/50): More detailed error responses - [@ashkan18](https://github.com/ashkan18).
33
52
 
34
- ### 0.3.2 (07/03/2018)
53
+ ### 0.3.2 (2018/07/03)
35
54
 
36
55
  * [#46](https://github.com/ashkan18/graphlient/pull/46): Fix issue with gathering error details when trying `to_s` on `GraphQLError` - [@ashkan18](https://github.com/ashkan18).
37
56
  * [#45](https://github.com/ashkan18/graphlient/pull/45): Drop Support for Ruby 2.2 and Lock RuboCop - [@jonallured](https://github.com/jonallured).
38
57
 
39
- ### 0.3.1 (04/17/2018)
58
+ ### 0.3.1 (2018/04/17)
40
59
 
41
60
  * [#43](https://github.com/ashkan18/graphlient/pull/43): Allow to load and dump schema to json - [@povilasjurcys](https://github.com/povilasjurcys).
42
61
 
43
- ### 0.3.0 (02/22/2018)
62
+ ### 0.3.0 (2018/02/22)
44
63
 
45
64
  * [#38](https://github.com/ashkan18/graphlient/pull/38): Add support for Ruby 2.5 - [@yuki24](https://github.com/yuki24).
46
65
  * [#39](https://github.com/ashkan18/graphlient/pull/39): Add support for Ruby 2.2 - [@yuki24](https://github.com/yuki24).
47
66
  * [#40](https://github.com/ashkan18/graphlient/pull/40): Add experimental support for JRuby - [@yuki24](https://github.com/yuki24).
48
67
 
49
- ### 0.2.0 (11/09/2017)
68
+ ### 0.2.0 (2017/11/09)
50
69
 
51
70
  * [#33](https://github.com/ashkan18/graphlient/pull/33): Added dsl for supporting parametrized queries/mutations - [@ashkan18](https://github.com/ashkan18).
52
71
  * [#34](https://github.com/ashkan18/graphlient/issues/34): Fix: don't convert variables to `String` - [@dblock](https://github.com/dblock).
53
72
 
54
- ### 0.1.0 (10/27/2017)
73
+ ### 0.1.0 (2017/10/27)
55
74
 
56
75
  * [#31](https://github.com/ashkan18/graphlient/issues/31): Fix: catch execution errors that don't contain field names - [@dblock](https://github.com/dblock).
57
76
 
58
- ### 0.0.9 (10/26/2017)
77
+ ### 0.0.9 (2017/10/26)
59
78
 
60
79
  * [#28](https://github.com/ashkan18/graphlient/pull/28): Raise errors in `execute`, not only `query` - [@dblock](https://github.com/dblock).
61
80
  * [#29](https://github.com/ashkan18/graphlient/pull/29): Added `Graphlient::Adapters::HTTP::HTTPAdapter` that replaces Faraday with `Net::HTTP` - [@dblock](https://github.com/dblock).
62
81
 
63
- ### 0.0.8 (10/26/2017)
82
+ ### 0.0.8 (2017/10/26)
64
83
 
65
84
  * [#27](https://github.com/ashkan18/graphlient/pull/27): Always raise an exception unless a query has succeeded - [@dblock](https://github.com/dblock).
66
85
 
67
- ### 0.0.7 (10/24/2017)
86
+ ### 0.0.7 (2017/10/24)
68
87
 
69
88
  * [#26](https://github.com/ashkan18/graphlient/pull/26): Support String queries - [@dblock](https://github.com/dblock).
70
89
 
71
- ### 0.0.6 (10/20/2017)
90
+ ### 0.0.6 (2017/10/20)
72
91
 
73
92
  * [#14](https://github.com/ashkan18/graphlient/pull/14): Switch to `graphql-client` for network calls and schema validation - [@ashkan18](https://github.com/ashkan18).
74
93
  * [#17](https://github.com/ashkan18/graphlient/pull/17): Specialize server errors as `Graphlient::Errors::Server` - [@dblock](https://github.com/dblock).
@@ -79,16 +98,16 @@
79
98
  * [#20](https://github.com/ashkan18/graphlient/pull/20): Added support for parameterized queries and mutations - [@dblock](https://github.com/dblock).
80
99
  * [#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).
81
100
 
82
- ### 0.0.5 (10/5/2017)
101
+ ### 0.0.5 (2017/10/05)
83
102
 
84
103
  * [#11](https://github.com/ashkan18/graphlient/pull/11): Fixed query argument types - [@ashkan18](https://github.com/ashkan18).
85
104
 
86
- ### 0.0.4 (10/4/2017)
105
+ ### 0.0.4 (2017/10/04)
87
106
 
88
107
  * [#8](https://github.com/ashkan18/graphlient/pull/8): Handle HTTP errors and raise `Graphlient::Errors::HTTP` on failure - [@dblock](https://github.com/dblock).
89
108
  * [#5](https://github.com/ashkan18/graphlient/pull/5): Added RuboCop, Ruby-style linter, CHANGELOG, CONTRIBUTING and RELEASING - [@dblock](https://github.com/dblock).
90
109
  * [#4](https://github.com/ashkan18/graphlient/pull/4): Refactored Graphlient::Client to take a URL and options, moved extensions - [@dblock](https://github.com/dblock).
91
110
 
92
- ### 0.0.3 (10/3/2017)
111
+ ### 0.0.3 (2017/10/03)
93
112
 
94
113
  * Initial public release - [@ashkan18](https://github.com/ashkan18).
data/Dangerfile CHANGED
@@ -1 +1,24 @@
1
- changelog.check
1
+ # frozen_string_literal: true
2
+
3
+ # --------------------------------------------------------------------------------------------------------------------
4
+ # Has any changes happened inside the actual library code?
5
+ # --------------------------------------------------------------------------------------------------------------------
6
+ has_app_changes = !git.modified_files.grep(/lib/).empty?
7
+ has_spec_changes = !git.modified_files.grep(/spec/).empty?
8
+
9
+ # --------------------------------------------------------------------------------------------------------------------
10
+ # You've made changes to lib, but didn't write any tests?
11
+ # --------------------------------------------------------------------------------------------------------------------
12
+ warn("There're library changes, but not tests. That's OK as long as you're refactoring existing code.", sticky: false) if has_app_changes && !has_spec_changes
13
+
14
+ # --------------------------------------------------------------------------------------------------------------------
15
+ # You've made changes to specs, but no library code has changed?
16
+ # --------------------------------------------------------------------------------------------------------------------
17
+ if !has_app_changes && has_spec_changes
18
+ message('We really appreciate pull requests that demonstrate issues, even without a fix. That said, the next step is to try and fix the failing tests!', sticky: false)
19
+ end
20
+
21
+ # --------------------------------------------------------------------------------------------------------------------
22
+ # Have you updated CHANGELOG.md?
23
+ # --------------------------------------------------------------------------------------------------------------------
24
+ changelog.check!
data/Gemfile CHANGED
@@ -10,11 +10,11 @@ end
10
10
 
11
11
  group :development do
12
12
  gem 'byebug', platform: :ruby
13
- gem 'danger-changelog', '~> 0.2.1'
14
13
  gem 'rubocop', '0.56.0'
15
14
  end
16
15
 
17
16
  group :test do
17
+ gem 'faraday-rack', '~> 2.0'
18
18
  gem 'graphql', '~> 1.9'
19
19
  gem 'graphql-errors'
20
20
  gem 'rack-parser'
data/Gemfile.danger ADDED
@@ -0,0 +1,5 @@
1
+ source 'https://rubygems.org'
2
+
3
+ group :test do
4
+ gem 'danger-changelog', '~> 0.6.0'
5
+ end
data/README.md CHANGED
@@ -1,10 +1,26 @@
1
1
  # Graphlient
2
2
 
3
3
  [![Gem Version](https://badge.fury.io/rb/graphlient.svg)](https://badge.fury.io/rb/graphlient)
4
- [![Build Status](https://travis-ci.org/ashkan18/graphlient.svg?branch=master)](https://travis-ci.org/ashkan18/graphlient)
4
+ [![Build Status](https://github.com/ashkan18/graphlient/actions/workflows/ci.yml/badge.svg)](https://github.com/ashkan18/graphlient/actions/workflows/ci.yml)
5
5
 
6
6
  A friendlier Ruby client for consuming GraphQL-based APIs. Built on top of your usual [graphql-client](https://github.com/github/graphql-client), but with better defaults, more consistent error handling, and using the [faraday](https://github.com/lostisland/faraday) HTTP client.
7
7
 
8
+ # Table of Contents
9
+
10
+ - [Installation](#installation)
11
+ - [Usage](#usage)
12
+ - [Schema Storing and Loading on Disk](#schema-storing-and-loading-on-disk)
13
+ - [Preloading Schema Once](#preloading-schema-once)
14
+ - [Error Handling](#error-handling)
15
+ - [Executing Parameterized Queries and Mutations](#executing-parameterized-queries-and-mutations)
16
+ - [Parse and Execute Queries Separately](#parse-and-execute-queries-separately)
17
+ - [Dynamic vs. Static Queries](#dynamic-vs-static-queries)
18
+ - [Generate Queries with Graphlient::Query](#generate-queries-with-graphlientquery)
19
+ - [Create API Client Classes with Graphlient::Extension::Query](#create-api-client-classes-with-graphlientextensionquery)
20
+ - [Swapping the HTTP Stack](#swapping-the-http-stack)
21
+ - [Testing with Graphlient and RSpec](#testing-with-graphlient-and-rspec)
22
+ - [License](#license)
23
+
8
24
  ## Installation
9
25
 
10
26
  Add the following line to your Gemfile.
@@ -125,6 +141,30 @@ client = Client.new(url, schema_path: 'config/your_graphql_schema.json')
125
141
  client.schema.dump! # you only need to call this when graphql schema changes
126
142
  ```
127
143
 
144
+ ### Preloading Schema Once
145
+
146
+ Even if caching the schema on disk, instantiating `Graphlient::Client` often can be both time and memory intensive due to loading the schema for each instance. This is especially true if the schema is a large file. To get around these performance issues, instantiate your schema once and pass it in as a configuration option.
147
+
148
+ One time in an initializer
149
+
150
+ ```ruby
151
+ schema = Graphlient::Schema.new(
152
+ 'https://graphql.foo.com/graphql', 'lib/graphql_schema_foo.json'
153
+ )
154
+ ```
155
+
156
+ Pass in each time you initialize a client
157
+
158
+ ```
159
+ client = Graphlient::Client.new(
160
+ 'https://graphql.foo.com/graphql',
161
+ schema: schema,
162
+ headers: {
163
+ 'Authorization' => 'Bearer 123',
164
+ }
165
+ )
166
+ ```
167
+
128
168
  ### Error Handling
129
169
 
130
170
  Unlike graphql-client, Graphlient will always raise an exception unless the query has succeeded.
@@ -300,6 +340,62 @@ query.to_s
300
340
  # "\nquery {\n invoice(id: 10){\n line_items\n }\n }\n"
301
341
  ```
302
342
 
343
+ ### Use of Fragments
344
+
345
+ [Fragments](https://github.com/github/graphql-client#defining-queries) should be referred by constant:
346
+
347
+ ```ruby
348
+ module Fragments
349
+ Invoice = client.parse <<~'GRAPHQL'
350
+ fragment on Invoice {
351
+ id
352
+ feeInCents
353
+ }
354
+ GRAPHQL
355
+ end
356
+ ```
357
+
358
+ `Graphlient` offers the syntax below to refer to the original constant:
359
+ * Triple underscore `___` to refer to the fragment
360
+ * Double underscore `__` for namespace separator
361
+
362
+ In this example, `Fragments::Invoice` would be referred as follows:
363
+
364
+ ```ruby
365
+ invoice_query = client.parse do
366
+ query do
367
+ invoice(id: 10) do
368
+ id
369
+ ___Fragments__Invoice
370
+ end
371
+ end
372
+ end
373
+ ```
374
+
375
+ The wrapped response only allows access to fields that have been explicitly asked for.
376
+ In this example, while `id` has been referenced directly in the main query, `feeInCents` has been spread via fragment and trying to access it in the original wrapped response will throw [`GraphQL::Client::ImplicitlyFetchedFieldError`](https://github.com/github/graphql-client/blob/master/guides/implicitly-fetched-field-error.md) (to prevent data leaks between components).
377
+
378
+ ```ruby
379
+ response = client.execute(invoice_query)
380
+ result = response.data.invoice
381
+ result.to_h
382
+ # {"id" => 10, "feeInCents"=> 20000}
383
+ result.id
384
+ # 10
385
+ result.fee_in_cents
386
+ # raises GraphQL::Client::ImplicitlyFetchedFieldError
387
+ ```
388
+
389
+ `feeInCents` cannot be fetched directly from the main query, but from the fragment as shown below:
390
+
391
+ ```ruby
392
+ invoice = Fragments::Invoice.new(result)
393
+ invoice.id
394
+ # 10
395
+ invoice.fee_in_cents
396
+ # 20000
397
+ ```
398
+
303
399
  ### Create API Client Classes with Graphlient::Extension::Query
304
400
 
305
401
  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.
@@ -435,4 +531,3 @@ end
435
531
  ## License
436
532
 
437
533
  MIT License, see [LICENSE](LICENSE)
438
-
data/graphlient.gemspec CHANGED
@@ -14,7 +14,6 @@ Gem::Specification.new do |s|
14
14
  s.homepage = 'http://github.com/ashkan18/graphlient'
15
15
  s.licenses = ['MIT']
16
16
  s.summary = 'A friendlier Ruby client for consuming GraphQL-based APIs.'
17
- s.add_dependency 'faraday', '>= 1.0'
18
- s.add_dependency 'faraday_middleware'
17
+ s.add_dependency 'faraday', '~> 2.0'
19
18
  s.add_dependency 'graphql-client'
20
19
  end
@@ -1,5 +1,5 @@
1
1
  require 'faraday'
2
- require 'faraday_middleware'
2
+ require 'json'
3
3
 
4
4
  module Graphlient
5
5
  module Adapters
@@ -14,7 +14,8 @@ module Graphlient
14
14
  variables: variables
15
15
  }.to_json
16
16
  end
17
- response.body
17
+
18
+ parse_body(response.body)
18
19
  rescue Faraday::ConnectionFailed => e
19
20
  raise Graphlient::Errors::ConnectionFailedError, e
20
21
  rescue Faraday::TimeoutError => e
@@ -40,6 +41,38 @@ module Graphlient
40
41
  end
41
42
  end
42
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
43
76
  end
44
77
  end
45
78
  end
@@ -2,9 +2,12 @@ module Graphlient
2
2
  class Client
3
3
  attr_accessor :uri, :options
4
4
 
5
+ class InvalidConfigurationError < StandardError; end
6
+
5
7
  def initialize(url, options = {}, &_block)
6
8
  @url = url
7
9
  @options = options.dup
10
+ raise_error_if_invalid_configuration!
8
11
  yield self if block_given?
9
12
  end
10
13
 
@@ -51,11 +54,15 @@ module Graphlient
51
54
  end
52
55
 
53
56
  def schema
54
- @schema ||= Graphlient::Schema.new(http, schema_path)
57
+ @schema ||= options[:schema] || Graphlient::Schema.new(http, schema_path)
55
58
  end
56
59
 
57
60
  private
58
61
 
62
+ def raise_error_if_invalid_configuration!
63
+ raise InvalidConfigurationError, 'schema_path and schema cannot both be provided' if options.key?(:schema_path) && options.key?(:schema)
64
+ end
65
+
59
66
  def schema_path
60
67
  return options[:schema_path].to_s if options[:schema_path]
61
68
  end
@@ -67,7 +74,7 @@ module Graphlient
67
74
  end
68
75
 
69
76
  def errors_in_result?(response)
70
- response.data && response.data.errors && response.data.errors.any?
77
+ response.data && response.data.errors && response.data.errors.all.any?
71
78
  end
72
79
  end
73
80
  end
@@ -9,13 +9,15 @@ module Graphlient
9
9
 
10
10
  ROOT_NODES = %w[query mutation subscription].freeze
11
11
 
12
+ FRAGMENT_DEFITION = /___(?<const>[A-Z][a-zA-Z0-9_]*(__[A-Z][a-zA-Z0-9_]*)*)/
13
+
12
14
  attr_accessor :query_str
13
15
 
14
16
  def initialize(&block)
15
17
  @indents = 0
16
18
  @query_str = ''
17
19
  @variables = []
18
- instance_eval(&block)
20
+ evaluate(&block)
19
21
  end
20
22
 
21
23
  def method_missing(method_name, *args, &block)
@@ -39,7 +41,24 @@ module Graphlient
39
41
 
40
42
  private
41
43
 
44
+ def evaluate(&block)
45
+ @last_block = block || self
46
+ (@context ||= {})[@last_block] ||= @last_block.binding
47
+ instance_eval(&block)
48
+ end
49
+
50
+ def resolve_fragment_constant(value)
51
+ return nil unless (match = value.to_s.match(FRAGMENT_DEFITION))
52
+ raw_const = match[:const].gsub('__', '::')
53
+ @context[@last_block].eval(raw_const).tap do |const|
54
+ msg = "Expected constant #{raw_const} to be GraphQL::Client::FragmentDefinition. Given #{const.class}"
55
+ raise Graphlient::Errors::Error, msg unless const.is_a? GraphQL::Client::FragmentDefinition
56
+ end
57
+ end
58
+
42
59
  def append_node(node, args, arg_processor: nil, &block)
60
+ node = "...#{resolve_fragment_constant(node)}".to_sym if node.to_s.start_with?('___')
61
+
43
62
  # add field
44
63
  @query_str << "\n#{indent}#{node}"
45
64
  # add filter
@@ -49,7 +68,7 @@ module Graphlient
49
68
  if block_given?
50
69
  @indents += 1
51
70
  @query_str << '{'
52
- instance_eval(&block)
71
+ evaluate(&block)
53
72
  @query_str << '}'
54
73
  @indents -= 1
55
74
  end
@@ -1,3 +1,3 @@
1
1
  module Graphlient
2
- VERSION = '0.5.0'.freeze
2
+ VERSION = '0.8.0'.freeze
3
3
  end
@@ -19,8 +19,8 @@ describe Graphlient::Adapters::HTTP::FaradayAdapter do
19
19
  expect(client.http.connection.builder.handlers).to eq(
20
20
  [
21
21
  Faraday::Response::RaiseError,
22
- FaradayMiddleware::EncodeJson,
23
- FaradayMiddleware::ParseJson
22
+ Faraday::Request::Json,
23
+ Faraday::Response::Json
24
24
  ]
25
25
  )
26
26
  end
@@ -65,7 +65,8 @@ describe Graphlient::Adapters::HTTP::FaradayAdapter do
65
65
  before do
66
66
  stub_request(:post, url).to_return(
67
67
  status: 200,
68
- body: DummySchema.execute(GraphQL::Introspection::INTROSPECTION_QUERY).to_json
68
+ body: DummySchema.execute(GraphQL::Introspection::INTROSPECTION_QUERY).to_json,
69
+ headers: { 'Content-Type' => 'application/json' }
69
70
  )
70
71
  end
71
72
 
@@ -74,6 +75,61 @@ describe Graphlient::Adapters::HTTP::FaradayAdapter do
74
75
  end
75
76
  end
76
77
 
78
+ context 'a non-error response without an appropriate JSON header' do
79
+ let(:url) { 'http://example.com/graphql' }
80
+ let(:headers) { { 'Content-Type' => 'application/notjson' } }
81
+ let(:client) { Graphlient::Client.new(url) }
82
+ let(:response_headers) { { 'Content-Type' => 'application/notjson' } }
83
+
84
+ context 'when the response body is valid JSON' do
85
+ before do
86
+ stub_request(:post, url).to_return(
87
+ status: 200,
88
+ headers: response_headers,
89
+ body: DummySchema.execute(GraphQL::Introspection::INTROSPECTION_QUERY).to_json
90
+ )
91
+ end
92
+
93
+ it 'retrieves schema' do
94
+ expect(client.schema).to be_a Graphlient::Schema
95
+ end
96
+ end
97
+
98
+ context 'when the response body is not valid JSON' do
99
+ before do
100
+ stub_request(:post, url).to_return(
101
+ status: 200,
102
+ headers: response_headers,
103
+ body: ''
104
+ )
105
+ end
106
+
107
+ it 'raises Graphlient::Errors::ServerError' do
108
+ expect { client.schema }.to raise_error(Graphlient::Errors::ServerError) { |error|
109
+ expect(error.message).to include('Failed to parse response body as JSON')
110
+ }
111
+ end
112
+ end
113
+
114
+ context 'when the Faraday response body object is not a type we expect from Faraday' do
115
+ before do
116
+ stub_request(:post, url).to_return(
117
+ status: 200,
118
+ headers: response_headers
119
+ )
120
+ end
121
+
122
+ it 'raises Graphlient::Errors::ClientError' do
123
+ mock_response = double('response', body: nil)
124
+ allow(client.http.connection).to receive(:post).and_return(mock_response)
125
+
126
+ expect { client.schema }.to raise_error(Graphlient::Errors::ClientError) { |error|
127
+ expect(error.message).to include "Unexpected response body type 'NilClass'"
128
+ }
129
+ end
130
+ end
131
+ end
132
+
77
133
  context 'Failed to open TCP connection error' do
78
134
  let(:url) { 'http://example.com/graphql' }
79
135
  let(:client) { Graphlient::Client.new(url) }
@@ -90,7 +146,6 @@ describe Graphlient::Adapters::HTTP::FaradayAdapter do
90
146
 
91
147
  specify do
92
148
  expected_error_message = "Connection refused - #{error_message}"
93
-
94
149
  expect { client.schema }.to raise_error(Graphlient::Errors::ConnectionFailedError, expected_error_message)
95
150
  end
96
151
  end
@@ -25,6 +25,43 @@ describe Graphlient::Client do
25
25
  invoice = response.data.invoice
26
26
  expect(invoice.id).to eq '10'
27
27
  end
28
+
29
+ context 'with fragment' do
30
+ let(:invoice_fragment) do
31
+ client.parse <<~'GRAPHQL'
32
+ fragment on Invoice {
33
+ id
34
+ feeInCents
35
+ }
36
+ GRAPHQL
37
+ end
38
+
39
+ let(:invoice_fragment_const) do
40
+ stub_const('Graphlient::InvoiceFragment', invoice_fragment)
41
+ end
42
+
43
+ let(:query) do
44
+ invoice_fragment_const
45
+ client.parse do
46
+ query do
47
+ invoice(id: 10) do
48
+ ___Graphlient__InvoiceFragment
49
+ end
50
+ end
51
+ end
52
+ end
53
+
54
+ it '#parse' do
55
+ expect(query).to be_a GraphQL::Client::OperationDefinition
56
+ end
57
+
58
+ it '#execute' do
59
+ response = client.execute(query)
60
+ invoice = response.data.invoice
61
+ fragment = invoice_fragment.new(invoice)
62
+ expect(fragment.id).to eq '10'
63
+ end
64
+ end
28
65
  end
29
66
 
30
67
  context 'parameterized query' do
@@ -80,6 +117,29 @@ describe Graphlient::Client do
80
117
  GRAPHQL
81
118
  end
82
119
 
120
+ let(:execution_error_query) do
121
+ <<-GRAPHQL
122
+ query($id: Int) {
123
+ executionErrorInvoice(id: $id) {
124
+ id
125
+ feeInCents
126
+ }
127
+ }
128
+ GRAPHQL
129
+ end
130
+
131
+ let(:partial_success_query) do
132
+ <<-GRAPHQL
133
+ query {
134
+ someInvoices {
135
+ id
136
+ feeInCents
137
+ createdAt
138
+ }
139
+ }
140
+ GRAPHQL
141
+ end
142
+
83
143
  it '#execute' do
84
144
  response = client.execute(query, id: 42)
85
145
  invoice = response.data.invoice
@@ -97,10 +157,9 @@ describe Graphlient::Client do
97
157
 
98
158
  it 'fails on an execution error' do
99
159
  expect do
100
- allow(OpenStruct).to receive(:new).and_raise StandardError, 'Unexpected error.'
101
- client.execute(query, id: 42)
160
+ client.execute(execution_error_query, id: 42)
102
161
  end.to raise_error Graphlient::Errors::ExecutionError do |e|
103
- expect(e.to_s).to eq 'invoice: Unexpected error.'
162
+ expect(e.to_s).to eq 'executionErrorInvoice: Execution Error'
104
163
  end
105
164
  end
106
165
 
@@ -119,6 +178,14 @@ describe Graphlient::Client do
119
178
  expect(e.response).to be_a GraphQL::Client::Response
120
179
  end
121
180
  end
181
+
182
+ it 'fails with a partial error response' do
183
+ expect do
184
+ client.execute(partial_success_query)
185
+ end.to raise_error Graphlient::Errors::ExecutionError do |e|
186
+ expect(e.response).to be_a GraphQL::Client::Response
187
+ end
188
+ end
122
189
  end
123
190
 
124
191
  context 'non-parameterized query' do
@@ -7,13 +7,23 @@ describe Graphlient::Client do
7
7
 
8
8
  describe '#schema' do
9
9
  before do
10
- stub_request(:post, url)
11
- .to_return(body: DummySchema.execute(GraphQL::Introspection::INTROSPECTION_QUERY).to_json)
10
+ stub_request(:post, url).to_return(
11
+ body: DummySchema.execute(GraphQL::Introspection::INTROSPECTION_QUERY).to_json,
12
+ headers: { 'Content-Type' => 'application/json' }
13
+ )
12
14
  end
13
15
 
14
16
  context 'when server returns error' do
15
17
  before do
16
- stub_request(:post, url).to_return(status: 500, body: { errors: [{ message: 'test message', extensions: { code: 'SOMETHING', timestamp: Time.now } }] }.to_json)
18
+ stub_request(:post, url).to_return(
19
+ status: 500,
20
+ body: {
21
+ errors: [
22
+ { message: 'test message', extensions: { code: 'SOMETHING', timestamp: Time.now } }
23
+ ]
24
+ }.to_json,
25
+ headers: { 'Content-Type' => 'application/json' }
26
+ )
17
27
  end
18
28
 
19
29
  it 'fails with an exception' do
@@ -41,5 +51,25 @@ describe Graphlient::Client do
41
51
  expect(client.schema.path).to eq 'config/schema.json'
42
52
  end
43
53
  end
54
+
55
+ context 'when preloaded schema is provided' do
56
+ let(:schema) { Graphlient::Schema.new(url, 'spec/support/fixtures/invoice_api.json') }
57
+ let(:client) { described_class.new(url, schema: schema) }
58
+
59
+ it 'returns the passed in schema' do
60
+ expect(client.schema).not_to be_nil
61
+ expect(client.schema).to eq(schema)
62
+ end
63
+ end
64
+
65
+ context 'when and a schema and a schema path are provided' do
66
+ let(:schema) { Graphlient::Schema.new(url, 'spec/support/fixtures/invoice_api.json') }
67
+ let(:client) { described_class.new(url, schema: schema, schema_path: 'spec/support/fixtures/invoice_api.json') }
68
+
69
+ it 'raises an invalid configuration error' do
70
+ expect { client }.to raise_error(Graphlient::Client::InvalidConfigurationError,
71
+ /schema_path and schema cannot both be provided/)
72
+ end
73
+ end
44
74
  end
45
75
  end
@@ -10,7 +10,10 @@ describe Graphlient::Schema do
10
10
  let!(:introspection_query_request) do
11
11
  stub_request(:post, url)
12
12
  .with(body: /query IntrospectionQuery/)
13
- .to_return(body: DummySchema.execute(GraphQL::Introspection::INTROSPECTION_QUERY).to_json)
13
+ .to_return(
14
+ body: DummySchema.execute(GraphQL::Introspection::INTROSPECTION_QUERY).to_json,
15
+ headers: { 'Content-Type' => 'application/json' }
16
+ )
14
17
  end
15
18
 
16
19
  context 'when schema path is not given' do
@@ -27,7 +27,8 @@ describe 'App' do
27
27
  before do
28
28
  stub_request(:post, url).to_return(
29
29
  status: 200,
30
- body: json_response
30
+ body: json_response,
31
+ headers: { 'Content-Type' => 'application/json' }
31
32
  )
32
33
  end
33
34
 
data/spec/spec_helper.rb CHANGED
@@ -7,6 +7,7 @@ require 'byebug' if RUBY_ENGINE != 'jruby'
7
7
  require 'rack/test'
8
8
  require 'webmock/rspec'
9
9
  require 'vcr'
10
+ require 'faraday/rack'
10
11
 
11
12
  Dir[File.join(File.dirname(__FILE__), 'support', '**/*.rb')].each do |file|
12
13
  require file
@@ -1,3 +1,4 @@
1
+ require_relative '../types/invoice_type'
1
2
  class CreateInvoice < GraphQL::Schema::RelayClassicMutation
2
3
  null true
3
4
 
@@ -10,6 +10,15 @@ class Query < GraphQL::Schema::Object
10
10
  argument :id, Integer, required: false
11
11
  end
12
12
 
13
+ field :execution_error_invoice, InvoiceType, null: false, extras: [:execution_errors] do
14
+ description 'Find invoice'
15
+ argument :id, Integer, required: false
16
+ end
17
+
18
+ field :some_invoices, [InvoiceType], null: true do
19
+ description 'List of invoices'
20
+ end
21
+
13
22
  def invoice(id: nil)
14
23
  return nil if id.nil?
15
24
  OpenStruct.new(
@@ -21,4 +30,18 @@ class Query < GraphQL::Schema::Object
21
30
  def not_null_invoice(*)
22
31
  nil
23
32
  end
33
+
34
+ def execution_error_invoice(id: nil, execution_errors:)
35
+ execution_errors.add(GraphQL::ExecutionError.new('Execution Error'))
36
+
37
+ invoice(id: id)
38
+ end
39
+
40
+ def some_invoices
41
+ [
42
+ OpenStruct.new(id: 0, fee_in_cents: 20_000),
43
+ OpenStruct.new(id: 1, fee_in_cents: 20_000),
44
+ OpenStruct.new(id: 2, fee_in_cents: 20_000)
45
+ ]
46
+ end
24
47
  end
@@ -4,4 +4,10 @@ class InvoiceType < GraphQL::Schema::Object
4
4
 
5
5
  field :id, ID, null: false
6
6
  field :fee_in_cents, Integer, null: true
7
+ field :created_at, String, null: true, extras: [:execution_errors]
8
+
9
+ def created_at(execution_errors:)
10
+ execution_errors.add(GraphQL::ExecutionError.new('This is a partial error'))
11
+ Time.now.iso8601
12
+ end
7
13
  end
metadata CHANGED
@@ -1,43 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: graphlient
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0
4
+ version: 0.8.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ashkan Nasseri
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-12-28 00:00:00.000000000 Z
11
+ date: 2024-01-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - ">="
17
+ - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '1.0'
19
+ version: '2.0'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - ">="
25
- - !ruby/object:Gem::Version
26
- version: '1.0'
27
- - !ruby/object:Gem::Dependency
28
- name: faraday_middleware
29
- requirement: !ruby/object:Gem::Requirement
30
- requirements:
31
- - - ">="
24
+ - - "~>"
32
25
  - !ruby/object:Gem::Version
33
- version: '0'
34
- type: :runtime
35
- prerelease: false
36
- version_requirements: !ruby/object:Gem::Requirement
37
- requirements:
38
- - - ">="
39
- - !ruby/object:Gem::Version
40
- version: '0'
26
+ version: '2.0'
41
27
  - !ruby/object:Gem::Dependency
42
28
  name: graphql-client
43
29
  requirement: !ruby/object:Gem::Requirement
@@ -58,15 +44,20 @@ executables: []
58
44
  extensions: []
59
45
  extra_rdoc_files: []
60
46
  files:
47
+ - ".github/ISSUE_TEMPLATE/bug_report.md"
48
+ - ".github/ISSUE_TEMPLATE/feature_request.md"
49
+ - ".github/workflows/ci.yml"
50
+ - ".github/workflows/danger.yml"
51
+ - ".github/workflows/rubocop.yml"
61
52
  - ".gitignore"
62
53
  - ".rspec"
63
54
  - ".rubocop.yml"
64
55
  - ".rubocop_todo.yml"
65
- - ".travis.yml"
66
56
  - CHANGELOG.md
67
57
  - CONTRIBUTING.md
68
58
  - Dangerfile
69
59
  - Gemfile
60
+ - Gemfile.danger
70
61
  - LICENSE
71
62
  - README.md
72
63
  - RELEASING.md
@@ -140,7 +131,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
140
131
  - !ruby/object:Gem::Version
141
132
  version: 1.3.6
142
133
  requirements: []
143
- rubygems_version: 3.0.8
134
+ rubygems_version: 3.3.7
144
135
  signing_key:
145
136
  specification_version: 4
146
137
  summary: A friendlier Ruby client for consuming GraphQL-based APIs.
data/.travis.yml DELETED
@@ -1,24 +0,0 @@
1
- language: ruby
2
-
3
- cache: bundler
4
-
5
- rvm:
6
- - 2.5.0
7
- - 2.7.2
8
- - 3.0.0
9
- - ruby-head
10
- - jruby-9.1.16.0
11
- - jruby-head
12
-
13
- before_install:
14
- - gem update --system
15
-
16
- matrix:
17
- include:
18
- - rvm: 2.7.2
19
- script:
20
- - bundle exec danger
21
- allow_failures:
22
- - rvm: ruby-head
23
- - rvm: jruby-9.1.16.0
24
- - rvm: jruby-head