Class: Types::BaseEnum

Inherits:
GraphQL::Schema::Enum
  • Object
show all
Defined in:
app/graphql/types/base_enum.rb

Direct Known Subclasses

AccessLevelEnum, Admin::Analytics::UsageTrends::MeasurementIdentifierEnum, AlertManagement::DomainFilterEnum, AlertManagement::IntegrationTypeEnum, AlertManagement::SeverityEnum, AlertManagement::StatusEnum, AvailabilityEnum, Types::BlobViewers::TypeEnum, Types::Boards::AssigneeWildcardIdEnum, Ci::CodeQualityDegradationSeverityEnum, Ci::Config::IncludeTypeEnum, Ci::Config::StatusEnum, Ci::JobArtifactFileTypeEnum, Ci::JobKindEnum, Ci::JobStatusEnum, Ci::PipelineConfigSourceEnum, Ci::PipelineScopeEnum, Ci::PipelineStatusEnum, Ci::RunnerAccessLevelEnum, Ci::RunnerMembershipFilterEnum, Ci::RunnerSortEnum, Ci::RunnerStatusEnum, Ci::RunnerTypeEnum, Ci::RunnerUpgradeStatusTypeEnum, Ci::TestCaseStatusEnum, CiConfiguration::Sast::UiComponentSizeEnum, Clusters::AgentTokenStatusEnum, CommitActionModeEnum, CommitEncodingEnum, ContainerExpirationPolicyCadenceEnum, ContainerExpirationPolicyKeepEnum, ContainerExpirationPolicyOlderThanEnum, ContainerRepositoryCleanupStatusEnum, ContainerRepositoryStatusEnum, ContainerRepositoryTagsSortEnum, DataVisualizationPalette::ColorEnum, DataVisualizationPalette::WeightEnum, DeploymentTierEnum, DesignManagement::DesignCollectionCopyStateEnum, DesignManagement::DesignVersionEventEnum, ErrorTracking::SentryErrorStatusEnum, EventActionEnum, GroupMemberRelationEnum, IncidentManagement::EscalationStatusEnum, IssuableSearchableFieldEnum, IssuableSeverityEnum, IssuableStateEnum, IssueStateEventEnum, IssueTypeEnum, MergeRequestReviewStateEnum, MergeRequestStateEventEnum, MergeRequests::MergeStatusEnum, MergeStrategyEnum, MilestoneStateEnum, MilestoneWildcardIdEnum, MutationOperationModeEnum, NegatedMilestoneWildcardIdEnum, Notes::PositionTypeEnum, Packages::Conan::MetadatumFileTypeEnum, Packages::PackageDependencyTypeEnum, Packages::PackageSortEnum, Packages::PackageStatusEnum, Packages::PackageTypeEnum, PermissionTypes::GroupEnum, ProjectMemberRelationEnum, Projects::NamespaceProjectSortEnum, Projects::ServiceTypeEnum, ReleaseAssetLinkTypeEnum, ReleaseSortEnum, ReleaseTagWildcardIdEnum, Security::ReportTypeEnum, ShaFormatEnum, Snippets::BlobActionEnum, Snippets::TypeEnum, Snippets::VisibilityScopesEnum, SortEnum, TodoActionEnum, TodoStateEnum, TodoTargetEnum, Tree::TypeEnum, UserCalloutFeatureNameEnum, UserStateEnum, VisibilityLevelsEnum, WorkItemStateEnum, WorkItems::StateEventEnum

Defined Under Namespace

Classes: CustomValue

Class Method Summary collapse

Class Method Details

.authorizationObject


67
68
69
# File 'app/graphql/types/base_enum.rb', line 67

def authorization
  @authorization ||= ::Gitlab::Graphql::Authorize::ObjectAuthorization.new(authorize)
end

.authorize(*abilities) ⇒ Object


71
72
73
# File 'app/graphql/types/base_enum.rb', line 71

def authorize(*abilities)
  @abilities = abilities
end

.authorized?(object, context) ⇒ Boolean

Returns:

  • (Boolean)

75
76
77
# File 'app/graphql/types/base_enum.rb', line 75

def authorized?(object, context)
  authorization.ok?(object, context[:current_user])
end

.declarative_enum(enum_mod, use_name: true, use_description: true) ⇒ Object

Registers enum definition by the given DeclarativeEnum module

Example:

class MyEnum < BaseEnum
  declarative_enum MyDeclarativeEnum
end

Disabling descriptions rubocop for a false positive here rubocop: disable Graphql/Descriptions

Parameters:

  • enum_mod (Module)

    The enum module to be used

  • use_name (Boolean) (defaults to: true)

    Does not override the name if set `false`

  • use_description (Boolean) (defaults to: true)

    Does not override the description if set `false`


36
37
38
39
40
41
42
43
# File 'app/graphql/types/base_enum.rb', line 36

def declarative_enum(enum_mod, use_name: true, use_description: true)
  graphql_name(enum_mod.name) if use_name
  description(enum_mod.description) if use_description

  enum_mod.definition.each do |key, content|
    value(key.to_s.upcase, value: key.to_s, description: content[:description])
  end
end

.enumObject

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).


63
64
65
# File 'app/graphql/types/base_enum.rb', line 63

def enum
  @enum_values ||= {}.with_indifferent_access
end

.from_rails_enum(enum, description:) ⇒ Object

Helper to define an enum member for each element of a Rails AR enum


47
48
49
50
51
52
53
# File 'app/graphql/types/base_enum.rb', line 47

def from_rails_enum(enum, description:)
  enum.each_key do |name|
    value name.to_s.upcase,
          value: name,
          description: format(description, name: name)
  end
end

.value(*args, **kwargs, &block) ⇒ Object


55
56
57
58
59
# File 'app/graphql/types/base_enum.rb', line 55

def value(*args, **kwargs, &block)
  enum[args[0].downcase] = kwargs[:value] || args[0]

  super(*args, **kwargs, &block)
end