Class: GraphQL::Stitching::Composer::BoundaryConfig

Inherits:
Object
  • Object
show all
Defined in:
lib/graphql/stitching/composer/boundary_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:, federation: false) ⇒ BoundaryConfig

Returns a new instance of BoundaryConfig.



66
67
68
69
70
# File 'lib/graphql/stitching/composer/boundary_config.rb', line 66

def initialize(key:, type_name:, federation: false)
  @key = key
  @type_name = type_name
  @federation = federation
end

Instance Attribute Details

#federationObject (readonly)

Returns the value of attribute federation.



64
65
66
# File 'lib/graphql/stitching/composer/boundary_config.rb', line 64

def federation
  @federation
end

#keyObject (readonly)

Returns the value of attribute key.



64
65
66
# File 'lib/graphql/stitching/composer/boundary_config.rb', line 64

def key
  @key
end

#type_nameObject (readonly)

Returns the value of attribute type_name.



64
65
66
# File 'lib/graphql/stitching/composer/boundary_config.rb', line 64

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/boundary_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 ComposerError, "Invalid stitch directive type `#{kwargs[:parent_type_name]}`" unless type

    field = type.get_field(kwargs[:field_name])
    raise ComposerError, "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/boundary_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 = directive.arguments.keyword_arguments.fetch(:fields).strip
      raise ComposerError, "Composite federation keys are not supported." unless /^\w+$/.match?(key)

      field_path = "#{location}._entities"
      memo[field_path] ||= []
      memo[field_path] << new(
        key: key,
        type_name: entity_type.graphql_name,
        federation: true,
      )
    end
  end
end

.from_kwargs(kwargs) ⇒ Object



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

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