Module: GraphQL::Client::Schema::ClassMethods

Defined in:
lib/graphql/client/schema.rb

Constant Summary collapse

DIRECTIVES =
{ include: IncludeDirective,
skip: SkipDirective }.freeze

Instance Method Summary collapse

Instance Method Details

#define_class(definition, ast_nodes, type) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/graphql/client/schema.rb', line 18

def define_class(definition, ast_nodes, type)
  type_class = case type.kind.name
  when "NON_NULL"
    define_class(definition, ast_nodes, type.of_type).to_non_null_type
  when "LIST"
    define_class(definition, ast_nodes, type.of_type).to_list_type
  else
    get_class(type.graphql_name).define_class(definition, ast_nodes)
  end

  ast_nodes.each do |ast_node|
    ast_node.directives.each do |directive|
      if directive = self.directives[directive.name.to_sym]
        type_class = directive.new(type_class)
      end
    end
  end

  type_class
end

#directivesObject



58
59
60
# File 'lib/graphql/client/schema.rb', line 58

def directives
  DIRECTIVES
end

#get_class(type_name) ⇒ Object



39
40
41
# File 'lib/graphql/client/schema.rb', line 39

def get_class(type_name)
  const_get(normalize_type_name(type_name))
end

#set_class(type_name, klass) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
# File 'lib/graphql/client/schema.rb', line 43

def set_class(type_name, klass)
  class_name = normalize_type_name(type_name)

  if const_defined?(class_name, false)
    raise ArgumentError,
      "Can't define #{class_name} to represent type #{type_name} " \
      "because it's already defined"
  end

  const_set(class_name, klass)
end