Class: GraphQLPreview::MemberFromPath

Inherits:
Object
  • Object
show all
Defined in:
lib/graphql-preview/member_from_path.rb

Defined Under Namespace

Classes: MemberNotFoundError

Instance Method Summary collapse

Constructor Details

#initialize(schema) ⇒ MemberFromPath

Returns a new instance of MemberFromPath.



5
6
7
# File 'lib/graphql-preview/member_from_path.rb', line 5

def initialize(schema)
  @schema = schema
end

Instance Method Details

#find(path) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/graphql-preview/member_from_path.rb', line 9

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.types[type_or_directive]

    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