Class: GraphQL::Api::Schema

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

Instance Method Summary collapse

Constructor Details

#initialize(commands: [], queries: [], models: []) ⇒ Schema

Returns a new instance of Schema.



12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/graphql/api/schema.rb', line 12

def initialize(commands: [], queries: [], models: [])
  @types = {}
  @mutations = {}

  @load_commands = commands
  @load_queries = queries
  @load_models = models

  build_model_types
  build_mutations
  build_object_types
end

Instance Method Details

#all_commandsObject



33
34
35
# File 'lib/graphql/api/schema.rb', line 33

def all_commands
  @all_commands ||= all_constants('commands') + @load_commands
end

#all_modelsObject



25
26
27
# File 'lib/graphql/api/schema.rb', line 25

def all_models
  @all_models ||= all_constants('models') + @load_models
end

#all_queriesObject



29
30
31
# File 'lib/graphql/api/schema.rb', line 29

def all_queries
  @all_queries ||= all_constants('queries') + @load_queries
end

#build_model_typesObject



259
260
261
# File 'lib/graphql/api/schema.rb', line 259

def build_model_types
  all_models.each { |model_class| @types[model_class] = create_type(model_class) }
end

#build_mutationsObject



267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
# File 'lib/graphql/api/schema.rb', line 267

def build_mutations
  all_models.each do |model_class|
    @mutations[model_class] = [
        ["create#{model_class.name}", create_mutation(model_class)],
        ["update#{model_class.name}", update_mutation(model_class)],
        ["delete#{model_class.name}", delete_mutation(model_class)],
    ].map { |x| x if x[1] }.compact
  end

  all_commands.each do |command|
    @mutations[command] = [
        [command.name.camelize(:lower), create_command_type(command)]
    ]
  end
end

#build_object_typesObject



263
264
265
# File 'lib/graphql/api/schema.rb', line 263

def build_object_types
  all_queries.each { |query| @types[query] = nil }
end

#create_command_type(object_type) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/graphql/api/schema.rb', line 75

def create_command_type(object_type)
  object_types = @types

  GraphQL::Relay::Mutation.define do
    name object_type.name
    description "Command #{object_type.name}"

    object_type.inputs.each do |input, type|
      input_field input, graphql_type_of(type)
    end

    object_type.returns.each do |return_name, return_type|
      return_field return_name, graphql_type_for_object(return_type, object_types)
    end

    resolve -> (inputs, ctx) {
      object_type.new(inputs, ctx).perform
    }
  end
end

#create_mutation(model_class) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/graphql/api/schema.rb', line 96

def create_mutation(model_class)
  return nil unless model_class < ActiveRecord::Base

  object_types = @types

  GraphQL::Relay::Mutation.define do
    name "Create#{model_class.name}"
    description "Create #{model_class.name}"

    model_class.columns.each do |column|
      input_field column.name, graphql_type(column)
    end

    return_field model_class.name.underscore.to_sym, object_types[model_class]

    resolve -> (inputs, ctx) {
      item = model_class.create!(inputs.to_h)
      {model_class.name.underscore.to_sym => item}
    }
  end
end

#create_type(model_class) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/graphql/api/schema.rb', line 37

def create_type(model_class)
  object_types = @types

  GraphQL::ObjectType.define do
    name model_class.name
    description "Get #{model_class.name}"

    if model_class.respond_to?(:columns)
      model_class.columns.each do |column|
        field column.name do
          type graphql_type(column)
          resolve -> (obj, args, ctx) { graphql_fetch(obj, ctx, column.name) }
        end
      end
    end

    if model_class.respond_to?(:fields)
      model_class.fields.each do |field_name, field_type|
        field field_name, graphql_type_of(field_type)
      end
    end

    if model_class.respond_to?(:reflections)
      model_class.reflections.each do |name, association|
        field name do
          if association.collection?
            type types[object_types[association.class_name.constantize]]
          else
            type object_types[association.class_name.constantize]
          end
          resolve -> (obj, args, ctx) { graphql_fetch(obj, ctx, name) }
        end
      end
    end

  end
end

