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

Overview

Extends SchemaDefinition::SchemaElements::Field to offer Apollo field directives.

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.

Parameters:

  • element (Object)

    element to check

  • tag_name (String)

    tag to check

Returns:

  • (Boolean)


70
71
72
# File 'elasticgraph-apollo/lib/elastic_graph/apollo/schema_definition/field_extension.rb', line 70

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) ⇒ void

This method returns an undefined value.

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

Calling this method on a field will cause the field and every schema element derived from the field (e.g. the filter field, he aggregation field, etc), to be tagged with the given tag_name, ensuring that all capabilities related to the field are available in the contract variant.

Examples:

Tag a field (and its derived elements) with “public”

ElasticGraph.define_schema do |schema|
  schema.object_type "Campaign" do |t|
    t.field "name", "String" do |f|
      f.tag_with "public"
    end
  end
end

Parameters:

  • tag_name (String)

    tag to add to schema elements generated for this field

See Also:



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'elasticgraph-apollo/lib/elastic_graph/apollo/schema_definition/field_extension.rb', line 47

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