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



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
86
# File 'lib/graphoid/definitions/types.rb', line 9

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] || send(method_name)
          end
        end
      end
    end

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

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

      relation_type = LIST[relation_class]
      unless relation_type
        warn "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
            send(relation.name)
          end
        end
      end

      next unless relation_type

      filter = Graphoid::Filters::LIST[relation_class]
      order  = Graphoid::Sorter::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

.resolve_many(field, _model, association) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/graphoid/definitions/types.rb', line 100

def resolve_many(field, _model, association)
  field.resolve lambda { |obj, args, _ctx|
    filter = args['where'].to_h
    order = args['order'].to_h
    limit = args['limit']
    skip = args['skip']

    processor = Graphoid::Queries::Processor

    result = obj.send(association.name)
    result = processor.execute(result, filter) if filter.present?

    if order.present?
      order = 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

.resolve_one(field, model, association) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
# File 'lib/graphoid/definitions/types.rb', line 88

def resolve_one(field, model, association)
  field.resolve lambda { |obj, args, _ctx|
    filter = args['where'].to_h
    result = obj.send(association.name)
    processor = Graphoid::Queries::Processor
    if filter.present? && result
      result = processor.execute(model.where(id: result.id), filter).first
    end
    result
  }
end