graphql-activerecord 0.4.3 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: eb0a5e6a7aede364e37bcc2998d14080ae9f4eb4
4
- data.tar.gz: a6d3c703939a2dab438abde64510c176d41504ce
3
+ metadata.gz: e776f9bc85c5d4a191d6fb030fe41b2de2a7a52e
4
+ data.tar.gz: 55a459231c7e67dfbec908637446952f41af703f
5
5
  SHA512:
6
- metadata.gz: 1b29432db0e4a92b1c286e59f8e447d6b01e20ac2969b1bbd117307cbe07dd2d7561ca99f4f7c076204c97d2b59dea3d0f7339f0fb71e34a4d30253442acfd47
7
- data.tar.gz: 4eb8896e974750dcd1522b259e0ae885a67aece1a6aa3e17d2e4ae52457009d8d19276a2dbda0dee38acc0878e3c0dcc3d7206624f61816d009e0c1af5676b41
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, **options)
18
- options = ENUM_OPTIONS.merge(options)
19
- options[:name] ||= "#{self.name}#{attribute.to_s.classify}"
20
- options[:description] ||= "#{attribute.to_s.titleize} field on #{self.name.titleize}"
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
- enum_type = graphql_enum_types[attribute]
31
- unless enum_type
32
- enum_type = options[:type] || GraphQL::EnumType.define do
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
- options[:values].each do |value_name, desc|
37
- value(value_name, desc)
38
- end
39
- end
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
- graphql_enum_types[attribute] = enum_type
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
- graphql_resolve(attribute) { send(attribute).try(:upcase) } if options[:upcase]
45
- enum_type
46
- end
32
+ type = GraphQL::EnumType.define do
33
+ name(name)
34
+ description(description)
47
35
 
48
- def graphql_resolvers
49
- @_graphql_resolvers ||= {}.with_indifferent_access
50
- end
36
+ values.each do |val|
37
+ value(upcase ? val.upcase : val, val.to_s.titleize, value: val)
38
+ end
39
+ end
51
40
 
52
- # Defines a custom method that is used to resolve this attribute's value when it is included
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
- if model_type.graphql_resolvers.include?(attribute)
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
@@ -1,5 +1,5 @@
1
1
  module GraphQL
2
2
  module Models
3
- VERSION = "0.4.3"
3
+ VERSION = "0.5.0"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: graphql-activerecord
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.3
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ryan Foster