graphql_connector 0.2.0 → 1.0.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: 8ce0306b4b797a61fd2e4e26036731e5b702c7a3b3772f68f90f8db2f124f1ef
4
- data.tar.gz: c7afc2f11f5a6c91bc3f788b08fda857de8451cba6905f179691f002ce6ba643
3
+ metadata.gz: 3fe4b7086d0edec851e2a6b7f12708e805e6d5eac0454d5eef4aa5cb3267ef4f
4
+ data.tar.gz: e2261580d8b3b238a7f9c9e1bdc6227290dc42efd51931d15a1a54d54e06afc9
5
5
  SHA512:
6
- metadata.gz: 82de4e8e154f407995fb1fbe865ee59610d9b283f876e54af68bce38d20a2bc80fe00a08b2b104a6c532c551b6e242fb3b4792efdc5f27122fc4ad597b9edf76
7
- data.tar.gz: da700041749e3f7d1ab40f13258c073556d6fc7f316224f3aa3c91368dce06895d9a3b9b4ac6098c896268b147a1cc370d19fe8072683b6255f028ca1c4a6aa2
6
+ metadata.gz: 8b45378200a895f3f163e52e99dce0547fa024316cceb5e3e0fa3a1b67859abc230435096667bdf1cb997a67b5b3d00c6e5fccf5d5ea59a1fac7f042b91c5b08
7
+ data.tar.gz: 621b269795d71c4bdf8ce268cb93b6f5e084e7c434c6d17e01b1c18179e8f133f3cd7b44716669e7d829ebdcbb51f8530b087432669ebd98ebe0a6996a619e95
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,23 @@
1
+ ## 1.0.0 (2020-1-26)
1
2
 
2
- ### 0.2.0
3
+ ### Breaking
4
+ * add multiple graphql server querying
5
+ * `query` and `raw_query` nested under server namespace
3
6
 
4
- ** Features
7
+
8
+ ## 0.2.0 (2019-11-19)
9
+
10
+ ### Features
5
11
  * 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.
12
+ string by your self and also you get only the parsed json back.
7
13
  * query supports associations for the selected_fields attribute
8
14
 
15
+ ## 0.1.0.beta1 (2019-10-06)
9
16
 
10
- ### 0.1.0.beta1
11
-
12
- ** BugFix
13
- use model instead of hardcoded product
17
+ ### BugFix
18
+ * use model instead of hardcoded product
14
19
 
15
20
 
16
- ### 0.1.0.beta
21
+ ## 0.1.0.beta (2019-10-03)
17
22
 
18
- easy graphql logic
23
+ * easy graphql logic
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- graphql_connector (0.2.0)
4
+ graphql_connector (1.0.0)
5
5
  httparty (~> 0.17)
6
6
 
7
7
  GEM
