graphql_connector 0.1.0.beta1 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +2 -0
- data/.rubocop_todo.yml +3 -0
- data/CHANGELOG.md +6 -2
- data/Gemfile.lock +2 -2
- data/README.md +37 -8
- data/graphql_connector.gemspec +5 -3
- data/lib/graphql_connector.rb +15 -1
- data/lib/graphql_connector/configuration.rb +1 -0
- data/lib/graphql_connector/custom_attribute_error.rb +3 -0
- data/lib/graphql_connector/query_builder.rb +22 -2
- data/lib/graphql_connector/version.rb +1 -1
- metadata +10 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8ce0306b4b797a61fd2e4e26036731e5b702c7a3b3772f68f90f8db2f124f1ef
|
4
|
+
data.tar.gz: c7afc2f11f5a6c91bc3f788b08fda857de8451cba6905f179691f002ce6ba643
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 82de4e8e154f407995fb1fbe865ee59610d9b283f876e54af68bce38d20a2bc80fe00a08b2b104a6c532c551b6e242fb3b4792efdc5f27122fc4ad597b9edf76
|
7
|
+
data.tar.gz: da700041749e3f7d1ab40f13258c073556d6fc7f316224f3aa3c91368dce06895d9a3b9b4ac6098c896268b147a1cc370d19fe8072683b6255f028ca1c4a6aa2
|
data/.rubocop.yml
ADDED
data/.rubocop_todo.yml
ADDED
data/CHANGELOG.md
CHANGED
@@ -1,7 +1,11 @@
|
|
1
1
|
|
2
|
-
### 0.
|
2
|
+
### 0.2.0
|
3
|
+
|
4
|
+
** Features
|
5
|
+
* 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.
|
7
|
+
* query supports associations for the selected_fields attribute
|
3
8
|
|
4
|
-
TBA
|
5
9
|
|
6
10
|
### 0.1.0.beta1
|
7
11
|
|
data/Gemfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
graphql_connector (0.
|
4
|
+
graphql_connector (0.2.0)
|
5
5
|
httparty (~> 0.17)
|
6
6
|
|
7
7
|
GEM
|
@@ -17,7 +17,7 @@ GEM
|
|
17
17
|
method_source (0.9.2)
|
18
18
|
mime-types (3.3)
|
19
19
|
mime-types-data (~> 3.2015)
|
20
|
-
mime-types-data (3.2019.
|
20
|
+
mime-types-data (3.2019.1009)
|
21
21
|
multi_xml (0.6.0)
|
22
22
|
parallel (1.17.0)
|
23
23
|
parser (2.6.5.0)
|
data/README.md
CHANGED
@@ -5,8 +5,8 @@ Version](https://badge.fury.io/rb/graphql_connector.svg)](https://badge.fury.io/
|
|
5
5
|
[](https://travis-ci.org/Garllon/graphql_connector)
|
7
7
|
|
8
|
-
An easy connector to call your `graphql` server.
|
9
|
-
|
8
|
+
An easy connector to call your `graphql` server. Currently there is no schema
|
9
|
+
check in the code, but i will add this.
|
10
10
|
|
11
11
|
## Installation
|
12
12
|
|
@@ -34,16 +34,42 @@ GraphqlConnector.configure do |config|
|
|
34
34
|
end
|
35
35
|
```
|
36
36
|
|
37
|
+
### raw_query
|
38
|
+
|
37
39
|
Then you can call your graphql_endpoint:
|
38
40
|
```ruby
|
39
41
|
GraphqlConnector.query(model, condition, selected_fields)
|
40
42
|
```
|
41
43
|
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
44
|
+
### query
|
45
|
+
|
46
|
+
Or your are using the more comfortable `query`:
|
47
|
+
```ruby
|
48
|
+
GraphqlConnector.query(model, condition, selected_fields)
|
49
|
+
```
|
50
|
+
|
51
|
+
| Variable | DataType | Example |
|
52
|
+
|----------------|-------------------------|------------------------------------------|
|
53
|
+
| model | String | 'product' |
|
54
|
+
| condition | Hash(key, value) | { id: 1 } |
|
55
|
+
| selected_fields | Array of Strings/Hashes | ['id', 'name', productCategory: ['id']] |
|
56
|
+
|
57
|
+
> Caution:
|
58
|
+
> You get an OpenStruct back. Currently only the first level attributes are
|
59
|
+
> supported with OpenStruct, associated objects are still a normal array of
|
60
|
+
> hashes.
|
61
|
+
|
62
|
+
#### selected_fields
|
63
|
+
|
64
|
+
The synatx for the associations looks like the following:
|
65
|
+
```
|
66
|
+
['<attribute_name>', <association_name>: ['<attribute_name_of_the_association>']]
|
67
|
+
```
|
68
|
+
|
69
|
+
Example:
|
70
|
+
```ruby
|
71
|
+
['id', 'name', productCategory: ['id', 'name']]
|
72
|
+
```
|
47
73
|
|
48
74
|
## Development
|
49
75
|
|
@@ -53,7 +79,10 @@ bundle install
|
|
53
79
|
```
|
54
80
|
|
55
81
|
Then, run
|
56
|
-
```
|
82
|
+
```shell
|
83
|
+
bundle exec rspec spec
|
84
|
+
```
|
85
|
+
to run the tests.
|
57
86
|
You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
58
87
|
|
59
88
|
## Contributing
|
data/graphql_connector.gemspec
CHANGED
@@ -10,8 +10,9 @@ Gem::Specification.new do |spec|
|
|
10
10
|
spec.authors = ['Garllon']
|
11
11
|
spec.email = ['palluthe.bennet@gmail.com']
|
12
12
|
|
13
|
-
spec.summary = '
|
14
|
-
spec.description = '
|
13
|
+
spec.summary = 'Simple GraphQL client'
|
14
|
+
spec.description = 'Simple grahql client to query with your own raw string'\
|
15
|
+
'or with the samll helper method query.'
|
15
16
|
spec.homepage = 'https://github.com/Garllon/graphql_connector/blob/master/README.md'
|
16
17
|
spec.license = 'MIT'
|
17
18
|
|
@@ -20,7 +21,8 @@ Gem::Specification.new do |spec|
|
|
20
21
|
spec.metadata['changelog_uri'] = 'https://github.com/Garllon/graphql_connector/blob/master/CHANGELOG.md'
|
21
22
|
|
22
23
|
# Specify which files should be added to the gem when it is released.
|
23
|
-
# The `git ls-files -z` loads the files in the RubyGem that have been added
|
24
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added
|
25
|
+
# into git.
|
24
26
|
spec.files = Dir.chdir(File.expand_path(__dir__)) do
|
25
27
|
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(spec|features)/}) }
|
26
28
|
end
|
data/lib/graphql_connector.rb
CHANGED
@@ -3,8 +3,12 @@
|
|
3
3
|
require 'graphql_connector/version'
|
4
4
|
require 'graphql_connector/query_builder'
|
5
5
|
require 'graphql_connector/configuration'
|
6
|
+
require 'graphql_connector/custom_attribute_error'
|
6
7
|
require 'httparty'
|
7
8
|
|
9
|
+
# Main file of the GraphQLConnector
|
10
|
+
# the main methods to configure the gem
|
11
|
+
# and to run a raw_query or a normal query.
|
8
12
|
module GraphqlConnector
|
9
13
|
class << self
|
10
14
|
attr_accessor :configuration
|
@@ -24,10 +28,20 @@ module GraphqlConnector
|
|
24
28
|
|
25
29
|
def self.query(model, conditions, selected_fields)
|
26
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)
|
27
36
|
response = HTTParty.post(GraphqlConnector.configuration.host,
|
28
37
|
headers: GraphqlConnector.configuration.headers,
|
29
38
|
body: { query: query_string })
|
30
39
|
parsed_body = JSON.parse(response.body)
|
31
|
-
|
40
|
+
|
41
|
+
if parsed_body.key? 'errors'
|
42
|
+
raise CustomAttributeError, parsed_body['errors']
|
43
|
+
end
|
44
|
+
|
45
|
+
parsed_body
|
32
46
|
end
|
33
47
|
end
|
@@ -1,6 +1,7 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
module GraphqlConnector
|
4
|
+
# create the graphql query_string out of the given attributes.
|
4
5
|
class QueryBuilder
|
5
6
|
def initialize(model, conditions, selected_fields)
|
6
7
|
@model = model
|
@@ -9,7 +10,7 @@ module GraphqlConnector
|
|
9
10
|
end
|
10
11
|
|
11
12
|
def create
|
12
|
-
"query { #{
|
13
|
+
"query { #{main_filter} { #{parse_fields(@selected_fields)} } }"
|
13
14
|
end
|
14
15
|
|
15
16
|
private
|
@@ -21,7 +22,7 @@ module GraphqlConnector
|
|
21
22
|
array << "#{key}: #{value_as_parameter(value)}"
|
22
23
|
end
|
23
24
|
|
24
|
-
conditions.join(', ')
|
25
|
+
"#{@model}(#{conditions.join(', ')})"
|
25
26
|
end
|
26
27
|
|
27
28
|
def value_as_parameter(value)
|
@@ -35,5 +36,24 @@ module GraphqlConnector
|
|
35
36
|
'"' + value.to_s + '"'
|
36
37
|
end
|
37
38
|
end
|
39
|
+
|
40
|
+
def parse_fields(selected_fields)
|
41
|
+
results = selected_fields.map do |field|
|
42
|
+
case field
|
43
|
+
when Hash
|
44
|
+
handle_association(field)
|
45
|
+
else
|
46
|
+
field
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
results.join(' ')
|
51
|
+
end
|
52
|
+
|
53
|
+
def handle_association(hash)
|
54
|
+
hash.map do |key, fields|
|
55
|
+
"#{key} { #{parse_fields(fields)} }"
|
56
|
+
end
|
57
|
+
end
|
38
58
|
end
|
39
59
|
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.
|
4
|
+
version: 0.2.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
|
+
date: 2019-11-19 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: httparty
|
@@ -94,7 +94,8 @@ dependencies:
|
|
94
94
|
- - "~>"
|
95
95
|
- !ruby/object:Gem::Version
|
96
96
|
version: '0.75'
|
97
|
-
description:
|
97
|
+
description: Simple grahql client to query with your own raw stringor with the samll
|
98
|
+
helper method query.
|
98
99
|
email:
|
99
100
|
- palluthe.bennet@gmail.com
|
100
101
|
executables: []
|
@@ -103,6 +104,8 @@ extra_rdoc_files: []
|
|
103
104
|
files:
|
104
105
|
- ".gitignore"
|
105
106
|
- ".rspec"
|
107
|
+
- ".rubocop.yml"
|
108
|
+
- ".rubocop_todo.yml"
|
106
109
|
- ".travis.yml"
|
107
110
|
- CHANGELOG.md
|
108
111
|
- CODE_OF_CONDUCT.md
|
@@ -114,6 +117,7 @@ files:
|
|
114
117
|
- graphql_connector.gemspec
|
115
118
|
- lib/graphql_connector.rb
|
116
119
|
- lib/graphql_connector/configuration.rb
|
120
|
+
- lib/graphql_connector/custom_attribute_error.rb
|
117
121
|
- lib/graphql_connector/query_builder.rb
|
118
122
|
- lib/graphql_connector/version.rb
|
119
123
|
homepage: https://github.com/Garllon/graphql_connector/blob/master/README.md
|
@@ -134,12 +138,12 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
134
138
|
version: '0'
|
135
139
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
136
140
|
requirements:
|
137
|
-
- - "
|
141
|
+
- - ">="
|
138
142
|
- !ruby/object:Gem::Version
|
139
|
-
version:
|
143
|
+
version: '0'
|
140
144
|
requirements: []
|
141
145
|
rubygems_version: 3.0.6
|
142
146
|
signing_key:
|
143
147
|
specification_version: 4
|
144
|
-
summary:
|
148
|
+
summary: Simple GraphQL client
|
145
149
|
test_files: []
|