Commit 230d0e3b authored by Sean McGivern's avatar Sean McGivern

Merge branch 'make_graphql_enums_slightly_nicer' into 'master'

Make GraphQL enums a little bit nicer

See merge request gitlab-org/gitlab!19998
parents c7a1d498 2690332c
......@@ -2,5 +2,18 @@
module Types
class BaseEnum < GraphQL::Schema::Enum
class << self
def value(*args, **kwargs, &block)
enum[args[0].downcase] = kwargs[:value] || args[0]
super(*args, **kwargs, &block)
end
# Returns an indifferent access hash with the key being the downcased name of the attribute
# and the value being the Ruby value (either the explicit `value` passed or the same as the value attr).
def enum
@enum_values ||= {}.with_indifferent_access
end
end
end
end
# frozen_string_literal: true
require 'spec_helper'
describe Types::BaseEnum do
describe '#enum' do
let(:enum) do
Class.new(described_class) do
value 'TEST', value: 3
value 'other'
value 'NORMAL'
end
end
it 'adds all enum values to #enum' do
expect(enum.enum.keys).to contain_exactly('test', 'other', 'normal')
expect(enum.enum.values).to contain_exactly(3, 'other', 'NORMAL')
end
it 'is a HashWithIndefferentAccess' do
expect(enum.enum).to be_a(HashWithIndifferentAccess)
end
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment