Module: ElasticGraph::SchemaDefinition::Mixins::ImplementsInterfaces

Included in:
SchemaElements::InterfaceType, SchemaElements::ObjectType
Defined in:
elasticgraph-schema_definition/lib/elastic_graph/schema_definition/mixins/implements_interfaces.rb

Overview

Mixin for types that can implement interfaces (SchemaElements::ObjectType and SchemaElements::InterfaceType).

Instance Method Summary collapse

Instance Method Details

#implemented_interfacesArray<SchemaElements::TypeReference>

Returns list of type references for the interface types implemented by this type.

Returns:

  • (Array<SchemaElements::TypeReference>)

    list of type references for the interface types implemented by this type



54
55
56
# File 'elasticgraph-schema_definition/lib/elastic_graph/schema_definition/mixins/implements_interfaces.rb', line 54

def implemented_interfaces
  @implemented_interfaces ||= []
end

#implements(*interface_names) ⇒ void

This method returns an undefined value.

Declares that the current type implements the specified interface, making the current type a subtype of the interface. The current type must define all of the fields of the named interface, with the exact same field types.

Examples:

Implement an interface

ElasticGraph.define_schema do |schema|
  schema.interface_type "Athlete" do |t|
    t.field "name", "String"
    t.field "team", "String"
  end

  schema.object_type "BaseballPlayer" do |t|
    t.implements "Athlete"
    t.field "name", "String"
    t.field "team", "String"
    t.field "battingAvg", "Float"
  end

  schema.object_type "BasketballPlayer" do |t|
    t.implements "Athlete"
    t.field "name", "String"
    t.field "team", "String"
    t.field "pointsPerGame", "Float"
  end
end

Parameters:

  • interface_names (Array<String>)

    names of interface types implemented by this type



43
44
45
46
47
48
49
50
51
# File 'elasticgraph-schema_definition/lib/elastic_graph/schema_definition/mixins/implements_interfaces.rb', line 43

def implements(*interface_names)
  interface_refs = interface_names.map do |interface_name|
    schema_def_state.type_ref(interface_name).to_final_form.tap do |interface_ref|
      schema_def_state.implementations_by_interface_ref[interface_ref] << self
    end
  end

  implemented_interfaces.concat(interface_refs)
end

#to_sdl {|SchemaElements::Argument| ... } ⇒ String

Returns SDL string of the type.

Yields:

Yield Returns:

  • (Boolean)

    whether or not to include the argument in the generated GraphQL SDL

Returns:

  • (String)

    SDL string of the type



109
110
111
112
113
114
115
116
117
118
# File 'elasticgraph-schema_definition/lib/elastic_graph/schema_definition/mixins/implements_interfaces.rb', line 109

def to_sdl(&field_arg_selector)
  name_section =
    if implemented_interfaces.empty?
      name
    else
      "#{name} implements #{implemented_interfaces.join(" & ")}"
    end

  generate_sdl(name_section: name_section, &field_arg_selector)
end