Module: ElasticGraph::SchemaDefinition::Mixins::HasDirectives

Overview

Provides support for annotating any schema element with a GraphQL directive.

Instance Method Summary collapse

Instance Method Details

#directive(name, arguments = {}) ⇒ void

Note:

If you’re using a custom directive rather than a standard GraphQL directive like ‘@deprecated`, you’ll also need to use API#raw_sdl to define the custom directive.

This method returns an undefined value.

Adds a GraphQL directive to the current schema element.

Examples:

Add a standard GraphQL directive to a field

ElasticGraph.define_schema do |schema|
  schema.object_type "Campaign" do |t|
    t.field "id", "ID" do |f|
      f.directive "deprecated"
    end
  end
end

Define a custom GraphQL directive and add it to an object type

ElasticGraph.define_schema do |schema|
  # Define a directive we can use to annotate what system a data type comes from.
  schema.raw_sdl "directive @sourcedFrom(system: String!) on OBJECT"

  schema.object_type "Campaign" do |t|
    t.field "id", "ID"
    t.directive "sourcedFrom", system: "campaigns"
  end
end

Parameters:

  • name (String)

    name of the directive

  • arguments (Hash<String, Object>) (defaults to: {})

    arguments for the directive



42
43
44
# File 'lib/elastic_graph/schema_definition/mixins/has_directives.rb', line 42

def directive(name, arguments = {})
  directives << schema_def_state.factory.new_directive(name, arguments)
end

#directivesArray<SchemaElements::Directive>

Returns directives attached to this schema element.

Returns:



59
60
61
# File 'lib/elastic_graph/schema_definition/mixins/has_directives.rb', line 59

def directives
  @directives ||= []
end

#directives_sdl(suffix_with: "", prefix_with: "") ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Helper method designed for use by including classes to get the formatted directive SDL.

Parameters:

  • suffix_with (String) (defaults to: "")

    suffix to add on the end of the SDL

  • prefix_with (String) (defaults to: "")

    prefix to add to the beginning of the SDL

Returns:

  • (String)

    SDL string for the directives



52
53
54
55
56
# File 'lib/elastic_graph/schema_definition/mixins/has_directives.rb', line 52

def directives_sdl(suffix_with: "", prefix_with: "")
  sdl = directives.map(&:to_sdl).join(" ")
  return sdl if sdl.empty?
  prefix_with + sdl + suffix_with
end