Skip to content

Commit

Permalink
Add integer_enum type
Browse files Browse the repository at this point in the history
  • Loading branch information
wadetandy committed Mar 27, 2019
1 parent 7ae1c2d commit a6db8a8
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 13 deletions.
9 changes: 5 additions & 4 deletions lib/graphiti/errors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -166,22 +166,23 @@ def message
end

class MissingEnumAllowList < Base
def initialize(resource_class, filter_name)
def initialize(resource_class, filter_name, enum_type)
@resource_class = resource_class
@filter_name = filter_name
@enum_type = enum_type
end

def message
<<-MSG
#{@resource_class.name} You declared an attribute or filter of type "enum" without providing a list of permitted values, which is required.
#{@resource_class.name} You declared an attribute or filter of type "#{@enum_type}" without providing a list of permitted values, which is required.
When declaring an attribute:
attribute :status, :string_enum, allow: ['published', 'draft']
attribute :status, :#{@enum_type}, allow: ['published', 'draft']
When declaring a filter:
filter :status, :string_enum, allow: ['published', 'draft'] do
filter :status, :#{@enum_type}, allow: ['published', 'draft'] do
# ...
end
MSG
Expand Down
2 changes: 1 addition & 1 deletion lib/graphiti/resource/dsl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def filter(name, *args, &blk)
opts[:single] = true
when :enum
if opts[:allow].blank?
raise Errors::MissingEnumAllowList.new(self, name)
raise Errors::MissingEnumAllowList.new(self, name, att[:type])
end
end

Expand Down
8 changes: 8 additions & 0 deletions lib/graphiti/types.rb
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,14 @@ def self.map
kind: "scalar",
description: "String enum type. Like a normal string, but only eq/!eq and case-sensitive. Limited to only the allowed values.",
},
integer_enum: {
canonical_name: :enum,
params: Dry::Types["coercible.integer"],
read: Dry::Types["coercible.integer"],
write: Dry::Types["coercible.integer"],
kind: "scalar",
description: "Integer enum type. Like a normal integer, but only eq/!eq filters. Limited to only the allowed values.",
},
string: {
params: Dry::Types["coercible.string"],
read: Dry::Types["coercible.string"],
Expand Down
14 changes: 7 additions & 7 deletions spec/filtering_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -378,32 +378,32 @@ def self.name
context "when filtering on an enum field" do
context "when allowed values are provided" do
before do
resource.filter :enum_first_name, :string_enum, allow: ["William"] do
resource.filter :enum_age, :integer_enum, allow: [1, 3, 5] do
eq do |scope, value|
scope[:conditions][:first_name] = value
scope[:conditions][:age] = value
scope
end
end
end

it "rejects values not in the allowlist" do
params[:filter] = {enum_first_name: {eq: "Harold"}}
params[:filter] = {enum_age: {eq: 2}}
expect {
records
}.to raise_error(Graphiti::Errors::InvalidFilterValue, /Allowlist: \[\"William\"\]/)
}.to raise_error(Graphiti::Errors::InvalidFilterValue, /Allowlist: \[1, 3, 5]/)
end
end

context "when allow list is omitted" do
it "raises an error at load time" do
expect {
resource.filter :enum_first_name, :string_enum do
resource.filter :enum_age, :integer_enum do
eq do |scope, value|
scope[:conditions][:first_name] = value
scope[:conditions][:age] = value
scope
end
end
}.to raise_error(Graphiti::Errors::MissingEnumAllowList)
}.to raise_error(Graphiti::Errors::MissingEnumAllowList, /integer_enum/)
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion spec/resource_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -684,7 +684,7 @@ def apply_attribute
it "raises a helpful error" do
expect {
klass.attribute :foo, :string_enum
}.to raise_error(Graphiti::Errors::MissingEnumAllowList)
}.to raise_error(Graphiti::Errors::MissingEnumAllowList, /string_enum/)
end
end
end
Expand Down
8 changes: 8 additions & 0 deletions spec/schema_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@
description: "String enum type. Like a normal string, but only eq/!eq and case-sensitive. Limited to only the allowed values.",
kind: "scalar",
},
integer_enum: {
description: "Integer enum type. Like a normal integer, but only eq/!eq filters. Limited to only the allowed values.",
kind: "scalar"
},
integer: {
kind: "scalar",
description: "Base Type.",
Expand Down Expand Up @@ -137,6 +141,10 @@
description: "Base Type.",
kind: "array",
},
array_of_integer_enums: {
description: "Base Type.",
kind: "array",
},
array_of_integers: {
kind: "array",
description: "Base Type.",
Expand Down

0 comments on commit a6db8a8

Please # to comment.