Class: Types::WorkItemIdType

Inherits:
GlobalIDType show all
Defined in:
app/graphql/types/work_item_id_type.rb

Overview

rubocop:disable Graphql/AuthorizeTypes TODO: This type should be removed when Work Items become generally available. This mechanism is introduced temporarily to make the client implementation easier during this transition.

Class Method Summary collapse

Methods inherited from GlobalIDType

[], model_name_to_graphql_name

Class Method Details

.coerce_input(string, ctx) ⇒ Object

Raises:

  • (GraphQL::CoercionError)

28
29
30
31
32
33
34
# File 'app/graphql/types/work_item_id_type.rb', line 28

def coerce_input(string, ctx)
  gid = super
  # Always return a WorkItemID even if an Issue Global ID is provided as input
  return work_item_gid(gid) if suitable?(gid)

  raise GraphQL::CoercionError, "#{string.inspect} does not represent an instance of WorkItem"
end

.coerce_result(gid, ctx) ⇒ Object

Raises:

  • (GraphQL::CoercionError)

19
20
21
22
23
24
25
26
# File 'app/graphql/types/work_item_id_type.rb', line 19

def coerce_result(gid, ctx)
  global_id = ::Gitlab::GlobalId.as_global_id(gid, model_name: 'WorkItem')

  raise GraphQL::CoercionError, "Expected a WorkItem ID, got #{global_id}" unless suitable?(global_id)

  # Always return a WorkItemID even if an Issue is returned by a resolver
  work_item_gid(global_id).to_s
end

.suitable?(gid) ⇒ Boolean

Returns:

  • (Boolean)

36
37
38
39
40
# File 'app/graphql/types/work_item_id_type.rb', line 36

def suitable?(gid)
  return false if gid&.model_name&.safe_constantize.blank?

  [::WorkItem, ::Issue].any? { |model_class| gid.model_class == model_class }
end