Class: HasHelpers::Resolvers::Cards::CardData

Inherits:
GraphQL::Schema::Resolver
  • Object
show all
Defined in:
app/graphql/has_helpers/resolvers/cards/card_data.rb

Overview

Resolves all cards (definitions and data) for a given role/resource_name/record_id. API:

cardData(
roleId: ID,
resourceName: String!,
recordId: ID!
): [Cards::CardDataType!]!

For each CardDefinition linked to the given resource, this resolver calls HasHelpers::Cards::CardDataCommand and returns an array of card payloads.

Instance Method Summary collapse

Instance Method Details

#resolve(role_id: nil, resource_name:, record_id:) ⇒ Object

Raises:

  • (GraphQL::ExecutionError)


23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'app/graphql/has_helpers/resolvers/cards/card_data.rb', line 23

def resolve(role_id: nil, resource_name:, record_id:)
  app = context[:current_application]
  raise GraphQL::ExecutionError, "current_application missing from GraphQL context" if app.nil?

  resource = ::HasHelpers::Resource.find_by!(
    name: resource_name.to_s,
    application_id: app.id,
    parent_id: nil
  )

  # Load the main record once; it will be reused for all cards
  record = resource.klass.find(record_id)

  # Load all card_definitions for this resource and eager-load
  # the associations needed by the command to avoid N+1.
  card_definitions =
    ::HasHelpers::Cards::CardDefinition.
      joins(:card_definition_resources).
      where(card_definition_resources: { resource_id: resource.id }).
      ordered.
      includes(
        :identifier,
        :layout_type,
        :variant,
        card_fields: %i[field_resource data_type],
        role_resource_cards: [:role_card_fields],
        card_definition_resources: { resource: :application }
      ).
      distinct

  card_definitions.map do |card_definition|
    result = ::HasHelpers::Cards::CardDataCommand.call_with_record(
      role_id: role_id,
      # We pass the Resource and Identifier objects directly. The command
      # supports both ids and instances via find_resource/find_identifier.
      resource_id: resource,
      identifier_id: card_definition.identifier,
      record: record
    )

    {
      application: app.name,
      card: result.card,
      fields: result.fields,
      entries: result.entries
    }
  end
end