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
|