#delete_mutation(model_class) ⇒ Object



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/graphql/api/schema.rb', line 142

def delete_mutation(model_class)
  return nil unless model_class < ActiveRecord::Base

  GraphQL::Relay::Mutation.define do
    name "Delete#{model_class.name}"
    description "Delete #{model_class.name}"

    input_field :id, !types.ID

    return_field "#{model_class.name.underscore}_id".to_sym, types.ID

    resolve -> (inputs, ctx) {
      item = model_class.find(inputs[:id]).destroy!
      {"#{model_class.name.underscore}_id".to_sym => item.id}
    }
  end
end

#mutation(&block) ⇒ Object



240
241
242
243
244
245
246
247
248
249
250
251
252
253
# File 'lib/graphql/api/schema.rb', line 240

def mutation(&block)
  mutations = @mutations

  @mutation ||= GraphQL::ObjectType.define do
    name 'Mutation'
    instance_eval(&block) if block

    mutations.each do |model_class, muts|
      muts.each do |mutation|
        field mutation[0], field: mutation[1].field
      end
    end
  end
end

#query(&block) ⇒ Object



160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
# File 'lib/graphql/api/schema.rb', line 160

def query(&block)
  object_types = @types

  @query ||= GraphQL::ObjectType.define do
    name 'Query'
    description 'The query root for this schema'

    instance_eval(&block) if block

    object_types.each do |object_class, graph_type|
      if object_class < ActiveRecord::Base

        field(object_class.name.camelize(:lower)) do
          type graph_type
          argument :id, types.ID

          if object_class.respond_to?(:arguments)
            object_class.arguments.each do |arg|
              argument arg, graphql_type(object_class.columns.find { |c| c.name.to_sym == arg.to_sym })
            end
          end

          resolve -> (obj, args, ctx) {
            if object_class.respond_to?(:graph_find)
              object_class.graph_find(args, ctx)
            else
              object_class.find_by!(args.to_h)
            end
          }
        end

        field(object_class.name.camelize(:lower).pluralize) do
          type types[graph_type]
          argument :limit, types.Int

          if object_class.respond_to?(:arguments)
            object_class.arguments.each do |arg|
              argument arg, graphql_type(object_class.columns.find { |c| c.name.to_sym == arg.to_sym })
            end
          end

          resolve -> (obj, args, ctx) {
            if object_class.respond_to?(:graph_where)
              object_class.graph_where(args, ctx)
            else
              eager_load = []
              ctx.irep_node.children.each do |child|
                eager_load << child[0] if object_class.reflections.find { |name, _| name == child[0] }
              end

              query_args = args.to_h
              query_args.delete('limit')

              q = object_class.where(query_args)
              q.eager_load(*eager_load) if eager_load.any?
              q.limit(args[:limit] || 30)
            end
          }
        end

      elsif object_class.respond_to?(:arguments) && object_class.respond_to?(:return_type)

        field(object_class.name.camelize(:lower)) do
          type(graphql_type_for_object(object_class.return_type, object_types))

          object_class.arguments.each do |argument_name, argument_type|
            argument argument_name, graphql_type_of(argument_type)
          end

          resolve -> (obj, args, ctx) {
            object_class.new(args, ctx).execute
          }
        end

      end
    end

  end
end

#schemaObject



255
256
257
# File 'lib/graphql/api/schema.rb', line 255

def schema
  @schema ||= GraphQL::Schema.define(query: query, mutation: mutation)
end

#update_mutation(model_class) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/graphql/api/schema.rb', line 118

def update_mutation(model_class)
  return nil unless model_class < ActiveRecord::Base

  object_types = @types

  GraphQL::Relay::Mutation.define do
    name "Update#{model_class.name}"
    description "Update #{model_class.name}"

    input_field :id, !types.ID
    model_class.columns.each do |column|
      input_field column.name, graphql_type(column)
    end

    return_field model_class.name.underscore.to_sym, object_types[model_class]

    resolve -> (inputs, ctx) {
      item = model_class.find(inputs[:id])
      item.update!(inputs.to_h)
      {model_class.name.underscore.to_sym => item}
    }
  end
end