graphql_connector 0.2.0 → 1.2.1

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: 8ce0306b4b797a61fd2e4e26036731e5b702c7a3b3772f68f90f8db2f124f1ef
4
- data.tar.gz: c7afc2f11f5a6c91bc3f788b08fda857de8451cba6905f179691f002ce6ba643
3
+ metadata.gz: b5e948edafea5975fe63377a5da8f4ca6d01d9876393a811da385b3d3f07fa88
4
+ data.tar.gz: 8aa20c39ae60c975e8e17fb1b54e8715a39d2be4f536d1e6df053a93208096b3
5
5
  SHA512:
6
- metadata.gz: 82de4e8e154f407995fb1fbe865ee59610d9b283f876e54af68bce38d20a2bc80fe00a08b2b104a6c532c551b6e242fb3b4792efdc5f27122fc4ad597b9edf76
7
- data.tar.gz: da700041749e3f7d1ab40f13258c073556d6fc7f316224f3aa3c91368dce06895d9a3b9b4ac6098c896268b147a1cc370d19fe8072683b6255f028ca1c4a6aa2
6
+ metadata.gz: a3a49cf0f8be6bbb061d29e34a5224c76f287c84a9fc2a9af51ba3c4475a043cb0321870ecd59ee027be111335a961b9ec55f609a5721429cabfb5897af5d4ed
7
+ data.tar.gz: f051786162a0f05136acc59d1682012c54c1d2d306f3e9284a8136b70be80410b101731c2c608d71d1f600101df0f0427917c0777c9ae725c1ebb8e1bee4bf80
@@ -0,0 +1,37 @@
1
+ name: CI
2
+
3
+ on: [push]
4
+
5
+ jobs:
6
+ build:
7
+ runs-on: ubuntu-latest
8
+ strategy:
9
+ matrix:
10
+ ruby: ['2.4.10', '2.5.9', '2.6.7', '2.7.3', '3.0.1']
11
+ name: "ruby ${{ matrix.ruby }}"
12
+ steps:
13
+ - uses: actions/checkout@v1
14
+ - uses: ruby/setup-ruby@v1
15
+ with:
16
+ ruby-version: ${{ matrix.ruby }}
17
+ - name: Install dependencies
18
+ run: |
19
+ gem install bundler
20
+ bundle install
21
+ - name: Run Test with rspec
22
+ run: bundle exec rspec spec
23
+ rubocop:
24
+ name: Rubocop
25
+ runs-on: ubuntu-latest
26
+ steps:
27
+ - uses: actions/checkout@v2
28
+ - uses: ruby/setup-ruby@v1
29
+ with:
30
+ ruby-version: 2.7.3
31
+ - name: Install dependencies
32
+ run: |
33
+ gem install bundler
34
+ bundle install
35
+ - name: Build and test
36
+ run: |
37
+ bundle exec rubocop
data/.rspec CHANGED
@@ -1 +1,2 @@
1
1
  --require spec_helper
2
+ --order rand
data/.rubocop_todo.yml CHANGED
@@ -1,3 +1,6 @@
1
1
  Lint/DuplicateMethods:
2
2
  Exclude:
3
3
  - lib/graphql_connector.rb
4
+ Metrics/BlockLength:
5
+ Exclude:
6
+ - spec/graphql_connector/**/*.rb
data/CHANGELOG.md CHANGED
@@ -1,18 +1,47 @@
1
+ ## 1.2.1 (2021-05-25)
1
2
 
2
- ### 0.2.0
3
+ * relax httparty dependency(`~> 0.17` => `~> 0.16`)
3
4
 
4
- ** Features
5
+ ## 1.2.0 (2020-12-22)
6
+
7
+ ### Features
8
+ * Add `mutation` under server namespace and service class inclusion
9
+ * See `README` for details
10
+
11
+ ## 1.1.1 (2020-5-04)
12
+
13
+ ### BugFix
14
+ * Omit invalid `()` for empty conditions in `query` method
15
+
16
+ ## 1.1.0 (2020-4-19)
17
+
18
+ ### Features
19
+ * Allow building graphql querying in custom classes via `service class inclusion`
20
+ * See `README` for details about `service class inclusion`
21
+
22
+ ### BugFix
23
+ * Forward `variables` when performing a `raw_query`
24
+
25
+ ## 1.0.0 (2020-1-26)
26
+
27
+ ### Breaking
28
+ * add multiple graphql server querying
29
+ * `query` and `raw_query` nested under server namespace
30
+
31
+
32
+ ## 0.2.0 (2019-11-19)
33
+
34
+ ### Features
5
35
  * new `raw_query` method. you have to write the graphql query
