hq-graphql 2.1.7 → 2.1.12

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: bb02d8ac286808a6ec7c77a888691740036aedb2e570b0cd493ffd52723ecbae
4
- data.tar.gz: 84aac61cd4de7dc2d38fa9e325efade24f06c892db0a9fd89f0fa9ca96d43340
3
+ metadata.gz: 34f3a14ee58de081c8d52088ebc7986cbc2ccf597392015bcb44aa629aef6556
4
+ data.tar.gz: 39a436fca18cae2f81e672e78ae3118c4893512781a594c24d086cbf0f7e00c1
5
5
  SHA512:
6
- metadata.gz: 0a8ff7468d91f94463b8311daa52468229414193982b1e110ab80ded645780792086c15f4a96844761c276e0f4916af36309cc3dc9749967c677a342ebd669f7
7
- data.tar.gz: 8b4342e2b679c368941ee81ded4d3f4b4946576a7e6f65c2fd16bd830257233de7123f170068f262bc17d0b9fe1b0de179252624a23204c7312590d6e401f5ba
6
+ metadata.gz: '09606ace844e47c5c414a73d1f3b2ffd7a7183b173090ade1e6287bc42aa6cc84eed691dd3d3a2b8d7ad4f8045779550f619fab017ed0dd79c49621c007ad9d1'
7
+ data.tar.gz: d8a07ccf03a311b807c7669202ce54d64e89db640f15911f2a1c6ddc3237d9fa94410e8527ae70539659a86ca06cf89317409b00d98b589056b9d6db5ae80dfc
data/README.md CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  OneHQ GraphQL interface to [Ruby Graphql](https://github.com/rmosolgo/graphql-ruby).
4
4
 
5
- [![CircleCI](https://img.shields.io/circleci/project/github/OneHQ/hq-graphql.svg)](https://circleci.com/gh/OneHQ/hq-graphql/tree/master)
5
+ ![Test and Lint](https://github.com/OneHQ/hq-graphql/workflows/Test%20and%20Lint/badge.svg)
6
6
  [![GitHub tag](https://img.shields.io/github/tag/OneHQ/hq-graphql.svg)](https://github.com/OneHQ/hq-graphql)
7
7
 
8
8
  ## Configuration
@@ -18,6 +18,15 @@ Define a global default scope.
18
18
  end
19
19
  ```
20
20
 
21
+ ### Excluded Inputs
22
+ Define a global excluded input fields. Useful for excluding (autogenerated | auto set) fields like below.
23
+
24
+ ```ruby
25
+ ::HQ::GraphQL.configure do |config|
26
+ config.excluded_inputs = [:id, :created_at, :updated_at]
27
+ end
28
+ ```
29
+
21
30
  ## GraphQL Resource
22
31
  Connect to ActiveRecord to auto generate queries and mutations.
23
32
 
@@ -36,6 +36,10 @@ module HQ
36
36
  config.extract_class.call(klass)
37
37
  end
38
38
 
39
+ def self.excluded_inputs
40
+ config.excluded_inputs || []
41
+ end
42
+
39
43
  def self.lookup_resource(klass)
40
44
  [klass, klass.base_class, klass.superclass].lazy.map do |k|
41
45
  config.resource_lookup.call(k) || resources.detect { |r| r.model_klass == k }
@@ -124,6 +124,10 @@ module HQ
124
124
  @removed_associations ||= []
125
125
  end
126
126
 
127
+ def camelize(name)
128
+ name.to_s.camelize(:lower)
129
+ end
130
+
127
131
  def is_enum?(association)
128
132
  ::HQ::GraphQL.enums.include?(association.klass)
129
133
  end
@@ -10,6 +10,7 @@ module HQ
10
10
  :extract_class,
11
11
  :resource_lookup,
12
12
  :use_experimental_associations,
13
+ :excluded_inputs,
13
14
  keyword_init: true
14
15
  )
15
16
  def initialize(
@@ -34,6 +34,7 @@ module HQ::GraphQL
34
34
  prefix: nil,
35
35
  register: true,
36
36
  scope: nil,
37
+ strip: /(^[^_a-zA-Z])|([^_a-zA-Z0-9]*)/,
37
38
  value_method: :name
38
39
  )
39
40
  raise ArgumentError.new(<<~ERROR) if !klass
@@ -49,7 +50,7 @@ module HQ::GraphQL
49
50
  lazy_load do
50
51
  records = scope ? klass.instance_exec(&scope) : klass.all
51
52
  records.each do |record|
52
- value "#{prefix}#{record.send(value_method).delete(": ")}", value: record
53
+ value "#{prefix}#{record.send(value_method).gsub(strip, "")}", value: record
53
54
  end
54
55
  end
55
56
  end
@@ -32,19 +32,21 @@ module HQ
32
32
  end
33
33
 
34
34
  #### Class Methods ####
35
- def self.with_model(model_name, attributes: true, associations: false, enums: true)
35
+ def self.with_model(model_name, attributes: true, associations: false, enums: true, excluded_inputs: [])
36
36
  self.model_name = model_name
37
37
  self.auto_load_attributes = attributes
38
38
  self.auto_load_associations = associations
39
39
  self.auto_load_enums = enums
40
40
 
41
41
  lazy_load do
42
+ excluded_inputs += ::HQ::GraphQL.excluded_inputs
43
+
42
44
  model_columns.each do |column|
43
- argument_from_column(column)
45
+ argument_from_column(column) unless excluded_inputs.include?(column.name.to_sym)
44
46
  end
45
47
 
46
48
  model_associations.each do |association|
47
- argument_from_association association
49
+ argument_from_association(association) unless excluded_inputs.include?(association.name.to_sym)
48
50
  end
49
51
 
50
52
  argument :X, String, required: false
@@ -67,6 +69,7 @@ module HQ
67
69
  is_enum = is_enum?(association)
68
70
  input_or_type = is_enum ? ::HQ::GraphQL::Types[association.klass] : ::HQ::GraphQL::Inputs[association.klass]
69
71
  name = association.name
72
+ return if argument_exists?(name)
70
73
 
71
74
  case association.macro
72
75
  when :has_many
@@ -87,7 +90,13 @@ module HQ
87
90
  end
88
91
 
89
92
  def argument_from_column(column)
90
- argument column.name, ::HQ::GraphQL::Types.type_from_column(column), required: false
93
+ name = column.name
94
+ return if argument_exists?(name)
95
+ argument name, ::HQ::GraphQL::Types.type_from_column(column), required: false
96
+ end
97
+
98
+ def argument_exists?(name)
99
+ !!arguments[camelize(name)]
91
100
  end
92
101
  end
93
102
  end
@@ -65,7 +65,7 @@ module HQ
65
65
  def field_from_association(association, auto_nil:, internal_association: false, &block)
66
66
  association_klass = association.klass
67
67
  name = association.name.to_s
68
- return if fields[name]
68
+ return if field_exists?(name)
69
69
 
70
70
  klass = model_klass
71
71
  type = Types[association_klass]
@@ -80,6 +80,10 @@ module HQ
80
80
  end
81
81
  instance_eval(&block) if block
82
82
  end
83
+ when :has_one
84
+ field name, type, null: !auto_nil || !has_presence_validation?(association), klass: model_name do
85
+ extension FieldExtension::AssociationLoaderExtension, klass: klass
86
+ end
83
87
  else
84
88
  field name, type, null: !auto_nil || !association_required?(association), klass: model_name do
85
89
  extension FieldExtension::AssociationLoaderExtension, klass: klass
@@ -91,14 +95,22 @@ module HQ
91
95
 
92
96
  def field_from_column(column, auto_nil:)
93
97
  name = column.name
94
- return if fields[name]
98
+ return if field_exists?(name)
95
99
 
96
100
  field name, Types.type_from_column(column), null: !auto_nil || column.null
97
101
  end
98
102
 
103
+ def field_exists?(name)
104
+ !!fields[camelize(name)]
105
+ end
106
+
99
107
  def association_required?(association)
100
- !association.options[:optional] || model_klass.validators.any? do |validation|
101
- next unless validation.class == ActiveRecord::Validations::PresenceValidator
108
+ !association.options[:optional] || has_presence_validation?(association)
109
+ end
110
+
111
+ def has_presence_validation?(association)
112
+ model_klass.validators.any? do |validation|
113
+ next unless validation.class == ActiveRecord::Validations::PresenceValidator && !(validation.options.include?(:if) || validation.options.include?(:unless))
102
114
  validation.attributes.any? { |a| a.to_s == association.name.to_s }
103
115
  end
104
116
  end
@@ -15,6 +15,11 @@ module HQ
15
15
  add_reflection(name, scope, options, :belongs_to, block)
16
16
  end
17
17
 
18
+ def has_one(name, scope = nil, through: nil, **options, &block)
19
+ raise TypeError, "has_one through is unsupported" if through
20
+ add_reflection(name, scope, options, :has_one, block)
21
+ end
22
+
18
23
  def has_many(name, scope = nil, through: nil, **options, &block)
19
24
  raise TypeError, "has_many through is unsupported" if through
20
25
  add_reflection(name, scope, options, :has_many, block)
@@ -108,6 +108,10 @@ module HQ
108
108
  self.sort_fields_enum = fields
109
109
  end
110
110
 
111
+ def excluded_inputs(*fields)
112
+ @excluded_inputs = fields
113
+ end
114
+
111
115
  def def_root(field_name, is_array: false, null: true, &block)
112
116
  resource = self
113
117
  resolver = -> {
@@ -181,10 +185,12 @@ module HQ
181
185
  def build_input_object(**options, &block)
182
186
  scoped_graphql_name = graphql_name
183
187
  scoped_model_name = model_name
188
+ scoped_excluded_inputs = @excluded_inputs || []
189
+
184
190
  Class.new(::HQ::GraphQL::InputObject) do
185
191
  graphql_name "#{scoped_graphql_name}Input"
186
192
 
187
- with_model scoped_model_name, **options
193
+ with_model scoped_model_name, excluded_inputs: scoped_excluded_inputs, **options
188
194
 
189
195
  class_eval(&block) if block
190
196
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module HQ
4
4
  module GraphQL
5
- VERSION = "2.1.7"
5
+ VERSION = "2.1.12"
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hq-graphql
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.1.7
4
+ version: 2.1.12
5
5
  platform: ruby
6
6
  authors:
7
7
  - Danny Jones
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-07-17 00:00:00.000000000 Z
11
+ date: 2020-11-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -285,9 +285,9 @@ require_paths:
285
285
  - lib
286
286
  required_ruby_version: !ruby/object:Gem::Requirement
287
287
  requirements:
288
- - - ">="
288
+ - - "~>"
289
289
  - !ruby/object:Gem::Version
290
- version: '0'
290
+ version: '2.6'
291
291
  required_rubygems_version: !ruby/object:Gem::Requirement
292
292
  requirements:
293
293
  - - ">="