data/README.md CHANGED
@@ -1,12 +1,14 @@
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
6
  [![Build
6
7
  Status](https://travis-ci.org/Garllon/graphql_connector.svg?branch=master)](https://travis-ci.org/Garllon/graphql_connector)
8
+ [![Maintainability](https://api.codeclimate.com/v1/badges/548db3cf0d078b379c84/maintainability)](https://codeclimate.com/github/Garllon/graphql_connector/maintainability)
7
9
 
8
10
  An easy connector to call your `graphql` server. Currently there is no schema
9
- check in the code, but i will add this.
11
+ check in the code, but i will add it.
10
12
 
11
13
  ## Installation
12
14
 
@@ -29,23 +31,26 @@ Or install it yourself as:
29
31
  You need to configure the `graphql_connector` first:
30
32
  ``` ruby
31
33
  GraphqlConnector.configure do |config|
32
- config.host = ''
33
- config.headers = {}
34
+ config.add_server(name: 'Foo', uri: 'http://foo.com/api/graphql', headers: {})
34
35
  end
36
+
37
+ For each graphql server you wish to query use `add_server`.
35
38
  ```
36
39
 
37
40
  ### raw_query
38
41
 
39
42
  Then you can call your graphql_endpoint:
40
43
  ```ruby
41
- GraphqlConnector.query(model, condition, selected_fields)
44
+ GraphqlConnector::<name>.raw_query(query_string)
42
45
  ```
43
46
 
47
+ Note that `<name>` has to be replaced by any of the ones added via `add_mapping`
48
+
44
49
  ### query
45
50
 
46
- Or your are using the more comfortable `query`:
51
+ You can also use the more comfortable `query`:
47
52
  ```ruby
48
- GraphqlConnector.query(model, condition, selected_fields)
53
+ GraphqlConnector::<name>.query(model, condition, selected_fields)
49
54
  ```
50
55
 
51
56
  | Variable | DataType | Example |
@@ -57,7 +62,7 @@ GraphqlConnector.query(model, condition, selected_fields)
57
62
  > Caution:
58
63
  > You get an OpenStruct back. Currently only the first level attributes are
59
64
  > supported with OpenStruct, associated objects are still a normal array of
60
- > hashes.
65
+ > hashes.
61
66
 
62
67
  #### selected_fields
63
68
 
@@ -87,7 +92,7 @@ You can also run `bin/console` for an interactive prompt that will allow you to
87
92
 
88
93
  ## Contributing
89
94
 
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.
95
+ 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
96
 
92
97
  ## License
93
98
 
@@ -12,7 +12,7 @@ Gem::Specification.new do |spec|
12
12
 
13
13
  spec.summary = 'Simple GraphQL client'
14
14
  spec.description = 'Simple grahql client to query with your own raw string'\
15
- 'or with the samll helper method query.'
15
+ ' or with the small helper method query.'
16
16
  spec.homepage = 'https://github.com/Garllon/graphql_connector/blob/master/README.md'
17
17
  spec.license = 'MIT'
18
18
 
@@ -3,6 +3,8 @@
3
3
  require 'graphql_connector/version'
4
4
  require 'graphql_connector/query_builder'
5
5
  require 'graphql_connector/configuration'
6
+ require 'graphql_connector/http_client'
7
+ require 'graphql_connector/base_server_type'
6
8
  require 'graphql_connector/custom_attribute_error'
7
9
  require 'httparty'
8
10
 
@@ -25,23 +27,4 @@ module GraphqlConnector
25
27
  def self.configure
26
28
  yield(configuration)
27
29
  end
28
-
29
- def self.query(model, conditions, selected_fields)
30
- query_string = QueryBuilder.new(model, conditions, selected_fields).create
31
- parsed_body = raw_query(query_string)
32
- OpenStruct.new(parsed_body['data'][model])
33
- end
34
-
35
- def self.raw_query(query_string)
36
- response = HTTParty.post(GraphqlConnector.configuration.host,
37
- headers: GraphqlConnector.configuration.headers,
38
- body: { query: query_string })
39
- parsed_body = JSON.parse(response.body)
40
-
41
- if parsed_body.key? 'errors'
42
- raise CustomAttributeError, parsed_body['errors']
43
- end
44
-
45
- parsed_body
46
- end
47
30
  end
@@ -0,0 +1,57 @@
1
+ # frozen_string_literal: true
2
+
3
+ module GraphqlConnector
4
+ class BaseServerTypeAlreadyExistsError < StandardError; end
5
+ # Class to wrap http_client calls under a specific namespaced class
6
+ class BaseServerType
7
+ class << self
8
+ def build(name, uri, headers)
9
+ verify_new_client_type_for!(name)
10
+ base_class = class_with(uri, headers)
11
+ base_object = GraphqlConnector.const_set(name, base_class)
12
+ inject_http_client(base_object)
13
+ inject_query_methods(base_object)
14
+
15
+ base_object
16
+ end
17
+
18
+ private
19
+
20
+ def verify_new_client_type_for!(name)
21
+ return unless GraphqlConnector.const_defined?(name)
22
+
23
+ raise BaseServerTypeAlreadyExistsError,
24
+ "The name: #{name} is already in use. Check your "\
25
+ 'configuration!'
26
+ end
27
+
28
+ def class_with(uri, headers)
29
+ Class.new do
30
+ attr_accessor :uri, :headers
31
+ @uri = uri
32
+ @headers = headers
33
+ end
34
+ end
35
+
36
+ def inject_http_client(base_object)
37
+ base_object.instance_eval do
38
+ def http_client
39
+ @http_client ||= GraphqlConnector::HttpClient.new(@uri, @headers)
40
+ end
41
+ end
42
+ end
43
+
44
+ def inject_query_methods(base_object)
45
+ base_object.instance_eval do
46
+ def query(model, conditions, selected_fields)
47
+ http_client.query(model, conditions, selected_fields)
48
+ end
49
+
50
+ def raw_query(query_string)
51
+ http_client.raw_query(query_string)
52
+ end
53
+ end
54
+ end
55
+ end
56
+ end
57
+ end
@@ -3,11 +3,21 @@
3
3
  module GraphqlConnector
4
4
  # The configuration template file for the gem.
5
5
  class Configuration
6
- attr_accessor :host, :headers
6
+ attr_reader :base_server_types
7
7
 
8
8
  def initialize
9
- @host = nil
10
- @headers = nil
9
+ @base_server_types = {}
10
+ end
11
+
12
+ def add_server(name:, uri:, headers:)
13
+ @base_server_types[name] = BaseServerType.build(name, uri, headers)
14
+ end
15
+
16
+ def reset!
17
+ @base_server_types.keys.each do |name|
18
+ GraphqlConnector.send :remove_const, name
19
+ end
20
+ @base_server_types = {}
11
21
  end
12
22
  end
13
23
  end
@@ -0,0 +1,39 @@
1
+ # frozen_string_literal: true
2
+
3
+ module GraphqlConnector
4
+ # Wrapper class for HTTParty post query
5
+ class HttpClient
6
+ def initialize(uri, headers)
7
+ @uri = uri
8
+ @headers = headers
9
+ end
10
+
11
+ def query(model, conditions, selected_fields)
12
+ query_string = GraphqlConnector::QueryBuilder.new(model,
13
+ conditions,
14
+ selected_fields).create
15
+ parsed_body = raw_query(query_string)
16
+ result = parsed_body['data'][model]
17
+ return OpenStruct.new(result) unless result.is_a? Array
18
+
19
+ result.map { |entry| OpenStruct.new(entry) }
20
+ end
21
+
22
+ def raw_query(query_string)
23
+ response = HTTParty.post(@uri,
24
+ headers: @headers,
25
+ body: { query: query_string })
26
+ parsed_body = JSON.parse(response.body)
27
+ verify_response!(parsed_body)
28
+ parsed_body
29
+ end
30
+
31
+ private
32
+
33
+ def verify_response!(parsed_body)
34
+ return unless parsed_body.key? 'errors'
35
+
36
+ raise CustomAttributeError, parsed_body['errors']
37
+ end
38
+ end
39
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module GraphqlConnector
4
- VERSION = '0.2.0'
4
+ VERSION = '1.0.0'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: graphql_connector
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Garllon
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-11-19 00:00:00.000000000 Z
11
+ date: 2020-01-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: httparty
@@ -94,7 +94,7 @@ dependencies:
94
94
  - - "~>"
95
95
  - !ruby/object:Gem::Version
96
96
  version: '0.75'
97
- description: Simple grahql client to query with your own raw stringor with the samll
97
+ description: Simple grahql client to query with your own raw string or with the small
98
98
  helper method query.
99
99
  email:
100
100
  - palluthe.bennet@gmail.com
@@ -116,8 +116,10 @@ files:
116
116
  - bin/console
117
117
  - graphql_connector.gemspec
118
118
  - lib/graphql_connector.rb
119
+ - lib/graphql_connector/base_server_type.rb
119
120
  - lib/graphql_connector/configuration.rb
120
121
  - lib/graphql_connector/custom_attribute_error.rb
122
+ - lib/graphql_connector/http_client.rb
121
123
  - lib/graphql_connector/query_builder.rb
122
124
  - lib/graphql_connector/version.rb
123
125
  homepage: https://github.com/Garllon/graphql_connector/blob/master/README.md