6
- string by your self and also you get only the parsed json back.
36
+ string by your self and also you get only the parsed json back.
7
37
  * query supports associations for the selected_fields attribute
8
38
 
39
+ ## 0.1.0.beta1 (2019-10-06)
9
40
 
10
- ### 0.1.0.beta1
11
-
12
- ** BugFix
13
- use model instead of hardcoded product
41
+ ### BugFix
42
+ * use model instead of hardcoded product
14
43
 
15
44
 
16
- ### 0.1.0.beta
45
+ ## 0.1.0.beta (2019-10-03)
17
46
 
18
- easy graphql logic
47
+ * easy graphql logic
data/Gemfile.lock CHANGED
@@ -1,8 +1,8 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- graphql_connector (0.2.0)
5
- httparty (~> 0.17)
4
+ graphql_connector (1.2.0)
5
+ httparty (~> 0.16)
6
6
 
7
7
  GEM
8
8
  remote: https://rubygems.org/
@@ -10,45 +10,46 @@ GEM
10
10
  ast (2.4.0)
11
11
  coderay (1.1.2)
12
12
  diff-lcs (1.3)
13
- httparty (0.17.1)
13
+ httparty (0.18.1)
14
14
  mime-types (~> 3.0)
15
15
  multi_xml (>= 0.5.2)
16
- jaro_winkler (1.5.3)
17
- method_source (0.9.2)
18
- mime-types (3.3)
16
+ jaro_winkler (1.5.4)
17
+ method_source (1.0.0)
18
+ mime-types (3.3.1)
19
19
  mime-types-data (~> 3.2015)
20
- mime-types-data (3.2019.1009)
20
+ mime-types-data (3.2021.0225)
21
21
  multi_xml (0.6.0)
22
- parallel (1.17.0)
23
- parser (2.6.5.0)
22
+ parallel (1.19.1)
23
+ parser (2.7.1.1)
24
24
  ast (~> 2.4.0)
25
- pry (0.12.2)
26
- coderay (~> 1.1.0)
27
- method_source (~> 0.9.0)
25
+ pry (0.13.1)
26
+ coderay (~> 1.1)
27
+ method_source (~> 1.0)
28
28
  rainbow (3.0.0)
29
- rake (10.5.0)
30
- rspec (3.8.0)
31
- rspec-core (~> 3.8.0)
32
- rspec-expectations (~> 3.8.0)
33
- rspec-mocks (~> 3.8.0)
34
- rspec-core (3.8.2)
35
- rspec-support (~> 3.8.0)
36
- rspec-expectations (3.8.5)
29
+ rexml (3.2.5)
30
+ rspec (3.9.0)
31
+ rspec-core (~> 3.9.0)
32
+ rspec-expectations (~> 3.9.0)
33
+ rspec-mocks (~> 3.9.0)
34
+ rspec-core (3.9.1)
35
+ rspec-support (~> 3.9.1)
36
+ rspec-expectations (3.9.1)
37
37
  diff-lcs (>= 1.2.0, < 2.0)
38
- rspec-support (~> 3.8.0)
39
- rspec-mocks (3.8.2)
38
+ rspec-support (~> 3.9.0)
39
+ rspec-mocks (3.9.1)
40
40
  diff-lcs (>= 1.2.0, < 2.0)
41
- rspec-support (~> 3.8.0)
42
- rspec-support (3.8.3)
43
- rubocop (0.75.0)
41
+ rspec-support (~> 3.9.0)
42
+ rspec-support (3.9.2)
43
+ rubocop (0.82.0)
44
44
  jaro_winkler (~> 1.5.1)
45
45
  parallel (~> 1.10)
