Class: GraphQL::Schema::Finder

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

Overview

Find schema members using string paths

Examples

Example: Finding object types

MySchema.find("SomeObjectType")

Example: Finding fields

MySchema.find("SomeObjectType.myField")

Example: Finding arguments

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

Example: 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.



37
38
39
# File 'lib/graphql/schema/finder.rb', line 37

def initialize(schema)
  @schema = schema
end

Instance Method Details

#find(path) ⇒ Object



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

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