Class: GraphQL::Schema::Directive

Inherits:
Member
  • Object
show all
Extended by:
Member::HasArguments, Member::HasArguments::HasDirectiveArguments, Member::HasValidators
Defined in:
lib/graphql/schema/directive.rb,
lib/graphql/schema/directive/skip.rb,
lib/graphql/schema/directive/one_of.rb,
lib/graphql/schema/directive/feature.rb,
lib/graphql/schema/directive/flagged.rb,
lib/graphql/schema/directive/include.rb,
lib/graphql/schema/directive/transform.rb,
lib/graphql/schema/directive/deprecated.rb,
lib/graphql/schema/directive/specified_by.rb

Overview

Subclasses of this can influence how GraphQL::Execution::Interpreter runs queries.

  • .include?: if it returns false, the field or fragment will be skipped altogether, as if it were absent
  • .resolve: Wraps field resolution (so it should call yield to continue)

Direct Known Subclasses

Deprecated, Feature, Flagged, Include, OneOf, Skip, SpecifiedBy, Transform

Defined Under Namespace

Classes: Deprecated, Feature, Flagged, Include, InvalidArgumentError, OneOf, Skip, SpecifiedBy, Transform

Constant Summary collapse

LOCATIONS =
[
  *(RUNTIME_LOCATIONS = [
    QUERY =                  :QUERY,
    MUTATION =               :MUTATION,
    SUBSCRIPTION =           :SUBSCRIPTION,
    FIELD =                  :FIELD,
    FRAGMENT_DEFINITION =    :FRAGMENT_DEFINITION,
    FRAGMENT_SPREAD =        :FRAGMENT_SPREAD,
    INLINE_FRAGMENT =        :INLINE_FRAGMENT,
    VARIABLE_DEFINITION =    :VARIABLE_DEFINITION,
  ]),
  SCHEMA =                 :SCHEMA,
  SCALAR =                 :SCALAR,
  OBJECT =                 :OBJECT,
  FIELD_DEFINITION =       :FIELD_DEFINITION,
  ARGUMENT_DEFINITION =    :ARGUMENT_DEFINITION,
  INTERFACE =              :INTERFACE,
  UNION =                  :UNION,
  ENUM =                   :ENUM,
  ENUM_VALUE =             :ENUM_VALUE,
  INPUT_OBJECT =           :INPUT_OBJECT,
  INPUT_FIELD_DEFINITION = :INPUT_FIELD_DEFINITION,
]
DEFAULT_DEPRECATION_REASON =
'No longer supported'
LOCATION_DESCRIPTIONS =
{
  QUERY:                    'Location adjacent to a query operation.',
  MUTATION:                 'Location adjacent to a mutation operation.',
  SUBSCRIPTION:             'Location adjacent to a subscription operation.',
  FIELD:                    'Location adjacent to a field.',
  FRAGMENT_DEFINITION:      'Location adjacent to a fragment definition.',
  FRAGMENT_SPREAD:          'Location adjacent to a fragment spread.',
  INLINE_FRAGMENT:          'Location adjacent to an inline fragment.',
  SCHEMA:                   'Location adjacent to a schema definition.',
  SCALAR:                   'Location adjacent to a scalar definition.',
  OBJECT:                   'Location adjacent to an object type definition.',
  FIELD_DEFINITION:         'Location adjacent to a field definition.',
  ARGUMENT_DEFINITION:      'Location adjacent to an argument definition.',
  INTERFACE:                'Location adjacent to an interface definition.',
  UNION:                    'Location adjacent to a union definition.',
  ENUM:                     'Location adjacent to an enum definition.',
  ENUM_VALUE:               'Location adjacent to an enum value definition.',
  INPUT_OBJECT:             'Location adjacent to an input object type definition.',
  INPUT_FIELD_DEFINITION:   'Location adjacent to an input object field definition.',
  VARIABLE_DEFINITION:      'Location adjacent to a variable definition.',
}

Constants included from Member::HasArguments

Member::HasArguments::NO_ARGUMENTS

Constants included from EmptyObjects

EmptyObjects::EMPTY_ARRAY, EmptyObjects::EMPTY_HASH

Constants included from Member::GraphQLTypeNames

Member::GraphQLTypeNames::Boolean, Member::GraphQLTypeNames::ID, Member::GraphQLTypeNames::Int

Class Attribute Summary collapse

Instance Attribute Summary collapse

Attributes included from Member::BaseDSLMethods

#default_graphql_name

Attributes included from Member::RelayShortcuts

#connection_type, #connection_type_class, #edge_type, #edge_type_class

Attributes included from Member::HasAstNode

#ast_node

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Member::HasArguments

