graphql_connector 0.2.0 → 1.2.1

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.
@@ -0,0 +1,108 @@
1
+ # frozen_string_literal: true
2
+
3
+ module GraphqlConnector
4
+ module ServiceClassable
5
+ # Module that allows to build query methods within the context of
6
+ # service class
7
+ module Queryable
8
+ CONDITIONS = 'binding.local_variables.map do |var|
9
+ [var, binding.local_variable_get(var)]
10
+ end.to_h'
11
+
12
+ def add_query(params: [], returns:, **method_to_query)
13
+ class_method_name = method_to_query.first[0]
14
+ query_type = method_to_query.first[1]
15
+ ReturnFieldsValidator.validate(returns)
16
+ ClassMethodValidator.validate_class_method(class_method_name, self)
17
+ ClassMethodValidator.validate_element_class_type(query_type, Symbol)
18
+
19
+ if params.empty?
20
+ return query_method(class_method_name, query_type, returns)
21
+ end
22
+
23
+ ParamsValidator.validate(params)
24
+ query_keyword_method(class_method_name, query_type, params, returns)
25
+ end
26
+
27
+ def add_raw_query(params: [], **method_to_raw_query)
28
+ class_method_name = method_to_raw_query.first[0]
29
+ query_string = method_to_raw_query.first[1]
30
+ ClassMethodValidator.validate_class_method(class_method_name, self)
31
+ ClassMethodValidator.validate_element_class_type(query_string, String)
32
+
33
+ if params.empty?
34
+ return raw_query_method(class_method_name, query_string)
35
+ end
36
+
37
+ ParamsValidator.validate(params)
38
+ raw_query_keyword_method(class_method_name, query_string, params)
39
+ end
40
+
41
+ def add_mutation(params: [], returns:, **method_to_query)
42
+ class_method_name = method_to_query.first[0]
43
+ query_type = method_to_query.first[1]
44
+ ReturnFieldsValidator.validate(returns)
45
+ ClassMethodValidator.validate_class_method(class_method_name, self)
46
+ ClassMethodValidator.validate_element_class_type(query_type, Symbol)
47
+
48
+ if params.empty?
49
+ return mutation_method(class_method_name, query_type, returns)
50
+ end
51
+
52
+ ParamsValidator.validate(params)
53
+ mutation_keyword_method(class_method_name, query_type, params, returns)
54
+ end
55
+
56
+ private
57
+
58
+ def query_method(class_method_name, query_type, return_fields)
59
+ define_singleton_method class_method_name do
60
+ http_client.query(query_type, {}, return_fields.to_a)
61
+ end
62
+ end
63
+
64
+ def raw_query_method(class_method_name, query_string)
65
+ define_singleton_method class_method_name do
66
+ http_client.raw_query(query_string)
67
+ end
68
+ end
69
+
70
+ def query_keyword_method(name, query_type, keywords, return_fields)
71
+ keywords = [keywords].flatten
72
+ instance_eval <<-METHOD, __FILE__, __LINE__ + 1
73
+ def #{name}(#{keywords.map { |keyword| "#{keyword}:" }.join(', ')})
74
+ http_client.query("#{query_type}",
75
+ #{CONDITIONS},
76
+ #{return_fields.to_a})
77
+ end
78
+ METHOD
79
+ end
80
+
81
+ def raw_query_keyword_method(name, query_string, keywords)
82
+ keywords = [keywords].flatten
83
+ instance_eval <<-METHOD, __FILE__, __LINE__ + 1
84
+ def #{name}(#{keywords.map { |keyword| "#{keyword}:" }.join(', ')})
85
+ http_client.raw_query("#{query_string}", variables: #{CONDITIONS})
86
+ end
87
+ METHOD
88
+ end
89
+
90
+ def mutation_method(class_method_name, query_type, return_fields)
91
+ define_singleton_method class_method_name do
92
+ http_client.mutation(query_type, {}, return_fields.to_a)
93
+ end
94
+ end
95
+
96
+ def mutation_keyword_method(name, query_type, keywords, return_fields)
97
+ keywords = [keywords].flatten
98
+ instance_eval <<-METHOD, __FILE__, __LINE__ + 1
99
+ def #{name}(#{keywords.map { |keyword| "#{keyword}:" }.join(', ')})
100
+ http_client.mutation("#{query_type}",
101
+ #{CONDITIONS},
102
+ #{return_fields.to_a})
103
+ end
104
+ METHOD
105
+ end
106
+ end
107
+ end
108
+ end
@@ -0,0 +1,43 @@
1
+ # frozen_string_literal: true
2
+
3
+ module GraphqlConnector
4
+ module ServiceClassable
5
+ class ReturnFieldsErrors < StandardError; end
6
+ # Valdations for return fields set within the context of a service class
7
+ class ReturnFieldsValidator
8
+ class << self
9
+ def validate(return_fields)
10
+ unless return_fields.is_a?(Array)
11
+ raise ReturnFieldsErrors, 'Please ensure that returns is followed '\
12
+ 'by an array. E.g. returns: [:id]'
13
+ end
14
+
15
+ return_fields.each { |entry| recursive_validation(entry) }
16
+ end
17
+
18
+ private
19
+
20
+ def recursive_validation(entry)
21
+ case entry
22
+ when Hash
23
+ hash_validation(entry)
24
+ when Array
25
+ entry.each { |item| recursive_validation(item) }
26
+ else
27
+ return if [String, Symbol].member?(entry.class)
28
+
29
+ raise ReturnFieldsErrors, "The #{entry} is neither a String nor a"\
30
+ 'Symbol!'
31
+ end
32
+ end
33
+
34
+ def hash_validation(hash)
35
+ hash.each do |key, value|
36
+ recursive_validation(key)
37
+ recursive_validation(value)
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
43
+ 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.2.1'
5
5
  end
metadata CHANGED
@@ -1,14 +1,15 @@
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.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Garllon
8
- autorequire:
8
+ - sushie1984
9
+ autorequire:
9
10
  bindir: exe
10
11
  cert_chain: []
11
- date: 2019-11-19 00:00:00.000000000 Z
12
+ date: 2021-05-25 00:00:00.000000000 Z
12
13
  dependencies:
13
14
  - !ruby/object:Gem::Dependency
14
15
  name: httparty
@@ -16,14 +17,14 @@ dependencies:
16
17
  requirements:
17
18
  - - "~>"
18
19
  - !ruby/object:Gem::Version
19
- version: '0.17'
20
+ version: '0.16'
20
21
  type: :runtime
21
22
  prerelease: false
22
23
  version_requirements: !ruby/object:Gem::Requirement
23
24
  requirements:
24
25
  - - "~>"
25
26
  - !ruby/object:Gem::Version
26
- version: '0.17'
27
+ version: '0.16'
27
28
  - !ruby/object:Gem::Dependency
28
29
  name: bundler
29
30
  requirement: !ruby/object:Gem::Requirement
@@ -52,20 +53,6 @@ dependencies:
52
53
  - - "~>"
53
54
  - !ruby/object:Gem::Version
54
55
  version: '0.10'
55
- - !ruby/object:Gem::Dependency
56
- name: rake
57
- requirement: !ruby/object:Gem::Requirement
58
- requirements:
59
- - - "~>"
60
- - !ruby/object:Gem::Version
61
- version: '10.0'
62
- type: :development
63
- prerelease: false
64
- version_requirements: !ruby/object:Gem::Requirement
65
- requirements:
66
- - - "~>"
67
- - !ruby/object:Gem::Version
68
- version: '10.0'
69
56
  - !ruby/object:Gem::Dependency
70
57
  name: rspec
71
58
  requirement: !ruby/object:Gem::Requirement
@@ -94,19 +81,20 @@ dependencies:
94
81
  - - "~>"
95
82
  - !ruby/object:Gem::Version
96
83
  version: '0.75'
97
- description: Simple grahql client to query with your own raw stringor with the samll
98
- helper method query.
84
+ description: Grahql client to query with your own raw string, with the small helper
85
+ method query or with service class inclusion.
99
86
  email:
100
87
  - palluthe.bennet@gmail.com
88
+ - sascha_burku@yahoo.de
101
89
  executables: []
102
90
  extensions: []
103
91
  extra_rdoc_files: []
104
92
  files:
93
+ - ".github/workflows/ci.yaml"
105
94
  - ".gitignore"
106
95
  - ".rspec"
107
96
  - ".rubocop.yml"
108
97
  - ".rubocop_todo.yml"
109
- - ".travis.yml"
110
98
  - CHANGELOG.md
111
99
  - CODE_OF_CONDUCT.md
112
100
  - Gemfile
@@ -114,11 +102,23 @@ files:
114
102
  - LICENSE.txt
115
103
  - README.md
116
104
  - bin/console
105
+ - examples/departments_service_class_examples.rb
106
+ - examples/mutation_examples.rb
107
+ - examples/query_examples.rb
108
+ - examples/raw_query_examples.rb
117
109
  - graphql_connector.gemspec
118
110
  - lib/graphql_connector.rb
111
+ - lib/graphql_connector/base_server_type.rb
119
112
  - lib/graphql_connector/configuration.rb
120
113
  - lib/graphql_connector/custom_attribute_error.rb
121
- - lib/graphql_connector/query_builder.rb
114
+ - lib/graphql_connector/formatters/base_format.rb
115
+ - lib/graphql_connector/formatters/mutation_format.rb
116
+ - lib/graphql_connector/formatters/query_format.rb
117
+ - lib/graphql_connector/http_client.rb
118
+ - lib/graphql_connector/service_classable/class_method_validator.rb
119
+ - lib/graphql_connector/service_classable/params_validator.rb
120
+ - lib/graphql_connector/service_classable/queryable.rb
121
+ - lib/graphql_connector/service_classable/return_fields_validator.rb
122
122
  - lib/graphql_connector/version.rb
123
123
  homepage: https://github.com/Garllon/graphql_connector/blob/master/README.md
124
124
  licenses:
@@ -127,7 +127,7 @@ metadata:
127
127
  homepage_uri: https://github.com/Garllon/graphql_connector/blob/master/README.md
128
128
  source_code_uri: https://github.com/Garllon/graphql_connector
129
129
  changelog_uri: https://github.com/Garllon/graphql_connector/blob/master/CHANGELOG.md
130
- post_install_message:
130
+ post_install_message:
131
131
  rdoc_options: []
132
132
  require_paths:
133
133
  - lib
@@ -142,8 +142,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
142
142
  - !ruby/object:Gem::Version
143
143
  version: '0'
144
144
  requirements: []
145
- rubygems_version: 3.0.6
146
- signing_key:
145
+ rubygems_version: 3.2.7
146
+ signing_key:
147
147
  specification_version: 4
148
- summary: Simple GraphQL client
148
+ summary: GraphQL client
149
149
  test_files: []
data/.travis.yml DELETED
@@ -1,9 +0,0 @@
1
- ---
2
- sudo: false
3
- language: ruby
4
- cache: bundler
5
- rvm:
6
- - 2.6.4
7
- before_install: gem install bundler -v 2.0.2
8
- script:
9
- - bundle exec rspec spec
@@ -1,59 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module GraphqlConnector
4
- # create the graphql query_string out of the given attributes.
5
- class QueryBuilder
6
- def initialize(model, conditions, selected_fields)
7
- @model = model
8
- @conditions = conditions
9
- @selected_fields = selected_fields
10
- end
11
-
12
- def create
13
- "query { #{main_filter} { #{parse_fields(@selected_fields)} } }"
14
- end
15
-
16
- private
17
-
18
- def main_filter
19
- conditions = @conditions.each_with_object([]) do |(key, value), array|
20
- next if value.is_a? Hash # will be processed in #field_with_filter
21
-
22
- array << "#{key}: #{value_as_parameter(value)}"
23
- end
24
-
25
- "#{@model}(#{conditions.join(', ')})"
26
- end
27
-
28
- def value_as_parameter(value)
29
- case value
30
- when TrueClass, FalseClass, Integer, Float
31
- value
32
- when Array
33
- casted_values = value.map { |v| value_as_parameter(v) }
34
- "[#{casted_values.join(',')}]"
35
- else # fallback to string
36
- '"' + value.to_s + '"'
37
- end
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
58
- end
59
- end