hq-graphql 2.1.12 → 2.2.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (37) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +25 -9
  3. data/lib/hq/graphql.rb +17 -8
  4. data/lib/hq/graphql/association_loader.rb +1 -0
  5. data/lib/hq/graphql/comparator.rb +22 -21
  6. data/lib/hq/graphql/enum/filter_operation.rb +15 -0
  7. data/lib/hq/graphql/enum/sort_by.rb +8 -4
  8. data/lib/hq/graphql/enum/sort_order.rb +8 -4
  9. data/lib/hq/graphql/ext.rb +8 -0
  10. data/lib/hq/graphql/ext/active_record_extensions.rb +148 -0
  11. data/lib/hq/graphql/ext/enum_extensions.rb +81 -0
  12. data/lib/hq/graphql/ext/input_object_extensions.rb +110 -0
  13. data/lib/hq/graphql/ext/mutation_extensions.rb +24 -0
  14. data/lib/hq/graphql/ext/object_extensions.rb +122 -0
  15. data/lib/hq/graphql/ext/schema_extensions.rb +50 -0
  16. data/lib/hq/graphql/field.rb +1 -1
  17. data/lib/hq/graphql/filter_operations.rb +59 -0
  18. data/lib/hq/graphql/filters.rb +150 -0
  19. data/lib/hq/graphql/paginated_association_loader.rb +1 -0
  20. data/lib/hq/graphql/record_loader.rb +1 -0
  21. data/lib/hq/graphql/resource.rb +86 -15
  22. data/lib/hq/graphql/resource/auto_mutation.rb +8 -5
  23. data/lib/hq/graphql/root_mutation.rb +1 -1
  24. data/lib/hq/graphql/root_query.rb +3 -3
  25. data/lib/hq/graphql/scalars.rb +0 -2
  26. data/lib/hq/graphql/types.rb +1 -2
  27. data/lib/hq/graphql/types/uuid.rb +3 -5
  28. data/lib/hq/graphql/util.rb +13 -0
  29. data/lib/hq/graphql/version.rb +1 -1
  30. metadata +28 -38
  31. data/lib/hq/graphql/active_record_extensions.rb +0 -143
  32. data/lib/hq/graphql/enum.rb +0 -78
  33. data/lib/hq/graphql/input_object.rb +0 -104
  34. data/lib/hq/graphql/mutation.rb +0 -15
  35. data/lib/hq/graphql/object.rb +0 -120
  36. data/lib/hq/graphql/schema.rb +0 -22
  37. data/lib/hq/graphql/types/object.rb +0 -35
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 34f3a14ee58de081c8d52088ebc7986cbc2ccf597392015bcb44aa629aef6556
4
- data.tar.gz: 39a436fca18cae2f81e672e78ae3118c4893512781a594c24d086cbf0f7e00c1
3
+ metadata.gz: ad646bb38a0bd0920ca49428245d5183377719ef6b4802a0a619dc5c8bdbbcdf
4
+ data.tar.gz: 53f4c4b248e1fe424d34eb37e86002a18e6a39a54c91141f939d6aa9d85438db
5
5
  SHA512:
