Module: AutoGraphQL::QueryBuilder

Extended by:
QueryBuilder
Included in:
QueryBuilder
Defined in:
lib/autographql/query_builder.rb

Instance Method Summary collapse

Instance Method Details

#build(models_and_opts) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/autographql/query_builder.rb', line 12

def build models_and_opts
  # dynamically create a QueryType subclass
  Class.new GraphQL::Schema::Object do
    def self.name
      'AutoGraphQL::QueryType'
    end

    type_map = AutoGraphQL::TypeBuilder.build(models_and_opts)

    type_map.each do |model, type|
      # define field for this type
      field type.name.downcase, type, null: true do
        argument :id, GraphQL::Types::ID, required: true
      end

      # create loader
      define_method(type.name.downcase) do |id:|
        model.find id
      end
    end
  end

end