Class: GraphQL::Execution::Lookahead

Inherits:
Object
  • Object
show all
Defined in:
lib/graphql/execution/lookahead.rb

Overview

Lookahead creates a uniform interface to inspect the forthcoming selections.

It assumes that the AST it’s working with is valid. (So, it’s safe to use during execution, but if you’re using it directly, be sure to validate first.)

A field may get access to its lookahead by adding extras: [:lookahead] to its configuration.

NOTE: Lookahead for typed fragments (eg node { ... on Thing { ... } }) hasn’t been implemented yet. It’s possible, I just didn’t need it yet. Feel free to open a PR or an issue if you want to add it.

Examples:

looking ahead in a field

field :articles, [Types::Article], null: false,
  extras: [:lookahead]

# For example, imagine a faster database call
# may be issued when only some fields are requested.
#
# Imagine that _full_ fetch must be made to satisfy `fullContent`,
# we can look ahead to see if we need that field. If we do,
# we make the expensive database call instead of the cheap one.
def articles(lookahead:)
  if lookahead.selects?(:full_content)
    fetch_full_articles(object)
  else
    fetch_preview_articles(object)
  end
end

Direct Known Subclasses

NullLookahead

Defined Under Namespace

Modules: ArgumentHelpers, FieldHelpers Classes: NullLookahead

Constant Summary

NULL_LOOKAHEAD =

A singleton, so that misses don’t come with overhead.

NullLookahead.new

Instance Method Summary collapse

Constructor Details

#initialize(query:, ast_nodes:, field: nil, root_type: nil) ⇒ Lookahead

Returns a new instance of Lookahead

Parameters:



38
39
40
41
42
43
# File 'lib/graphql/execution/lookahead.rb', line 38

def initialize(query:, ast_nodes:, field: nil, root_type: nil)
  @ast_nodes = ast_nodes
  @field = field
  @root_type = root_type
  @query = query
end

Instance Method Details

#selected?Boolean

Returns True if this lookahead represents a field that was requested

Returns:

  • (Boolean)

    True if this lookahead represents a field that was requested



62
63
64
# File 'lib/graphql/execution/lookahead.rb', line 62

def selected?
  true
end

#selection(field_name, arguments: nil) ⇒ GraphQL::Execution::Lookahead

Like #selects?, but can be used for chaining. It returns a null object (check with #selected?)



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
# File 'lib/graphql/execution/lookahead.rb', line 69

def selection(field_name, arguments: nil)
  next_field_name = normalize_name(field_name)

  next_field_owner = if @field
    @field.type.unwrap
  else
    @root_type
  end

  next_field_defn = FieldHelpers.get_field(@query.schema, next_field_owner, next_field_name)
  if next_field_defn
    next_nodes = []
    @ast_nodes.each do |ast_node|
      ast_node.selections.each do |selection|
        find_selected_nodes(selection, next_field_name, next_field_defn, arguments: arguments, matches: next_nodes)
      end
    end

    if next_nodes.any?
      Lookahead.new(query: @query, ast_nodes: next_nodes, field: next_field_defn)
    else
      NULL_LOOKAHEAD
    end
  else
    NULL_LOOKAHEAD
  end
end

#selects?(field_name, arguments: nil) ⇒ Boolean

True if this node has a selection on field_name. If field_name is a String, it is treated as a GraphQL-style (camelized) field name and used verbatim. If field_name is a Symbol, it is treated as a Ruby-style (underscored) name and camelized before comparing.

If arguments: is provided, each provided key/value will be matched against the arguments in the next selection. This method will return false if any of the given arguments: are not present and matching in the next selection. (But, the next selection may contain more than the given arguments.)

Parameters:

  • field_name (String, Symbol)
  • arguments (Hash)

    Arguments which must match in the selection

Returns:

  • (Boolean)


57
58
59
# File 'lib/graphql/execution/lookahead.rb', line 57

def selects?(field_name, arguments: nil)
  selection(field_name, arguments: arguments).selected?
end