hq-graphql 2.0.6 → 2.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,4 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- # typed: strict
4
- require "hq/graphql/loaders/association"
@@ -1,52 +0,0 @@
1
- # typed: false
2
- # frozen_string_literal: true
3
-
4
- module HQ
5
- module GraphQL
6
- module Loaders
7
- class Association < ::GraphQL::Batch::Loader
8
- def initialize(model, association_name)
9
- @model = model
10
- @association_name = association_name
11
- validate
12
- end
13
-
14
- def load(record)
15
- raise TypeError, "#{@model} loader can't load association for #{record.class}" unless record.is_a?(@model)
16
- return Promise.resolve(read_association(record)) if association_loaded?(record)
17
- super
18
- end
19
-
20
- # We want to load the associations on all records, even if they have the same id
21
- def cache_key(record)
22
- record.object_id
23
- end
24
-
25
- def perform(records)
26
- preload_association(records)
27
- records.each { |record| fulfill(record, read_association(record)) }
28
- end
29
-
30
- private
31
-
32
- def validate
33
- unless @model.reflect_on_association(@association_name)
34
- raise ArgumentError, "No association #{@association_name} on #{@model}"
35
- end
36
- end
37
-
38
- def preload_association(records)
39
- ::ActiveRecord::Associations::Preloader.new.preload(records, @association_name)
40
- end
41
-
42
- def read_association(record)
43
- record.public_send(@association_name)
44
- end
45
-
46
- def association_loaded?(record)
47
- record.association(@association_name).loaded?
48
- end
49
- end
50
- end
51
- end
52
- end
@@ -1,39 +0,0 @@
1
- # typed: false
2
- # frozen_string_literal: true
3
-
4
- module HQ
5
- module GraphQL
6
- module Resource
7
- module Mutation
8
- def self.build(model_name, action:, graphql_name:, require_primary_key: false, nil_klass: false, &block)
9
- Class.new(::HQ::GraphQL::Mutation) do
10
- graphql_name graphql_name
11
-
12
- define_method(:ready?) do |*args|
13
- super(*args) && ::HQ::GraphQL.authorized?(action, model_name, context)
14
- end
15
-
16
- lazy_load do
17
- field :errors, ::HQ::GraphQL::Types::Object, null: false
18
- field :resource, ::HQ::GraphQL::Types[model_name, nil_klass], null: true
19
- end
20
-
21
- instance_eval(&block)
22
-
23
- if require_primary_key
24
- lazy_load do
25
- klass = model_name.constantize
26
- primary_key = klass.primary_key
27
- argument primary_key, ::GraphQL::Types::ID, required: true
28
- end
29
- end
30
-
31
- def errors_from_resource(resource)
32
- resource.errors.to_h.deep_transform_keys { |k| k.to_s.camelize(:lower) }
33
- end
34
- end
35
- end
36
- end
37
- end
38
- end
39
- end