Module: GraphQL::Schema::Member::HasDirectives

Included in:
Argument, EnumValue, Field, Interface::DefinitionMethods, GraphQL::Schema::Member, Resolver
Defined in:
lib/graphql/schema/member/has_directives.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.add_directive(schema_member, directives, directive_class, directive_options) ⇒ Object



60
61
62
63
# File 'lib/graphql/schema/member/has_directives.rb', line 60

def add_directive(schema_member, directives, directive_class, directive_options)
  remove_directive(directives, directive_class) unless directive_class.repeatable?
  directives << directive_class.new(schema_member, **directive_options)
end

.extended(child_cls) ⇒ Object



9
10
11
12
13
14
15
# File 'lib/graphql/schema/member/has_directives.rb', line 9

def self.extended(child_cls)
  super
  child_cls.module_exec {
    #: self as GraphQL::Schema::Member::HasDirectives
    self.own_directives = nil
  }
end

.get_directives(schema_member, directives, directives_method) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/graphql/schema/member/has_directives.rb', line 69

def get_directives(schema_member, directives, directives_method)
  case schema_member
  when Class
    inherited_directives = if schema_member.superclass.respond_to?(directives_method)
      superclass_directives = schema_member.superclass.public_send(directives_method)
      if superclass_directives.empty?
        # `superclass_directives` includes inherited ones, so there's nothing to merge
        GraphQL::EmptyObjects::EMPTY_ARRAY
      else
        get_directives(schema_member.superclass, superclass_directives, directives_method)
      end
    else
      GraphQL::EmptyObjects::EMPTY_ARRAY
    end
    if !inherited_directives.empty? && directives
      dirs = [] #: Array[GraphQL::Schema::Directive]?
      merge_directives(dirs, inherited_directives)
      merge_directives(dirs, directives)
      dirs
    elsif directives
      directives
    elsif !inherited_directives.empty?
      inherited_directives
    else
      GraphQL::EmptyObjects::EMPTY_ARRAY
    end
  when HasDirectives
    directives || GraphQL::EmptyObjects::EMPTY_ARRAY
  else
    raise "Invariant: how could #{schema_member} not be a Class or instance of HasDirectives?"
  end
end

.remove_directive(directives, directive_class) ⇒ Object



65
66
67
# File 'lib/graphql/schema/member/has_directives.rb', line 65

def remove_directive(directives, directive_class)
  directives && directives.reject! { |d| d.is_a?(directive_class) }
end

Instance Method Details

#directive(dir_class, **options) ⇒ Object

Create an instance of dir_class for self, using options.

It removes a previously-attached instance of dir_class, if there is one.

Returns

  • void

:call-seq:

directive(dir_class, **options) -> void


32
33
34
35
36
# File 'lib/graphql/schema/member/has_directives.rb', line 32

def directive(dir_class, **options)
  @own_directives ||= []
  HasDirectives.add_directive(self, @own_directives, dir_class, options)
  nil
end

#directives ⇒ Object



55
56
57
# File 'lib/graphql/schema/member/has_directives.rb', line 55

def directives
  HasDirectives.get_directives(self, @own_directives, :directives)
end

#inherited(child_cls) ⇒ Object



17
18
19
20
# File 'lib/graphql/schema/member/has_directives.rb', line 17

def inherited(child_cls)
  super
  child_cls.own_directives = nil
end

#remove_directive(dir_class) ⇒ Object

Remove an attached instance of dir_class, if there is one

Parameters

  • dir_class (Class<GraphQL::Schema::Directive>)

Returns

  • void

:call-seq:

remove_directive(Class[GraphQL::Schema::Directive] dir_class) -> void


50
51
52
53
# File 'lib/graphql/schema/member/has_directives.rb', line 50

def remove_directive(dir_class)
  HasDirectives.remove_directive(@own_directives, dir_class)
  nil
end