Class: GraphQL::Schema

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

Overview

SCHEMA keeps track of defined nodes, fields and calls.

Although you don’t interact with it directly, it responds to queries for ‘schema()` and `__type__` info.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSchema

Returns a new instance of Schema.



7
8
9
10
11
12
13
# File 'lib/graphql/schema.rb', line 7

def initialize
  @types = {}
  @connections = {}
  @fields = {}
  @class_names = {}
  @calls = {}
end

Instance Attribute Details

#callsObject (readonly)

Returns the value of attribute calls.



6
7
8
# File 'lib/graphql/schema.rb', line 6

def calls
  @calls
end

#class_namesObject (readonly)

Returns the value of attribute class_names.



6
7
8
# File 'lib/graphql/schema.rb', line 6

def class_names
  @class_names
end

#connectionsObject (readonly)

Returns the value of attribute connections.



6
7
8
# File 'lib/graphql/schema.rb', line 6

def connections
  @connections
end

#fieldsObject (readonly)

Returns the value of attribute fields.



6
7
8
# File 'lib/graphql/schema.rb', line 6

def fields
  @fields
end

#typesObject (readonly)

Returns the value of attribute types.



6
7
8
# File 'lib/graphql/schema.rb', line 6

def types
  @types
end

Instance Method Details

#add_call(call_class) ⇒ Object



15
16
17
18
# File 'lib/graphql/schema.rb', line 15

def add_call(call_class)
  remove_call(call_class)
  @calls[call_class.schema_name] = call_class
end

#add_connection(node_class) ⇒ Object



68
69
70
71
72
73
74
# File 'lib/graphql/schema.rb', line 68

def add_connection(node_class)
  existing_name = @connections.key(node_class)
  if existing_name
    @connections.delete(existing_name)
  end
  @connections[node_class.schema_name.to_s] = node_class
end

#add_field(field_class) ⇒ Object



84
85
86
87
88
89
90
# File 'lib/graphql/schema.rb', line 84

def add_field(field_class)
  existing_name = @fields.key(field_class)
  if existing_name
    @fields.delete(existing_name)
  end
  @fields[field_class.schema_name.to_s] = field_class
end

#add_type(node_class) ⇒ Object



35
36
37
38
39
40
41
42
43
# File 'lib/graphql/schema.rb', line 35

def add_type(node_class)
  existing_name = @types.key(node_class)
  if existing_name
    @types.delete(existing_name)
  end

  @class_names[node_class.ruby_class_name] = node_class
  @types[node_class.schema_name] = node_class
end

#call_namesObject



31
32
33
# File 'lib/graphql/schema.rb', line 31

def call_names
  @calls.keys
end

#connection_namesObject



80
81
82
# File 'lib/graphql/schema.rb', line 80

def connection_names
  @connections.keys
end

#field_namesObject



96
97
98
# File 'lib/graphql/schema.rb', line 96

def field_names
  @fields.keys
end

#get_call(identifier) ⇒ Object



20
21
22
# File 'lib/graphql/schema.rb', line 20

def get_call(identifier)
  @calls[identifier.to_s] || raise(GraphQL::RootCallNotDefinedError.new(identifier))
end

#get_connection(identifier) ⇒ Object



76
77
78
# File 'lib/graphql/schema.rb', line 76

def get_connection(identifier)
  @connections[identifier] || GraphQL::Connection.default_connection || raise(GraphQL::ConnectionNotDefinedError.new(identifier))
end

#get_field(identifier) ⇒ Object



92
93
94
# File 'lib/graphql/schema.rb', line 92

def get_field(identifier)
  @fields[identifier.to_s] || raise(GraphQL::FieldNotDefinedError.new("<unknown>", identifier))
end

#get_type(identifier) ⇒ Object



45
46
47
# File 'lib/graphql/schema.rb', line 45

def get_type(identifier)
  @types[identifier.to_s] || raise(GraphQL::NodeNotDefinedError.new(identifier))
end

#remove_call(call_class) ⇒ Object



24
25
26
27
28
29
# File 'lib/graphql/schema.rb', line 24

def remove_call(call_class)
  existing_name = @calls.key(call_class)
  if existing_name
    @calls.delete(existing_name)
  end
end

#type_for_object(app_object) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/graphql/schema.rb', line 53

def type_for_object(app_object)
  registered_class_names = @class_names.keys
  if app_object.is_a?(Class)
    app_class = app_object
  else
    app_class = app_object.class
  end
  app_class.ancestors.map(&:name).each do |class_name|
    if registered_class_names.include?(class_name)
      return @class_names[class_name]
    end
  end
  raise "Couldn't find node for class #{app_class} #{app_object} (ancestors: #{app_class.ancestors.map(&:name)}, defined: #{registered_class_names})"
end

#type_namesObject



49
50
51
# File 'lib/graphql/schema.rb', line 49

def type_names
  @types.keys
end