46
- parser (>= 2.6)
46
+ parser (>= 2.7.0.1)
47
47
  rainbow (>= 2.2.2, < 4.0)
48
+ rexml
48
49
  ruby-progressbar (~> 1.7)
49
- unicode-display_width (>= 1.4.0, < 1.7)
50
+ unicode-display_width (>= 1.4.0, < 2.0)
50
51
  ruby-progressbar (1.10.1)
51
- unicode-display_width (1.6.0)
52
+ unicode-display_width (1.7.0)
52
53
 
53
54
  PLATFORMS
54
55
  ruby
@@ -57,9 +58,8 @@ DEPENDENCIES
57
58
  bundler (~> 2.0)
58
59
  graphql_connector!
59
60
  pry (~> 0.10)
60
- rake (~> 10.0)
61
61
  rspec (~> 3.8)
62
62
  rubocop (~> 0.75)
63
63
 
64
64
  BUNDLED WITH
65
- 2.0.2
65
+ 2.1.4
data/README.md CHANGED
@@ -1,12 +1,13 @@
1
+
1
2
  # GraphqlConnector
2
3
 
3
4
  [![Gem
4
5
  Version](https://badge.fury.io/rb/graphql_connector.svg)](https://badge.fury.io/rb/graphql_connector)
5
- [![Build
6
- Status](https://travis-ci.org/Garllon/graphql_connector.svg?branch=master)](https://travis-ci.org/Garllon/graphql_connector)
6
+ [![CI](https://github.com/Garllon/graphql_connector/workflows/CI/badge.svg)](https://github.com/Garllon/graphql_connector/actions?query=workflow%3ACI)
7
+ [![Maintainability](https://api.codeclimate.com/v1/badges/548db3cf0d078b379c84/maintainability)](https://codeclimate.com/github/Garllon/graphql_connector/maintainability)
7
8
 
8
9
  An easy connector to call your `graphql` server. Currently there is no schema
9
- check in the code, but i will add this.
10
+ check in the code, but i will add it.
10
11
 
11
12
  ## Installation
12
13
 
@@ -29,23 +30,39 @@ Or install it yourself as:
29
30
  You need to configure the `graphql_connector` first:
30
31
  ``` ruby
31
32
  GraphqlConnector.configure do |config|
32
- config.host = ''
33
- config.headers = {}
33
+ config.add_server(name: 'Foo', uri: 'http://foo.com/api/graphql', headers: {})
34
34
  end
35
35
  ```
36
36
 
37
+ For each graphql server you wish to query use `add_server`.
38
+
39
+ Afterwards you will have the following options to fetch and/or mutate data with
40
+ one or many graphql servers:
41
+
42
+ * `raw_query` --> [Examples](examples/raw_query_examples.rb)
43
+ * `query` --> [Examples](examples/query_examples.rb)
44
+ * `mutation` --> [Examples](examples/mutation_examples.rb)
45
+ * `service class inclusion` --> [Examples](examples/departments_service_class_examples.rb)
46
+
47
+ See the following sub sections for details
48
+
37
49
  ### raw_query
38
50
 
39
- Then you can call your graphql_endpoint:
51
+ You can call your graphql_endpoint via:
40
52
  ```ruby
41
- GraphqlConnector.query(model, condition, selected_fields)
53
+ GraphqlConnector::<name>.raw_query(query_string)
42
54
  ```
43
55
 
56
+ Note that `<name>` has to be replaced by any of the ones added via `add_server`
57
+
58
+ See also [here](examples/raw_query_examples.rb) for example usage
59
+
60
+ ---
44
61
  ### query
45
62
 
46
- Or your are using the more comfortable `query`:
63
+ You can also use the more comfortable `query`:
47
64
  ```ruby
48
- GraphqlConnector.query(model, condition, selected_fields)
65
+ GraphqlConnector::<name>.query(model, condition, selected_fields)
49
66
  ```
50
67
 
51
68
  | Variable | DataType | Example |
@@ -57,11 +74,13 @@ GraphqlConnector.query(model, condition, selected_fields)
57
74
  > Caution:
58
75
  > You get an OpenStruct back. Currently only the first level attributes are
59
76
  > supported with OpenStruct, associated objects are still a normal array of
60
- > hashes.
77
+ > hashes.
78
+
79
+ See also [here](examples/query_examples.rb) for example usage
61
80
 
62
81
  #### selected_fields
63
82
 
64
- The synatx for the associations looks like the following:
83
+ The syntax for the associations looks like the following:
65
84
  ```
66
85
  ['<attribute_name>', <association_name>: ['<attribute_name_of_the_association>']]
67
86
  ```
@@ -71,6 +90,129 @@ Example:
71
90
  ['id', 'name', productCategory: ['id', 'name']]
72
91
  ```
73
92
 
93
+ ---
94
+
95
+ ### mutation
96
+
97
+ Works in the same way as [query](#query)
98
+
99
+ See also [here](examples/mutation_examples.rb) for example usage
100
+
101
+ ### Service class inclusion
102
+
103
+ This approach can be used to `graphqlize` **any** kind of ruby (service) class
104
+ so that it has re-usable graphql `query` and `mutation` **class methods**.
105
+
106
+ * First add `extend GraphqlConnector::<server>::Query` in the the class(es) that should be `graphqlized`
107
+
108
+ * Then you can aliases as many graphql server types via `add_query` and/or `add_raw_query` and/or `add_mutation`:
109
+
110
+ ```ruby
111
+ add_query <alias>: :<graphql_server_type>, params: [...], returns: [...]
112
+
113
+ add_raw_query <alias>: 'query { ... }', params: [...]
114
+
115
+ add_mutation <alias>: :<graphql_server_type>, params: [...], returns: [...]
116
+ ```
117
+ * :grey_exclamation: If not needed omit `params`
118
+
119
+ See also [here](examples/departments_service_class_examples.rb) and also here for complete example usage:
120
+
121
+ ```ruby
122
+ GraphqlConnector.configure do |config|
123
+ config.add_server(name: 'Foo', uri: 'http://foo.com/api/graphql', headers: {})
124
+ end
125
+
126
+ # product.rb
127
+ class Product
128
+ extend GraphqlConnector::Foo::Query
129
+
130
+ add_query all: :products,
131
+ returns: [:id, :name]
132
+
133
+ add_query by_id: :products,
134
+ params: :id,
135
+ returns: [:name, product_category: [:id, :name]]
136
+
137
+ add_query by_names: :products,
138
+ params: :names,
139
+ returns: [:id, :name, product_category: [:id, :name]]
140
+
141
+ add_query by: :products,
142
+ params: [:id, :name],
143
+ returns: [:name]
144
+
145
+ add_query by_category_id: :products,
146
+ params: :product_category,
147
+ returns: [product_category: [:id, :name]]
148
+
149
+ add_mutation create: :createProduct,
150
+ params: [:name, :catgetoryId],
151
+ returns: [:id, :name]
152
+ end
153
+
154
+ Product.all
155
+ => [OpenStruct<id=1, name='Demo Product', ...]
156
+
157
+ Product.by_id(id: 1)
158
+ => [OpenStruct<name='Demo Product', product_category=<ProductCategory<id=10, name='Demo Category'>>]
159
+
160
+ Product.by_names(names: ['Demo Product', 'Non Demo Product'])
161
+ => [OpenStruct<id=1, name='Demo Product', product_category=<ProductCategory<id=10, name='Demo Category'>>, Product<id=2, name='Demo Product' ...]
162
+
163
+ Product.by(id: 1, name: 'Demo Product')
164
+ => OpenStruct<name='Demo Product'>
165
+
166
+ Product.by_category_id(product_category: { id: 10})
167
+ => OpenStruct<product_category=<ProductCategory<id=10, name='Demo Category'>>
168
+
169
+ Product.create(name: 'Another Product', catgetoryId: 10)
170
+ => OpenStruct<id=10, name='Another Product'>
171
+ ```
172
+
173
+ Also custom **class methods** can used to call any kind of `query` and do further selection instead:
174
+
175
+ ```ruby
176
+ class Product
177
+ extend GraphqlConnector::Foo::Query
178
+
179
+ add_query all: :products, returns: [:name]
180
+
181
+ def self.by_id(id:)
182
+ all.select { |products| products.id == id }.first
183
+ end
184
+ end
185
+
186
+ Product.by_id(id: 1)
187
+ => OpenStruct<id=1, name='Demo Product'>>
188
+ ```
189
+
190
+ :warning: Ensure that your custom **class method** never has the **same name** as an `<alias>` of `add_query`, `add_raw_query` or `add_mutation`. Otherwise the associated grapqhl query will not be performed because of [Ruby Open Class principle](https://ruby-lang.co/ruby-open-class/)
191
+
192
+
193
+ Example for `raw_query`:
194
+
195
+ ```ruby
196
+ class Product
197
+ extend GraphqlConnector::Foo::Query
198
+
199
+ add_raw_query all: ' query { products { id name } } '
200
+ add_raw_query by: ' query products($id: !ID, $name: !String) '\
201
+ '{ products(id: $id, name: $name) { id name } }',
202
+ params: [:id, :name]
203
+
204
+ end
205
+
206
+ Product.all
207
+ => [ { id: '1', name: 'Demo Product' }, ...]
208
+
209
+ Product.by(id: '1', name: 'Demo Product')
210
+ => { id: '1', name: 'Demo Product' }
211
+
212
+ ```
213
+
214
+ :exclamation: There is no `add_raw_mutation` since `add_raw_query` does already cover such a case
215
+
74
216
  ## Development
75
217
 
76
218
  After checking out the repo, run
@@ -87,7 +229,7 @@ You can also run `bin/console` for an interactive prompt that will allow you to
87
229
 
88
230
  ## Contributing
89
231
 
90
- Bug reports and pull requests are welcome on GitHub at https://github.com/garllon/graphql_connector. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
232
+ Bug reports and pull requests are welcome on GitHub at [https://github.com/garllon/graphql_connector](https://github.com/garllon/graphql_connector). This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
91
233
 
92
234
  ## License
93
235
 
@@ -95,4 +237,4 @@ The gem is available as open source under the terms of the [MIT License](https:/
95
237
 
96
238
  ## Code of Conduct
97
239
 
98
- Everyone interacting in the GraphqlConnector project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/graphql_connector/blob/master/CODE_OF_CONDUCT.md).
240
+ Everyone interacting in the GraphqlConnector project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/Garllon/graphql_connector/blob/master/CODE_OF_CONDUCT.md).
@@ -0,0 +1,51 @@
1
+ # frozen_string_literal: true
2
+
3
+ # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
4
+ # ! The following examples are used together with
5
+ # ! https://github.com/sushie1984/rails-graphql-server
6
+ # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
7
+
8
+ uri = 'http://rails-graphql-server.herokuapp.com/api/graphql'
9
+ GraphqlConnector.configure do |config|
10
+ config.add_server(name: 'RailsGraphqlServer', uri: uri, headers: {})
11
+ end
12
+
13
+ # Service class for fetching department data
14
+ class Department
15
+ extend GraphqlConnector::RailsGraphqlServer::Query
16
+
17
+ add_query all: :departments, returns: [:id, :name, employees: [:yearlySalary]]
18
+
19
+ add_query by_id: :departments,
20
+ params: [:id],
21
+ returns: [:id, :name, employees: [:yearlySalary]]
22
+
23
+ add_raw_query all_raw: 'query {
24
+ departments {
25
+ id name employees { yearlySalary }
26
+ }
27
+ }'
28
+
29
+ add_raw_query by_id_raw: 'query departments($id: [ID!]) {
30
+ departments(id: $id) {
31
+ name employees { name }
32
+ }
33
+ }',
34
+ params: [:id]
35
+
36
+ # We use a nested 'input' as of convention for
37
+ # https://github.com/sushie1984/rails-graphql-server/blob/master/app/graphql/input_objects/department_attributes.rb
38
+ add_mutation create: :createDepartment,
39
+ params: :input,
40
+ returns: [department: %i[id name location]]
41
+ end
42
+
43
+ Department.all
44
+
45
+ Department.by_id(id: %w[1 2])
46
+
47
+ Department.all_raw
48
+
49
+ Department.by_id_raw(id: %w[1 2])
50
+
51
+ Department.create(input: { attributes: { name: 'One', location: 'Berlin' } })