Class: GraphQL::InternalRepresentation::Scope

Inherits:
Object
  • Object
show all
Defined in:
lib/graphql/internal_representation/scope.rb

Overview

At a point in the AST, selections may apply to one or more types. Scope represents those types which selections may apply to.

Scopes can be defined by:

  • A single concrete or abstract type
  • An array of types
  • nil

The AST may be scoped to an array of types when two abstractly-typed fragments occur in inside one another.

Constant Summary collapse

NO_TYPES =
[].freeze

Instance Method Summary collapse

Constructor Details

#initialize(query, type_defn) ⇒ Scope

Returns a new instance of Scope.

Parameters:



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/graphql/internal_representation/scope.rb', line 20

def initialize(query, type_defn)
  @query = query
  @type = type_defn
  @abstract_type = false
  @types = case type_defn
  when Array
    type_defn
  when GraphQL::BaseType
    @abstract_type = true
    nil
  when nil
    NO_TYPES
  else
    raise "Unexpected scope type: #{type_defn}"
  end
end

Instance Method Details

#eachObject

Call the block for each type in self. This uses the simplest possible expression of self, so if this scope is defined by an abstract type, it gets yielded.



69
70
71
72
73
74
75
# File 'lib/graphql/internal_representation/scope.rb', line 69

def each
  if @abstract_type
    yield(@type)
  else
    @types.each { |t| yield(t) }
  end
end

#enter(other_type_defn) ⇒ Scope

From a starting point of self, create a new scope by condition other_type_defn.

Parameters:

Returns:



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/graphql/internal_representation/scope.rb', line 40

def enter(other_type_defn)
  case other_type_defn
  when nil
    # The type wasn't found, who cares
    Scope.new(@query, nil)
  when @type
    # The condition is the same as current, so reuse self
    self
  when GraphQL::UnionType, GraphQL::InterfaceType
    # Make a new scope of the intersection between the previous & next conditions
    new_types = @query.possible_types(other_type_defn) & concrete_types
    Scope.new(@query, new_types)
  when GraphQL::BaseType
    # If this type is valid within the current scope,
    # return a new scope of _exactly_ this type.
    # Otherwise, this type is out-of-scope so the scope is null.
    if concrete_types.include?(other_type_defn)
      Scope.new(@query, other_type_defn)
    else
      Scope.new(@query, nil)
    end
  else
    raise "Unexpected scope: #{other_type_defn.inspect}"
  end
end