Class: GraphQL::Schema::FieldExtension

Inherits:
Object
  • Object
show all
Defined in:
lib/graphql/schema/field_extension.rb

Overview

Extend this class to make field-level customizations to resolve behavior.

When a extension is added to a field with extension(MyExtension), a MyExtension instance is created, and its hooks are applied whenever that field is called.

The instance is frozen so that instance variables aren’t modified during query execution, which could cause all kinds of issues due to race conditions.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(field:, options:) ⇒ FieldExtension

Called when the extension is mounted with extension(name, options). The instance is frozen to avoid improper use of state during execution.

Parameters:

  • field (GraphQL::Schema::Field)

    The field where this extension was mounted

  • options (Object)

    The second argument to extension, or {} if nothing was passed.



22
23
24
25
26
27
# File 'lib/graphql/schema/field_extension.rb', line 22

def initialize(field:, options:)
  @field = field
  @options = options || {}
  apply
  freeze
end

Instance Attribute Details

#fieldGraphQL::Schema::Field (readonly)



13
14
15
# File 'lib/graphql/schema/field_extension.rb', line 13

def field
  @field
end

#optionsObject (readonly)

Returns:



16
17
18
# File 'lib/graphql/schema/field_extension.rb', line 16

def options
  @options
end

Instance Method Details

#after_resolve(object:, arguments:, context:, value:, memo:) ⇒ Object

Called after #field was resolved, and after any lazy values (like Promises) were synced, but before the value was added to the GraphQL response.

Whatever this hook returns will be used as the return value.

Parameters:

  • object (Object)

    The object the field is being resolved on

  • arguments (Hash)

    Ruby keyword arguments for resolving this field

  • context (Query::Context)

    the context for this query

  • value (Object)

    Whatever the field previously returned

  • memo (Object)

    The third value yielded by #resolve, or nil if there wasn’t one

Returns:

  • (Object)

    The return value for this field.



64
65
66
# File 'lib/graphql/schema/field_extension.rb', line 64

def after_resolve(object:, arguments:, context:, value:, memo:)
  value
end

#applyvoid

This method returns an undefined value.

Called when this extension is attached to a field. The field definition may be extended during this method.



32
33
# File 'lib/graphql/schema/field_extension.rb', line 32

def apply
end

#resolve(object:, arguments:, context:) {|object, arguments, memo| ... } ⇒ Object

Called before resolving #field. It should either:

  • yield values to continue execution; OR
  • return something else to shortcut field execution.

Whatever this method returns will be used for execution.

Parameters:

  • object (Object)

    The object the field is being resolved on

  • arguments (Hash)

    Ruby keyword arguments for resolving this field

  • context (Query::Context)

    the context for this query

Yield Parameters:

  • object (Object)

    The object to continue resolving the field on

  • arguments (Hash)

    The keyword arguments to continue resolving with

  • memo (Object)

    Any extension-specific value which will be passed to #after_resolve later

Returns:

  • (Object)

    The return value for this field.



49
50
51
# File 'lib/graphql/schema/field_extension.rb', line 49

def resolve(object:, arguments:, context:)
  yield(object, arguments, nil)
end