hq-graphql 2.1.5 → 2.1.10

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 212005e9fc2e97777c5853273618ff5608c310db175324ac75a2e594cb3fa862
4
- data.tar.gz: 2b656d56fee16a199bc865370fcdaa4f3460414909b07b2086d9df82c56d2738
3
+ metadata.gz: 9dd3d2f47411b616e76f20e118b4a959af58a13223b5d8e758bb35919c5b2b02
4
+ data.tar.gz: 781c03507b71570f5e9c6b9dc34117f34649097d1e4026701fd740c78209acad
5
5
  SHA512:
6
- metadata.gz: 65e69ef87752fcd9322b06001fb8bd5dd91e7e6e234c43738776681c96d333d7729d3de88118ac1e7829b24d2c139aa71cb3affb055def6a5a63a63af3379079
7
- data.tar.gz: 42016b3a7db045b5a768abb5217b9e68baf6b1d8b29e25b758f25c2c559190d2569671a297c0bf423518dae1f20130541904f87b74608105279a237a3677b8ad
6
+ metadata.gz: f5be4f443b74567f67e082cec1ae52d83f3adb0b2ae7964afb2f49e74467ed0185c04f34f363f3a0bb153e97bf90c7d8eff2e831d9d137cb1ca7c669b9af2ac8
7
+ data.tar.gz: aecd6fcaf3f269d0bf2d7504cdebf4e022fd5e4c670d9ad5153f1658d127ba12ca0661711d7cfd1863615d0b94b7ab2098dd5d8e22a8fbb7271a41f5f5a89da5
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
@@ -77,6 +77,7 @@ require "hq/graphql/input_object"
77
77
  require "hq/graphql/mutation"
78
78
  require "hq/graphql/object"
79
79
  require "hq/graphql/paginated_association_loader"
80
+ require "hq/graphql/record_loader"
80
81
  require "hq/graphql/resource"
81
82
  require "hq/graphql/root_mutation"
82
83
  require "hq/graphql/root_query"
@@ -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
@@ -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
@@ -67,6 +67,7 @@ module HQ
67
67
  is_enum = is_enum?(association)
68
68
  input_or_type = is_enum ? ::HQ::GraphQL::Types[association.klass] : ::HQ::GraphQL::Inputs[association.klass]
69
69
  name = association.name
70
+ return if argument_exists?(name)
70
71
 
71
72
  case association.macro
72
73
  when :has_many
@@ -87,7 +88,13 @@ module HQ
87
88
  end
88
89
 
89
90
  def argument_from_column(column)
90
- argument column.name, ::HQ::GraphQL::Types.type_from_column(column), required: false
91
+ name = column.name
92
+ return if argument_exists?(name)
93
+ argument name, ::HQ::GraphQL::Types.type_from_column(column), required: false
94
+ end
95
+
96
+ def argument_exists?(name)
97
+ !!arguments[camelize(name)]
91
98
  end
92
99
  end
93
100
  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)
@@ -64,7 +64,6 @@ module HQ
64
64
 
65
65
  inside_scope = default_scope.
66
66
  select(inner_table[::Arel.star]).
67
- from(inner_table).
68
67
  where(lateral_join_table[target_join_key].eq(from_table[target_join_key])).
69
68
  reorder(arel_order(inner_table)).
70
69
  limit(@limit).
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ module HQ
4
+ module GraphQL
5
+ class RecordLoader < ::GraphQL::Batch::Loader
6
+ def initialize(model, column: model.primary_key, where: nil)
7
+ @model = model
8
+ @column = column.to_s
9
+ @column_type = model.type_for_attribute(@column)
10
+ @where = where
11
+ end
12
+
13
+ def load(key)
14
+ super(@column_type.cast(key))
15
+ end
16
+
17
+ def perform(keys)
18
+ query(keys).each { |record| fulfill(record.public_send(@column), record) }
19
+ keys.each { |key| fulfill(key, nil) unless fulfilled?(key) }
20
+ end
21
+
22
+ private
23
+
24
+ def query(keys)
25
+ scope = @model
26
+ scope = scope.where(@where) if @where
27
+ scope.where(@column => keys)
28
+ end
29
+ end
30
+ end
31
+ end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module HQ
4
4
  module GraphQL
5
- VERSION = "2.1.5"
5
+ VERSION = "2.1.10"
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.5
4
+ version: 2.1.10
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-08 00:00:00.000000000 Z
11
+ date: 2020-09-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -264,6 +264,7 @@ files:
264
264
  - lib/hq/graphql/object.rb
265
265
  - lib/hq/graphql/object_association.rb
266
266
  - lib/hq/graphql/paginated_association_loader.rb
267
+ - lib/hq/graphql/record_loader.rb
267
268
  - lib/hq/graphql/resource.rb
268
269
  - lib/hq/graphql/resource/auto_mutation.rb
269
270
  - lib/hq/graphql/root_mutation.rb
@@ -284,9 +285,9 @@ require_paths:
284
285
  - lib
285
286
  required_ruby_version: !ruby/object:Gem::Requirement
286
287
  requirements:
287
- - - ">="
288
+ - - "~>"
288
289
  - !ruby/object:Gem::Version
289
- version: '0'
290
+ version: '2.6'
290
291
  required_rubygems_version: !ruby/object:Gem::Requirement
291
292
  requirements:
292
293
  - - ">="