Module: Graphoid::Types

Defined in:
lib/graphoid/definitions/types.rb

Constant Summary collapse

LIST =
{}
ENUMS =
{}

Class Method Summary collapse

Class Method Details

.generate(model) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/graphoid/definitions/types.rb', line 8

def generate(model)
  Graphoid::Types::Meta ||= GraphQL::ObjectType.define do
    name("Meta")
    description("Meta Type")
    field("count", types.Int)
  end

  LIST[model] ||= GraphQL::ObjectType.define do
    name = Utils.graphqlize(model.name)
    name("#{name}Type")
    description("Generated model type for #{name}")

    Attribute.fields_of(model).each do |_field|
      type = Graphoid::Mapper.convert(_field)
      name = Utils.camelize(_field.name)
      field(name, type)

      model.class_eval do
        if _field.name.include?('_')
          define_method :"#{Utils.camelize(_field.name)}" do
            method_name = _field.name.to_s
            self[method_name] || self.send(method_name)
          end
        end
      end
    end

    Relation.relations_of(model).each do |name, relation|
      relation_class = relation.class_name.safe_constantize

      message = "in model #{model.name}: skipping relation #{relation.class_name}"
      unless relation_class
        STDERR.puts "Graphoid: warning: #{message} because the model name is not valid" if ENV['DEBUG']
        next
      end

      relation_type = LIST[relation_class]
      unless relation_type
        STDERR.puts "Graphoid: warning: #{message} because it was not found as a model" if ENV['DEBUG']
        next
      end

      name = Utils.camelize(relation.name)

      model.class_eval do
        if relation.name.to_s.include?('_')
          define_method :"#{name}" do
            self.send(relation.name)
          end
        end
      end

      if relation_type
        filter = Graphoid::Filters::LIST[relation_class]
        order  = Graphoid::Orders::LIST[relation_class]

        if Relation.new(relation).many?
          plural_name = name.pluralize

          field plural_name, types[relation_type] do
            Graphoid::Argument.query_many(self, filter, order)
            Graphoid::Types.resolve_many(self, relation_class, relation)
          end

          field "_#{plural_name}_meta", Graphoid::Types::Meta do
            Graphoid::Argument.query_many(self, filter, order)
            Graphoid::Types.resolve_many(self, relation_class, relation)
          end
        else
          field name, relation_type do
            argument :where, filter
            Graphoid::Types.resolve_one(self, relation_class, relation)
          end
        end
      end
    end
  end
end

.resolve_many(field, model, association) ⇒ Object



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

def resolve_many(field, model, association)
  field.resolve -> (obj, args, ctx) do
    filter = args["where"].to_h
    order = args["order"].to_h
    limit = args["limit"]
    skip = args["skip"]

    result = obj.send(association.name)
    result = Graphoid::Queries::Processor.execute(result, filter) if filter.present?

    if order.present?
      order = Graphoid::Queries::Processor.parse_order(obj.send(association.name), order)
      result = result.order(order)
    end

    result = result.limit(limit) if limit.present?
    result = result.skip(skip) if skip.present?

    result
  end
end

.resolve_one(field, model, association) ⇒ Object



87
88
89
90
91
92
93
94
# File 'lib/graphoid/definitions/types.rb', line 87

def resolve_one(field, model, association)
  field.resolve -> (obj, args, ctx) do
    filter = args["where"].to_h
    result = obj.send(association.name)
    result = Graphoid::Queries::Processor.execute(model.where({ id: result.id }), filter).first if filter.present? && result
    result
  end
end