add_argument, all_argument_definitions, any_arguments?, argument, argument_class, arguments_statically_coercible?, coerce_arguments, extended, get_argument, included, own_arguments, remove_argument, validate_directive_argument

Methods included from Member::HasArguments::HasDirectiveArguments

validate_directive_argument

Methods included from Member::HasValidators

extended, validates, validators

Methods inherited from Member

authorizes?

Methods included from Member::BaseDSLMethods

#authorized?, #comment, #default_relay?, #description, #introspection, #introspection?, #mutation, #visible?

Methods included from FindInheritedValue

extended, included

Methods included from Member::BaseDSLMethods::ConfigurationExtension

#inherited

Methods included from Member::TypeSystemHelpers

#kind, #list?, #non_null?, #to_list_type, #to_non_null_type, #to_type_signature

Methods included from Member::Scoped

#inherited, #reauthorize_scoped_objects, #scope_items

Methods included from Member::HasPath

#path

Methods included from Member::HasAstNode

extended, #inherited

Methods included from Member::HasDirectives

add_directive, #directive, #directives, extended, get_directives, #inherited, #remove_directive, remove_directive

Constructor Details

#initialize(owner, **arguments) ⇒ Directive

Returns a new instance of Directive.



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'lib/graphql/schema/directive.rb', line 150

def initialize(owner, **arguments)
  @owner = owner
  assert_valid_owner
  if (arguments_cache = self.class.coerced_arguments_cache)
    if !self.class.arguments_statically_coercible?
      arguments_cache = nil
    elsif (cached_arguments = arguments_cache[arguments])
      @arguments = cached_arguments
      return
    end
  end
  # It's be nice if we had the real context here, but we don't. What we _would_ get is:
  # - error handling
  # - lazy resolution
  # Probably, those won't be needed here, since these are configuration arguments,
  # not runtime arguments.
  context = Query::NullContext.instance
  self.class.all_argument_definitions.each do |arg_defn|
    keyword = arg_defn.keyword
    arg_type = arg_defn.type
    if arguments.key?(keyword)
      value = arguments[keyword]
      # This is a Ruby-land value; convert it to graphql for validation
      graphql_value = begin
        coerce_value = value
        if arg_type.list? && (!coerce_value.nil?) && (!coerce_value.is_a?(Array))
          # When validating inputs, GraphQL accepts a single item
          # and implicitly converts it to a one-item list.
          # However, we're using result coercion here to go from Ruby value
          # to GraphQL value, so it doesn't have that feature.
          # Keep the GraphQL-type behavior but implement it manually:
          wrap_type = arg_type
          while wrap_type.list?
            if wrap_type.non_null?
              wrap_type = wrap_type.of_type
            end
            wrap_type = wrap_type.of_type
            coerce_value = [coerce_value]
          end
        end
        arg_type.coerce_isolated_result(coerce_value)
      rescue GraphQL::Schema::Enum::UnresolvedValueError
        # Let validation handle this
        value
      end
    elsif arg_defn.default_value?
      value = arg_defn.default_value
      graphql_value = arg_type.coerce_isolated_result(value) unless value.nil?
    else
      value = graphql_value = nil
    end

    result = arg_type.validate_input(graphql_value, context)
    if !result.valid?
      raise InvalidArgumentError, "@#{graphql_name}.#{arg_defn.graphql_name} on #{owner.path} is invalid (#{value.inspect}): #{result.problems.first["explanation"]}"
    end
  end
  self.class.validate!(arguments, context)
  @arguments = self.class.coerce_arguments(nil, arguments, context)
  if @arguments.is_a?(GraphQL::ExecutionError)
    raise @arguments
  end
  if arguments_cache
    arguments_cache[arguments] = @arguments
  end
end

Class Attribute Details

.coerced_arguments_cache ⇒ Object

When this is a Hash, instances with equal, statically coercible arguments reuse the first instance's coerced arguments. BuildFromDefinition uses this while it builds a schema, then removes it.



120
121
122
# File 'lib/graphql/schema/directive.rb', line 120

def coerced_arguments_cache
  @coerced_arguments_cache
end

Instance Attribute Details

#arguments ⇒ Object (readonly)

Returns

  • GraphQL::Interpreter::Arguments


145
146
147
# File 'lib/graphql/schema/directive.rb', line 145

def arguments
  @arguments
end

#owner ⇒ Object (readonly)

Returns

  • GraphQL::Schema::Field, GraphQL::Schema::Argument, Class, Module


140
141
142
# File 'lib/graphql/schema/directive.rb', line 140

def owner
  @owner
end

Class Method Details

.default_directive(new_default_directive = nil) ⇒ Object



55
56
57
58
59
60
61
62
63
# File 'lib/graphql/schema/directive.rb', line 55

