graphql-activerecord 0.4.3 → 0.5.0
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
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e776f9bc85c5d4a191d6fb030fe41b2de2a7a52e
|
4
|
+
data.tar.gz: 55a459231c7e67dfbec908637446952f41af703f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b64aa499d1d6773cbb81c333ed76ecc5bbd559793ae33e0eeeb49d002e372399e6270205b8aa62e2bee575c193073ad3f5560712ee62f873049c1532314a0ecb
|
7
|
+
data.tar.gz: 55f8c84efa13e1dd6233490ff44e90c970816d7fa9325c80fa64cfe7c82943d2b5ab32bc4d827a5038ab0e12510e619694f7b21a7e7235e44fede56207b982f6
|
@@ -2,58 +2,43 @@ module GraphQL
|
|
2
2
|
module Models
|
3
3
|
module ActiveRecordExtension
|
4
4
|
extend ActiveSupport::Concern
|
5
|
-
|
6
|
-
# Default options for graphql_enums
|
7
|
-
ENUM_OPTIONS = {
|
8
|
-
upcase: true
|
9
|
-
}
|
10
|
-
|
11
5
|
class_methods do
|
12
6
|
def graphql_enum_types
|
13
7
|
@_graphql_enum_types ||= {}.with_indifferent_access
|
14
8
|
end
|
15
9
|
|
16
10
|
# Defines a GraphQL enum type on the model
|
17
|
-
def graphql_enum(attribute,
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
if !options.include?(:values) && !options.include?(:type)
|
23
|
-
if defined_enums.include?(attribute.to_s)
|
24
|
-
options[:values] = defined_enums[attribute.to_s].keys.map { |ev| [options[:upcase] ? ev.upcase : ev, ev.titleize] }.to_h
|
25
|
-
else
|
26
|
-
fail ArgumentError.new("Could not auto-detect the values for enum #{attribute} on #{self.name}")
|
27
|
-
end
|
11
|
+
def graphql_enum(attribute, type: nil, upcase: true)
|
12
|
+
# Case 1: Providing a type. Only thing to do is register the enum type.
|
13
|
+
if type
|
14
|
+
graphql_enum_types[attribute] = type
|
15
|
+
return type
|
28
16
|
end
|
29
17
|
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
name options[:name]
|
34
|
-
description options[:description]
|
18
|
+
# Case 2: Automatically generating the type
|
19
|
+
name = "#{self.name}#{attribute.to_s.classify}"
|
20
|
+
description = "#{attribute.to_s.titleize} field on #{self.name.titleize}"
|
35
21
|
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
22
|
+
if defined_enums.include?(attribute.to_s)
|
23
|
+
values = defined_enums[attribute.to_s].keys
|
24
|
+
else
|
25
|
+
values = DefinitionHelpers.detect_inclusion_values(self, attribute)
|
26
|
+
end
|
40
27
|
|
41
|
-
|
28
|
+
if values.nil?
|
29
|
+
fail ArgumentError.new("Could not auto-detect the values for enum #{attribute} on #{self.name}")
|
42
30
|
end
|
43
31
|
|
44
|
-
|
45
|
-
|
46
|
-
|
32
|
+
type = GraphQL::EnumType.define do
|
33
|
+
name(name)
|
34
|
+
description(description)
|
47
35
|
|
48
|
-
|
49
|
-
|
50
|
-
|
36
|
+
values.each do |val|
|
37
|
+
value(upcase ? val.upcase : val, val.to_s.titleize, value: val)
|
38
|
+
end
|
39
|
+
end
|
51
40
|
|
52
|
-
|
53
|
-
# on a GraphQL type.
|
54
|
-
def graphql_resolve(attribute, &block)
|
55
|
-
fail ArgumentError.new("#{__method__} requires a block") unless block_given?
|
56
|
-
graphql_resolvers[attribute] = block
|
41
|
+
graphql_enum_types[attribute] = type
|
57
42
|
end
|
58
43
|
end
|
59
44
|
end
|
@@ -95,12 +95,7 @@ module GraphQL
|
|
95
95
|
if column.is_range
|
96
96
|
DefinitionHelpers.range_to_graphql(model.public_send(attribute))
|
97
97
|
else
|
98
|
-
|
99
|
-
resolve_proc = model_type.graphql_resolvers[attribute]
|
100
|
-
model.instance_exec(&resolve_proc)
|
101
|
-
else
|
102
|
-
model.public_send(attribute)
|
103
|
-
end
|
98
|
+
model.public_send(attribute)
|
104
99
|
end
|
105
100
|
end
|
106
101
|
end
|