6
- metadata.gz: '09606ace844e47c5c414a73d1f3b2ffd7a7183b173090ade1e6287bc42aa6cc84eed691dd3d3a2b8d7ad4f8045779550f619fab017ed0dd79c49621c007ad9d1'
7
- data.tar.gz: d8a07ccf03a311b807c7669202ce54d64e89db640f15911f2a1c6ddc3237d9fa94410e8527ae70539659a86ca06cf89317409b00d98b589056b9d6db5ae80dfc
6
+ metadata.gz: c5978e1805f3097aaf708f667fbfb669b74205edc8c4f73f7e859f768636a41e6fd72de00dc62322e90a6e127850467b7bf0a9ebd82507f50b197625259cf7cd
7
+ data.tar.gz: fc94b8611372d29218b621e762ccbfac2de279a5b16276eeaf8043a8f9f19a03539ceb90c9270fe22b306b4a3191c7ab7e8e50bcf6d989d512ff3db94e23edd1
data/README.md CHANGED
@@ -8,6 +8,7 @@ OneHQ GraphQL interface to [Ruby Graphql](https://github.com/rmosolgo/graphql-ru
8
8
  ## Configuration
9
9
 
10
10
  ### Default Scope
11
+
11
12
  Define a global default scope.
12
13
 
13
14
  ```ruby
@@ -19,6 +20,7 @@ end
19
20
  ```
20
21
 
21
22
  ### Excluded Inputs
23
+
22
24
  Define a global excluded input fields. Useful for excluding (autogenerated | auto set) fields like below.
23
25
 
24
26
  ```ruby
@@ -28,9 +30,11 @@ end
28
30
  ```
29
31
 
30
32
  ## GraphQL Resource
33
+
31
34
  Connect to ActiveRecord to auto generate queries and mutations.
32
35
 
33
36
  ### Queries
37
+
34
38
  Include `::HQ::GraphQL::Resource` and set `self.model_name` to start using queries.
35
39
  Fields will be created for all active record columns. Association fields will be created if the association
36
40
  is also a GraphQL Resource.
@@ -45,6 +49,7 @@ end
45
49
  ```
46
50
 
47
51
  #### Customize
52
+
48
53
  ```ruby
49
54
  class AdvisorResource
50
55
  include ::HQ::GraphQL::Resource
@@ -66,6 +71,7 @@ end
66
71
  ```
67
72
 
68
73
  ### Mutations
74
+
69
75
  Mutations will not be created by default. Add `mutations` to a resource to build mutations for create, update, and destroy.
70
76
 
71
77
  ```ruby
@@ -88,34 +94,40 @@ end
88
94
  ```
89
95
 
90
96
  ### Enums
97
+
91
98
  Auto generate enums from the database using ActiveRecord
92
99
  This comes in handy when we have constants that we want represented as enums.
93
100
 
94
101
  #### Example
102
+
95
103
  Let's assume we're saving data into a user types table
104
+
96
105
  ```postgresl
97
106
  # select * from user_types;
98
- id | name
107
+ id | name
99
108
  --- +-------------
100
- 1 | Admin
109
+ 1 | Admin
101
110
  2 | Support User
102
111
  (2 rows)
103
112
  ```
104
113
 
105
114
  ```ruby
106
- class Enums::UserType < ::HQ::GraphQL::Enum
115
+ class Enums::UserType < ::GraphQL::Schema::Enum
107
116
  with_model
108
117
  end
109
118
  ```
119
+
110
120
  This class automatically uses the UserType ActiveRecord model to generate an enum:
121
+
111
122
  ```graphql
112
- enum UserType {
113
- Admin
114
- SupportUser
115
- }
123
+ enum UserType {
124
+ Admin
125
+ SupportUser
126
+ }
116
127
  ```
117
128
 
118
129
  ### Root Mutations
130
+
119
131
  Add mutations to your schema
120
132
 
121
133
  ```ruby
@@ -127,7 +139,9 @@ end
127
139
  ```
128
140
 
129
141
  ### Default Root Queries
142
+
130
143
  Create a root query:
144
+
131
145
  ```ruby
132
146
  class AdvisorResource
133
147
  include ::HQ::GraphQL::Resource
@@ -144,6 +158,7 @@ end
144
158
  ```
145
159
 
146
160
  ### Custom Root Queries
161
+
147
162
  ```ruby
148
163
  class AdvisorResource
149
164
  include ::HQ::GraphQL::Resource
@@ -163,9 +178,10 @@ class AdvisorResource
163
178
  end
164
179
  ```
165
180
 
166
- ## Create a new ::HQ::GraphQL::Object
181
+ ## Create a new ::GraphQL::Schema::Object
182
+
167
183
  ```ruby
168
- class AdvisorType < ::HQ::GraphQL::Object
184
+ class AdvisorType < ::GraphQL::Schema::Object
169
185
  # Supports graphql-ruby functionality
170
186
  field :id, Int, null: false
171
187
 
data/lib/hq/graphql.rb CHANGED
@@ -51,6 +51,7 @@ module HQ
51
51
  end
52
52
 
53
53
  def self.reset!
54
+ @lazy_load_classes = nil
54
55
  @root_queries = nil
55
56
  @enums = nil
56
57
  @resources = nil
@@ -58,16 +59,28 @@ module HQ
58
59
  ::HQ::GraphQL::Types.reset!
59
60
  end
60
61
 
62
+ def self.load_types!
63
+ lazy_load_classes.pop.lazy_load! while lazy_load_classes.length > 0
64
+ end
65
+
66
+ def self.lazy_load(klass)
67
+ lazy_load_classes << klass unless lazy_load_classes.include?(klass)
68
+ end
69
+
70
+ def self.lazy_load_classes
71
+ @lazy_load_classes ||= []
72
+ end
73
+
61
74
  def self.root_queries
62
- @root_queries ||= Set.new
75
+ @root_queries ||= []
63
76
  end
64
77
 
65
78
  def self.enums
66
- @enums ||= Set.new
79
+ @enums ||= []
67
80
  end
68
81
 
69
82
  def self.resources
70
- @resources ||= Set.new
83
+ @resources ||= []
71
84
  end
72
85
  end
73
86
  end
@@ -75,16 +88,12 @@ end
75
88
  require "hq/graphql/association_loader"
76
89
  require "hq/graphql/scalars"
77
90
  require "hq/graphql/comparator"
78
- require "hq/graphql/enum"
91
+ require "hq/graphql/ext"
79
92
  require "hq/graphql/inputs"
80
- require "hq/graphql/input_object"
81
- require "hq/graphql/mutation"
82
- require "hq/graphql/object"
83
93
  require "hq/graphql/paginated_association_loader"
84
94
  require "hq/graphql/record_loader"
85
95
  require "hq/graphql/resource"
86
96
  require "hq/graphql/root_mutation"
87
97
  require "hq/graphql/root_query"
88
- require "hq/graphql/schema"
89
98
  require "hq/graphql/types"
90
99
  require "hq/graphql/engine"
@@ -4,6 +4,7 @@ module HQ
4
4
  module GraphQL
5
5
  class AssociationLoader < ::GraphQL::Batch::Loader
6
6
  def initialize(model, association_name)
7
+ super()
7
8
  @model = model
8
9
  @association_name = association_name
9
10
  validate
@@ -13,31 +13,32 @@ module HQ
13
13
  non_breaking: 2
14
14
  }