def default_directive(new_default_directive = nil)
  if new_default_directive != nil
    @default_directive = new_default_directive
  elsif @default_directive.nil?
    @default_directive = (superclass.respond_to?(:default_directive) ? superclass.default_directive : false)
  else
    !!@default_directive
  end
end

.default_directive? ⇒ Boolean

Returns:



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

def default_directive?
  default_directive
end

.default_graphql_name ⇒ Object

Return a name based on the class name, but downcase the first letter.



26
27
28
29
30
31
32
# File 'lib/graphql/schema/directive.rb', line 26

def default_graphql_name
  @default_graphql_name ||= begin
    camelized_name = super.dup
    camelized_name[0] = camelized_name[0].downcase
    -camelized_name
  end
end

.include?(_object, arguments, context) ⇒ Boolean

If false, this part of the query won't be evaluated

Returns:



70
71
72
# File 'lib/graphql/schema/directive.rb', line 70

def include?(_object, arguments, context)
  static_include?(arguments, context)
end

.locations(*new_locations) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/graphql/schema/directive.rb', line 34

def locations(*new_locations)
  if !new_locations.empty?
    is_runtime = false #: bool
    new_locations.each do |new_loc|
      loc_sym = new_loc.to_sym
      if !LOCATIONS.include?(loc_sym)
        raise ArgumentError, "#{self} (#{self.graphql_name}) has an invalid directive location: `locations #{new_loc}` "
      end
      is_runtime ||= RUNTIME_LOCATIONS.include?(loc_sym)
    end
    @locations = new_locations
    @is_runtime = is_runtime
  else
    @locations ||= (superclass.respond_to?(:locations) ? superclass.locations : [])
  end
end

.on_field? ⇒ Boolean

Returns:



98
99
100
# File 'lib/graphql/schema/directive.rb', line 98

def on_field?
  locations.include?(FIELD)
end

.on_fragment? ⇒ Boolean

Returns:



102
103
104
# File 'lib/graphql/schema/directive.rb', line 102

def on_fragment?
  locations.include?(FRAGMENT_SPREAD) && locations.include?(INLINE_FRAGMENT)
end

.on_operation? ⇒ Boolean

Returns:



106
107
108
# File 'lib/graphql/schema/directive.rb', line 106

def on_operation?
  locations.include?(QUERY) && locations.include?(MUTATION) && locations.include?(SUBSCRIPTION)
end

.path ⇒ Object



20
21
22
# File 'lib/graphql/schema/directive.rb', line 20

def path
  "@#{super}"
end

.repeatable(new_value) ⇒ Object



114
115
116
# File 'lib/graphql/schema/directive.rb', line 114

def repeatable(new_value)
  @repeatable = new_value
end

.repeatable? ⇒ Boolean

Returns:



110
111
112
# File 'lib/graphql/schema/directive.rb', line 110

def repeatable?
  !!@repeatable
end

.resolve(object, arguments, context) ⇒ Object

Continuing is passed as a block; yield to continue



80
81
82
# File 'lib/graphql/schema/directive.rb', line 80

def resolve(object, arguments, context)
  yield
end

.resolve_each(object, arguments, context) ⇒ Object

Continuing is passed as a block, yield to continue.



85
86
87
# File 'lib/graphql/schema/directive.rb', line 85

def resolve_each(object, arguments, context)
  yield
end

.resolve_field ⇒ Object



89
# File 'lib/graphql/schema/directive.rb', line 89

def resolve_field(...); end

.resolve_fragment_spread ⇒ Object



90
# File 'lib/graphql/schema/directive.rb', line 90

def resolve_fragment_spread(...); end

.resolve_inline_fragment ⇒ Object



91
# File 'lib/graphql/schema/directive.rb', line 91

def resolve_inline_fragment(...); end

.resolve_operation ⇒ Object



92
# File 'lib/graphql/schema/directive.rb', line 92

def resolve_operation(...); end

.runtime? ⇒ Boolean

Returns:



51
52
53
# File 'lib/graphql/schema/directive.rb', line 51

def runtime?
  @is_runtime
end

.static_include?(_arguments, _context) ⇒ Boolean

Determines whether Execution::Lookahead considers the field to be selected

Returns:



75
76
77
# File 'lib/graphql/schema/directive.rb', line 75

def static_include?(_arguments, _context)
  true
end

.validate!(arguments, context) ⇒ Object



94
95
96
# File 'lib/graphql/schema/directive.rb', line 94

def validate!(arguments, context)
  Schema::Validator.validate!(validators, self, context, arguments)
end

Instance Method Details

#graphql_name ⇒ Object



217
218
219
# File 'lib/graphql/schema/directive.rb', line 217

def graphql_name
  self.class.graphql_name
end