Class: GraphQL::Schema::Finder

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

Overview

Find schema members using string paths

Examples:

Finding object types

MySchema.find("SomeObjectType")

Finding fields

MySchema.find("SomeObjectType.myField")

Finding arguments

MySchema.find("SomeObjectType.myField.anArgument")

Finding directives

MySchema.find("@include")

Defined Under Namespace

Classes: MemberNotFoundError

Instance Method Summary collapse

Constructor Details

#initialize(schema) ⇒ Finder

Returns a new instance of Finder.



22
23
24
# File 'lib/graphql/schema/finder.rb', line 22

def initialize(schema)
  @schema = schema
end

Instance Method Details

#find(path) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/graphql/schema/finder.rb', line 26

def find(path)
  path = path.split(".")
  type_or_directive = path.shift

  if type_or_directive.start_with?("@")
    directive = schema.directives[type_or_directive[1..-1]]

    if directive.nil?
      raise MemberNotFoundError, "Could not find directive `#{type_or_directive}` in schema."
    end

    return directive if path.empty?

    find_in_directive(directive, path: path)
  else
    type = schema.get_type(type_or_directive) # rubocop:disable Development/ContextIsPassedCop -- build-time

    if type.nil?
      raise MemberNotFoundError, "Could not find type `#{type_or_directive}` in schema."
    end

    return type if path.empty?

    find_in_type(type, path: path)
  end
end