Module: ElasticGraph::Apollo::SchemaDefinition::FieldExtension

Overview

Extension module applied to ‘ElasticGraph::SchemaDefinition::SchemaElements::Field` to add Apollo tagging support.

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ApolloDirectives::Tag

#apollo_tag

Methods included from ApolloDirectives::Shareable

#apollo_shareable

Methods included from ApolloDirectives::RequiresScopes

#apollo_requires_scopes

Methods included from ApolloDirectives::Requires

#apollo_requires

Methods included from ApolloDirectives::Provides

#apollo_provides

Methods included from ApolloDirectives::Policy

#apollo_policy

Methods included from ApolloDirectives::Override

#apollo_override

Methods included from ApolloDirectives::Inaccessible

#apollo_inaccessible

Methods included from ApolloDirectives::External

#apollo_external

Methods included from ApolloDirectives::Authenticated

#apollo_authenticated

Class Method Details

.tagged_with?(element, tag_name) ⇒ Boolean

Helper method that indicates if the given schema element has a specific tag.

Returns:

  • (Boolean)


53
54
55
# File 'lib/elastic_graph/apollo/schema_definition/field_extension.rb', line 53

def self.tagged_with?(element, tag_name)
  element.directives.any? { |dir| dir.name == "tag" && dir.arguments == {name: tag_name} }
end

Instance Method Details

#tag_with(tag_name) ⇒ Object

Extension method designed to support Apollo’s contract variant tagging:

www.apollographql.com/docs/studio/contracts/

Calling this method on a field will cause the field to be tagged and also every schema element derived from the field (e.g. the filter field, the aggregation field, an the sort order enum), ensuring that all capabilities related to the field are available in the contract variant.



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/elastic_graph/apollo/schema_definition/field_extension.rb', line 34

def tag_with(tag_name)
  on_each_generated_schema_element do |element|
    needs_tagging =
      if element.is_a?(ElasticGraph::SchemaDefinition::SchemaElements::SortOrderEnumValue)
        # Each sort order enum value is based on a full field path (e.g. `parentField_subField_furtherNestedField_ASC`).
        # We must only tag the enum if each part of the full field path is also tagged. In this example, we should only
        # tag the enum value if `parentField`, `subField`, and `furtherNestedField` are all tagged.
        element.sort_order_field_path.all? { |f| FieldExtension.tagged_with?(f, tag_name) }
      else
        true
      end

    if needs_tagging && !FieldExtension.tagged_with?(element, tag_name)
      (_ = element).apollo_tag name: tag_name
    end
  end
end