hq-graphql 2.1.6 → 2.1.11

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: 6bec5e8d7faf20cfc45c6420b0f989aee48ca2b5c6f72a3d2a9ff1d4be030530
4
- data.tar.gz: 6d71560948e242279e32ec521a86c351e7c2278af78694581f5e1bf27b5807a5
3
+ metadata.gz: 2dc8a13aebd5bba09acb1b42fe2ece1bae74b7a19ee369f8ce225192f243bc56
4
+ data.tar.gz: ffbf49f7c59f4aa1bae28a04559c296ea16d6eeefab4cb904b4b3d3126a95176
5
5
  SHA512:
6
- metadata.gz: c62a8b032a5f43dd27c8a50685739cc6a07d21bab55a7b4a6dc750377080f9a3440c6954a129ef56c1c4bd60a90f4d390efae6937875d949c66a5b26b5ab918b
7
- data.tar.gz: 40966a645c6777ee54311a8e8eb3f7df001413fdd91d1bcb21e1928c947299a331e69a18960fba6f0102996559f06c2dc56bdeb7ba611b5707b3f7830e61553d
6
+ metadata.gz: fd94207679a00f9a67a72719cb8deccc0c489050a43aa6ba52ffc1daac9bde7380b47748db634de92d43751791d7597dffa136b1818b9577428c0799ac361eb4
7
+ data.tar.gz: a811ef46fcce2c00290fdc22e61e8775b6d5367fb3afcded30796f4e72778f793dac0c0fce79ada7527ddbd06404cad3f7cc918548de3e2420bd6522adc8306d
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,13 +95,21 @@ 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|
108
+ !association.options[:optional] || has_presence_validation?(association)
109
+ end
110
+
111
+ def has_presence_validation?(association)
112
+ model_klass.validators.any? do |validation|
101
113
  next unless validation.class == ActiveRecord::Validations::PresenceValidator
102
114
  validation.attributes.any? { |a| a.to_s == association.name.to_s }
103
115
  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.6"
5
+ VERSION = "2.1.11"
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.6
4
+ version: 2.1.11
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-14 00:00:00.000000000 Z
11
+ date: 2020-11-02 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
  - - ">="