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
@@ -1,15 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module HQ
4
- module GraphQL
5
- class Mutation < ::GraphQL::Schema::Mutation
6
- include Scalars
7
- include ::HQ::GraphQL::ActiveRecordExtensions
8
-
9
- def self.generate_payload_type
10
- lazy_load!
11
- super
12
- end
13
- end
14
- end
15
- end
@@ -1,120 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "hq/graphql/active_record_extensions"
4
- require "hq/graphql/field"
5
- require "hq/graphql/field_extension/association_loader_extension"
6
- require "hq/graphql/field_extension/paginated_arguments"
7
- require "hq/graphql/field_extension/paginated_loader"
8
- require "hq/graphql/object_association"
9
- require "hq/graphql/types"
10
-
11
- module HQ
12
- module GraphQL
13
- class Object < ::GraphQL::Schema::Object
14
- include Scalars
15
- include ActiveRecordExtensions
16
- extend ObjectAssociation
17
-
18
- field_class Field
19
-
20
- def self.authorize_action(action)
21
- self.authorized_action = action
22
- end
23
-
24
- def self.authorized?(object, context)
25
- super && ::HQ::GraphQL.authorized?(authorized_action, object, context)
26
- end
27
-
28
- def self.with_model(model_name, attributes: true, associations: true, auto_nil: true, enums: true)
29
- self.model_name = model_name
30
- self.auto_load_attributes = attributes
31
- self.auto_load_associations = associations
32
- self.auto_load_enums = enums
33
-
34
- lazy_load do
35
- model_columns.each do |column|
36
- field_from_column(column, auto_nil: auto_nil)
37
- end
38
-
39
- model_associations.each do |association|
40
- next if resource_reflections[association.name.to_s]
41
- field_from_association(association, auto_nil: auto_nil)
42
- end
43
-
44
- resource_reflections.values.each do |resource_reflection|
45
- reflection = resource_reflection.reflection(model_klass)
46
- next unless reflection
47
- field_from_association(reflection, auto_nil: auto_nil, internal_association: true, &resource_reflection.block)
48
- end
49
- end
50
- end
51
-
52
- def self.to_graphql
53
- lazy_load!
54
- super
55
- end
56
-
57
- class << self
58
- private
59
- attr_writer :authorized_action
60
-
61
- def authorized_action
62
- @authorized_action ||= :read
63
- end
64
-
65
- def field_from_association(association, auto_nil:, internal_association: false, &block)
66
- association_klass = association.klass
67
- name = association.name.to_s
68
- return if field_exists?(name)
69
-
70
- klass = model_klass
71
- type = Types[association_klass]
72
- case association.macro
73
- when :has_many
74
- field name, [type], null: false, klass: model_name do
75
- if ::HQ::GraphQL.use_experimental_associations?
76
- extension FieldExtension::PaginatedArguments, klass: association_klass
77
- extension FieldExtension::PaginatedLoader, klass: klass, association: name, internal_association: internal_association
78
- else
79
- extension FieldExtension::AssociationLoaderExtension, klass: klass
80
- end
81
- instance_eval(&block) if block
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
87
- else
88
- field name, type, null: !auto_nil || !association_required?(association), klass: model_name do
89
- extension FieldExtension::AssociationLoaderExtension, klass: klass
90
- end
91
- end
92
- rescue Types::Error
93
- nil
94
- end
95
-
96
- def field_from_column(column, auto_nil:)
97
- name = column.name
98
- return if field_exists?(name)
99
-
100
- field name, Types.type_from_column(column), null: !auto_nil || column.null
101
- end
102
-
103
- def field_exists?(name)
104
- !!fields[camelize(name)]
105
- end
106
-
107
- def association_required?(association)
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))
114
- validation.attributes.any? { |a| a.to_s == association.name.to_s }
115
- end
116
- end
117
- end
118
- end
119
- end
120
- end
@@ -1,22 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module HQ
4
- module GraphQL
5
- class Schema < ::GraphQL::Schema
6
- class << self
7
- def dump_directory(directory = Rails.root.join("app/graphql"))
8
- @dump_directory ||= directory
9
- end
10
-
11
- def dump_filename(filename = "#{self.name.underscore}.graphql")
12
- @dump_filename ||= filename
13
- end
14
-
15
- def dump
16
- ::FileUtils.mkdir_p(dump_directory)
17
- ::File.open(::File.join(dump_directory, dump_filename), "w") { |file| file.write(self.to_definition) }
18
- end
19
- end
20
- end
21
- end
22
- end
@@ -1,35 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module HQ
4
- module GraphQL
5
- module Types
6
- class Object < ::GraphQL::Schema::Scalar
7
- description "Object"
8
-
9
- def self.coerce_input(value, _context)
10
- validate_and_return_object(value)
11
- end
12
-
13
- def self.coerce_result(value, _context)
14
- validate_and_return_object(value)
15
- end
16
-
17
- class << self
18
- private
19
-
20
- def validate_and_return_object(value)
21
- if validate_object(value)
22
- value
23
- else
24
- raise ::GraphQL::CoercionError, "#{value.inspect} is not a valid Object"
25
- end
26
- end
27
-
28
- def validate_object(value)
29
- value.is_a?(Hash)
30
- end
31
- end
32
- end
33
- end
34
- end
35
- end