Class: GraphQL::Stitching::Composer::TypeResolverConfig

Inherits:
Object
  • Object
show all
Defined in:
lib/graphql/stitching/composer/type_resolver_config.rb

Constant Summary collapse

ENTITY_TYPENAME =
"_Entity"
ENTITIES_QUERY =
"_entities"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key:, type_name:, arguments: nil) ⇒ TypeResolverConfig

Returns a new instance of TypeResolverConfig.



71
72
73
74
75
# File 'lib/graphql/stitching/composer/type_resolver_config.rb', line 71

def initialize(key:, type_name:, arguments: nil)
  @key = key
  @type_name = type_name
  @arguments = arguments
end

Instance Attribute Details

#argumentsObject (readonly)

Returns the value of attribute arguments.



69
70
71
# File 'lib/graphql/stitching/composer/type_resolver_config.rb', line 69

def arguments
  @arguments
end

#keyObject (readonly)

Returns the value of attribute key.



69
70
71
# File 'lib/graphql/stitching/composer/type_resolver_config.rb', line 69

def key
  @key
end

#type_nameObject (readonly)

Returns the value of attribute type_name.



69
70
71
# File 'lib/graphql/stitching/composer/type_resolver_config.rb', line 69

def type_name
  @type_name
end

Class Method Details

.extract_directive_assignments(schema, location, assignments) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/graphql/stitching/composer/type_resolver_config.rb', line 10

def extract_directive_assignments(schema, location, assignments)
  return EMPTY_OBJECT unless assignments && assignments.any?

  assignments.each_with_object({}) do |kwargs, memo|
    type = kwargs[:parent_type_name] ? schema.get_type(kwargs[:parent_type_name]) : schema.query
    raise CompositionError, "Invalid stitch directive type `#{kwargs[:parent_type_name]}`" unless type

    field = type.get_field(kwargs[:field_name])
    raise CompositionError, "Invalid stitch directive field `#{kwargs[:field_name]}`" unless field

    field_path = "#{location}.#{field.name}"
    memo[field_path] ||= []
    memo[field_path] << from_kwargs(kwargs)
  end
end

.extract_federation_entities(schema, location) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/graphql/stitching/composer/type_resolver_config.rb', line 26

def extract_federation_entities(schema, location)
  return EMPTY_OBJECT unless federation_entities_schema?(schema)

  schema.possible_types(schema.get_type(ENTITY_TYPENAME)).each_with_object({}) do |entity_type, memo|
    entity_type.directives.each do |directive|
      next unless directive.graphql_name == "key"

      key = TypeResolver.parse_key(directive.arguments.keyword_arguments.fetch(:fields))
      key_fields = key.map { "#{_1.name}: $.#{_1.name}" }
      field_path = "#{location}._entities"

      memo[field_path] ||= []
      memo[field_path] << new(
        key: key.to_definition,
        type_name: entity_type.graphql_name,
        arguments: "representations: { #{key_fields.join(", ")}, #{TYPENAME}: $.#{TYPENAME} }",
      )
    end
  end
end

.from_kwargs(kwargs) ⇒ Object



47
48
49
50
51
52
53
# File 'lib/graphql/stitching/composer/type_resolver_config.rb', line 47

def from_kwargs(kwargs)
  new(
    key: kwargs[:key],
    type_name: kwargs[:type_name] || kwargs[:typeName],
    arguments: kwargs[:arguments],
  )
end