15
15
 
16
- class << self
17
- def compare(old_schema, new_schema, criticality: :breaking)
18
- level = CRITICALITY[criticality]
19
- raise ::ArgumentError, "Invalid criticality. Possible values are #{CRITICALITY.keys.join(", ")}" unless level
20
-
21
- result = ::GraphQL::SchemaComparator.compare(convert_schema_to_string(old_schema), convert_schema_to_string(new_schema))
22
- return nil if result.identical?
23
-
24
- changes = {}
25
- changes[:breaking] = result.breaking_changes
26
- if level >= CRITICALITY[:dangerous]
27
- changes[:dangerous] = result.dangerous_changes
28
- end
29
- if level >= CRITICALITY[:non_breaking]
30
- changes[:non_breaking] = result.non_breaking_changes
31
- end
32
- return nil unless changes.values.flatten.any?
33
-
34
- changes
16
+ def self.compare(old_schema, new_schema, criticality: :breaking)
17
+ level = CRITICALITY[criticality]
18
+ raise ::ArgumentError, "Invalid criticality. Possible values are #{CRITICALITY.keys.join(", ")}" unless level
19
+
20
+ result = ::GraphQL::SchemaComparator.compare(prepare_schema(old_schema), prepare_schema(new_schema))
21
+ return if result.identical?
22
+ changes = {}
23
+ changes[:breaking] = result.breaking_changes
24
+ if level >= CRITICALITY[:dangerous]
25
+ changes[:dangerous] = result.dangerous_changes
35
26
  end
