Module: GraphQL::Schema::JSONLoader

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

Class Method Summary collapse

Class Method Details

.define_interface(types, type) ⇒ Object



56
57
58
59
60
# File 'lib/graphql/schema/json_loader.rb', line 56

def self.define_interface(types, type)
  InterfaceType.define do
    name type.fetch("name")
  end
end

.define_object(types, type) ⇒ Object



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

def self.define_object(types, type)
  ObjectType.define do
    name type.fetch("name")
    description type["description"]

    Array(type["fields"]).each do |field_data|
      field field_data["name"] do
        type JSONLoader.resolve_type(types, field_data["type"])
        description field_data["description"]
        field_data["args"].each do |arg|
          argument arg["name"] do
            type JSONLoader.resolve_type(types, arg["type"])
            description arg["description"]
          end
        end
      end
    end
  end
end

.define_scalar(types, type) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/graphql/schema/json_loader.rb', line 82

def self.define_scalar(types, type)
  case name = type.fetch("name")
  when "Int"
    INT_TYPE
  when "String"
    STRING_TYPE
  when "Float"
    FLOAT_TYPE
  when "Boolean"
    BOOLEAN_TYPE
  when "ID"
    ID_TYPE
  else
    # TODO: handle other scalar names
    fail NotImplementedError, name + " scalar not implemented"
  end
end

.define_schema(json) ⇒ Object



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

def self.define_schema(json)
  schema = JSON.load(json).fetch("data").fetch("__schema")
  types = Schema::JSONLoader.define_types(schema)
  # TODO: handle schema["mutationType"]
  # TODO: handle schema["subscriptionType"]
  query = types.fetch(schema.fetch("queryType").fetch("name"))
  Schema.new(query: query, types: types.values)
end

.define_types(schema) ⇒ Object



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

def self.define_types(schema)
  schema.fetch("types").inject({}) do |types, type|
    type_kind, type_name = type.fetch("kind"), type.fetch("name")

    if !type_name.start_with?("__")
      case type_kind
      when "INTERFACE"
        types[type_name] = define_interface(types, type)
      when "OBJECT"
        types[type_name] = define_object(types, type)
      when "SCALAR"
        types[type_name] = define_scalar(types, type)
      else
        # TODO: handle other type kinds
        fail NotImplementedError, type_kind + " not implemented"
      end
    end

    types
  end
end

.resolve_type(types, type) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/graphql/schema/json_loader.rb', line 38

def self.resolve_type(types, type)
  case kind = type.fetch("kind")
  when "INTERFACE"
    types.fetch(type.fetch("name"))
  when "LIST"
    ListType.new(of_type: resolve_type(types, type.fetch("ofType")))
  when "NON_NULL"
    NonNullType.new(of_type: resolve_type(types, type.fetch("ofType")))
  when "OBJECT"
    types.fetch(type.fetch("name"))
  when "SCALAR"
    types.fetch(type.fetch("name"))
  else
    # TODO: handle other type kinds
    fail NotImplementedError, kind + " not implemented"
  end
end