Module: GraphQL::Client::Schema

Defined in:
lib/graphql/client/schema.rb,
lib/graphql/client/schema/base_type.rb,
lib/graphql/client/schema/enum_type.rb,
lib/graphql/client/schema/list_type.rb,
lib/graphql/client/schema/union_type.rb,
lib/graphql/client/schema/object_type.rb,
lib/graphql/client/schema/scalar_type.rb,
lib/graphql/client/schema/non_null_type.rb,
lib/graphql/client/schema/interface_type.rb,
lib/graphql/client/schema/possible_types.rb,
lib/graphql/client/schema/skip_directive.rb,
lib/graphql/client/schema/include_directive.rb

Defined Under Namespace

Modules: BaseType, ClassMethods, ObjectType Classes: EnumType, IncludeDirective, InterfaceType, ListType, NonNullType, ObjectClass, PossibleTypes, ScalarType, SkipDirective, UnionType

Class Method Summary collapse

Class Method Details

.class_for(schema, type, cache) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/graphql/client/schema.rb', line 85

def self.class_for(schema, type, cache)
  return cache[type] if cache[type]

  case type
  when GraphQL::InputObjectType
    nil
  when GraphQL::ScalarType
    cache[type] = ScalarType.new(type)
  when GraphQL::EnumType
    cache[type] = EnumType.new(type)
  when GraphQL::ListType
    cache[type] = class_for(schema, type.of_type, cache).to_list_type
  when GraphQL::NonNullType
    cache[type] = class_for(schema, type.of_type, cache).to_non_null_type
  when GraphQL::UnionType
    klass = cache[type] = UnionType.new(type)

    type.possible_types.each do |possible_type|
      possible_klass = class_for(schema, possible_type, cache)
      possible_klass.send :include, klass
    end

    klass
  when GraphQL::InterfaceType
    cache[type] = InterfaceType.new(type)
  when GraphQL::ObjectType
    klass = cache[type] = ObjectType.new(type)

    type.interfaces.each do |interface|
      klass.send :include, class_for(schema, interface, cache)
    end

    type.all_fields.each do |field|
      klass.fields[field.name.to_sym] = class_for(schema, field.type, cache)
    end

    klass
  else
    raise TypeError, "unexpected #{type.class}"
  end
end

.generate(schema) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/graphql/client/schema.rb', line 60

def self.generate(schema)
  mod = Module.new
  mod.extend ClassMethods

  mod.define_singleton_method :schema do
    schema
  end

  cache = {}
  schema.types.each do |name, type|
    next if name.start_with?("__")
    if klass = class_for(schema, type, cache)
      klass.schema_module = mod
      mod.set_class(name, klass)
    end
  end

  directives = {}
  mod.define_singleton_method(:directives) { directives }
  directives[:include] = IncludeDirective
  directives[:skip] = SkipDirective

  mod
end