27
+ if level >= CRITICALITY[:non_breaking]
28
+ changes[:non_breaking] = result.non_breaking_changes
29
+ end
30
+ return unless changes.values.flatten.any?
31
+
32
+ changes
33
+ end
36
34
 
35
+ class << self
37
36
  private
38
37
 
39
- def convert_schema_to_string(schema)
40
- schema.is_a?(::String) ? schema : schema.to_definition
38
+ def prepare_schema(schema)
39
+ schema = ::GraphQL::Schema.from_definition(schema) if schema.is_a?(String)
40
+ schema.load_types!
41
+ schema
41
42
  end
42
43
  end
43
44
  end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "hq/graphql/filter_operations"
4
+
5
+ module HQ
6
+ module GraphQL
7
+ module Enum
8
+ class FilterOperation < ::GraphQL::Schema::Enum
9
+ ::HQ::GraphQL::FilterOperations::OPERATIONS.each do |filter_operation|
10
+ value filter_operation.name, value: filter_operation
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
@@ -1,10 +1,14 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "hq/graphql/enum"
3
+ require "hq/graphql/ext/enum_extensions"
4
4
 
5
5
  module HQ
6
- class GraphQL::Enum::SortBy < ::HQ::GraphQL::Enum
7
- value "CreatedAt", value: :created_at
8
- value "UpdatedAt", value: :updated_at
6
+ module GraphQL
7
+ module Enum
8
+ class SortBy < ::GraphQL::Schema::Enum
9
+ value "CreatedAt", value: :created_at
10
+ value "UpdatedAt", value: :updated_at
11
+ end
12
+ end
9
13
  end
10
14
  end
@@ -1,10 +1,14 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "hq/graphql/enum"
3
+ require "hq/graphql/ext/enum_extensions"
4
4
 
5
5
  module HQ
6
- class GraphQL::Enum::SortOrder < ::HQ::GraphQL::Enum
7
- value "ASC", value: :asc
8
- value "DESC", value: :desc
6
+ module GraphQL
7
+ module Enum
8
+ class SortOrder < ::GraphQL::Schema::Enum
9
+ value "ASC", value: :asc
10
+ value "DESC", value: :desc
11
+ end
12
+ end
9
13
  end
10
14
  end
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "hq/graphql/ext/active_record_extensions"
4
+ require "hq/graphql/ext/enum_extensions"
5
+ require "hq/graphql/ext/input_object_extensions"
6
+ require "hq/graphql/ext/mutation_extensions"
7
+ require "hq/graphql/ext/object_extensions"
8
+ require "hq/graphql/ext/schema_extensions"
@@ -0,0 +1,148 @@
1
+ # frozen_string_literal: true
2
+
3
+ module HQ
4
+ module GraphQL
5
+ module Ext
6
+ module ActiveRecordExtensions
7
+ class ActiveRecordError < StandardError
8
+ MISSING_MODEL_MSG = "Your GraphQL object must be connected to a model: `::GraphQL::Schema::Object.with_model 'User'`"
9
+ MISSING_ATTR_MSG = "Can't find attr %{model}.%{attr}'`"
10
+ MISSING_ASSOC_MSG = "Can't find association %{model}.%{assoc}'`"
11
+ end
12
+
13
+ def self.included(klass)
14
+ klass.extend(ClassMethods)
15
+ end
16
+
17
+ module ClassMethods
18
+ attr_accessor :model_name,
19
+ :authorize_action,
20
+ :auto_load_attributes,
21
+ :auto_load_associations,
22
+ :auto_load_enums
23
+
24
+ def lazy_load(&block)
25
+ @lazy_load ||= []
26
+ if block
27
+ ::HQ::GraphQL.lazy_load(self)
28
+ @lazy_load << block
29
+ end
30
+ @lazy_load
31
+ end
32
+
33
+ def lazy_load!
34
+ lazy_load.shift.call while lazy_load.length > 0
35
+ @lazy_load = nil
36
+ end
37
+
38
+ def model_columns
39
+ model_columns =
40
+ if auto_load_attributes
41
+ model_klass.columns
42
+ else
43
+ added_attributes.map { |attr| column_from_model(attr) }
44
+ end
45
+
46
+ # validate removed_attributes exist
47
+ removed_attributes.each { |attr| column_from_model(attr) }
48
+
49
+ model_columns.reject { |c| removed_attributes.include?(c.name.to_sym) }.sort_by(&:name)
50
+ end
51
+
52
+ def model_associations
53
+ model_associations = []
54
+ enums = model_klass.reflect_on_all_associations.select { |a| is_enum?(a) }
55
+ associatons = model_klass.reflect_on_all_associations - enums
56
+
57
+ if auto_load_enums
58
+ model_associations.concat(enums)
59
+ end
60
+
61
+ if auto_load_associations
62
+ model_associations.concat(associatons)
63
+ end
64
+
65
+ model_associations.concat(added_associations.map { |association| association_from_model(association) }).uniq
66
+
67
+ # validate removed_associations exist
68
+ removed_associations.each { |association| association_from_model(association) }
69
+
70
+ model_associations.reject { |a| removed_associations.include?(a.name.to_sym) }.sort_by(&:name)
71
+ end
72
+
73
+ private
74
+
75
+ def add_attributes(*attrs)
76
+ validate_model!
77
+ added_attributes.concat attrs.map(&:to_sym)
78
+ end
79
+ alias_method :add_attribute, :add_attributes
80
+ alias_method :add_attrs, :add_attributes
81
+ alias_method :add_attr, :add_attributes
82
+
83
+ def remove_attributes(*attrs)
84
+ validate_model!
85
+ removed_attributes.concat attrs.map(&:to_sym)
86
+ end
87
+ alias_method :remove_attribute, :remove_attributes
88
+ alias_method :remove_attrs, :remove_attributes
89
+ alias_method :remove_attr, :remove_attributes
90
+
91
+ def add_associations(*associations)
92
+ validate_model!
93
+ added_associations.concat associations.map(&:to_sym)
94
+ end
95
+ alias_method :add_association, :add_associations
96
+
97
+ def remove_associations(*associations)
98
+ validate_model!
99
+ removed_associations.concat associations.map(&:to_sym)
100
+ end
101
+ alias_method :remove_association, :remove_associations
102
+
103
+ def model_klass
104
+ @model_klass ||= model_name.constantize
105
+ end
106
+
107
+ def column_from_model(attr)
108
+ model_klass.columns_hash[attr.to_s] || raise(ActiveRecordError, ActiveRecordError::MISSING_ATTR_MSG % { model: model_name, attr: attr })
109
+ end
110
+
111
+ def association_from_model(association)
112
+ model_klass.reflect_on_association(association) || raise(ActiveRecordError, ActiveRecordError::MISSING_ASSOC_MSG % { model: model_name, assoc: association })
113
+ end
114
+
115
+ def added_attributes
116
+ @added_attributes ||= []
117
+ end
118
+
119
+ def removed_attributes
120
+ @removed_attributes ||= []
121
+ end
122
+
123
+ def added_associations
124
+ @added_associations ||= []
125
+ end
126
+
127
+ def removed_associations
128
+ @removed_associations ||= []
129
+ end
130
+
131
+ def camelize(name)
132
+ name.to_s.camelize(:lower)
133
+ end
134
+
135
+ def is_enum?(association)
136
+ ::HQ::GraphQL.enums.include?(association.klass)
137
+ end
138
+
139
+ def validate_model!
140
+ lazy_load do
141
+ model_name || raise(ActiveRecordError, ActiveRecordError::MISSING_MODEL_MSG)
142
+ end
143
+ end
144
+ end
145
+ end
146
+ end
147
+ end
148
+ end