Module: Graphql::Generators::GraphqlHelpers

Included in:
CreateAllGenerator, GraphqlGenerator
Defined in:
lib/generators/graphql/graphql_helpers.rb

Instance Method Summary collapse

Instance Method Details

#add_connection_query(model) ⇒ Object



126
127
128
129
130
131
132
133
134
135
# File 'lib/generators/graphql/graphql_helpers.rb', line 126

def add_connection_query(model)
  inject_into_file 'app/graphql/types/root_level_type.rb', after: "field :id, field: GraphQL::Relay::GlobalIdField.new('RootLevel')\n" do <<-FILE
  connection :#{model.to_s.tableize}, #{model.to_s}Type.connection_type do
    resolve ->(object, args, ctx){
#{model.to_s}.all
    }
  end
  FILE
  end
end

#add_connections(model) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/generators/graphql/graphql_helpers.rb', line 91

def add_connections(model)
  associations = model.reflect_on_all_associations(:has_many)
  associations += model.reflect_on_all_associations(:has_and_belongs_to_many)

  associations.map(&:name).each do |ast|
    association_klass = model.reflect_on_association(ast).class_name
    begin
      if models.include? association_klass.classify.constantize
        if circular_finder[model.to_s].include? association_klass || association_klass == model.to_s
          inject_into_file type_path(model), before: "# End of fields\n" do <<-FILE
  connection :#{ast.to_s}, -> { #{model.reflect_on_association(ast).class_name}Type.connection_type } do
    resolve ->(#{singular_route_key(model)}, args, ctx) {
#{singular_route_key(model)}.#{ast}
    }
  end

          FILE
          end
        else
          inject_into_file type_path(model), before: "# End of fields\n" do <<-FILE
  connection :#{ast.to_s}, #{model.reflect_on_association(ast).class_name}Type.connection_type do
    resolve -> (#{singular_route_key(model)}, args, ctx) {
#{singular_route_key(model)}.#{ast}
    }
  end
          FILE
          end
        end
      end
    rescue => ex
      puts ex
    end
  end
end

#add_fields(model) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/generators/graphql/graphql_helpers.rb', line 38

def add_fields(model)
  columns = model.columns_hash
  columns.keys.each do |k|
    next if columns[k].name == 'id'

    if columns[k].type.present?
      inject_into_file type_path(model), after: "field :id, field: GraphQL::Relay::GlobalIdField.new('#{model.to_s}')\n" do <<-FILE
  field :#{columns[k].name}, types.#{type_mapper[columns[k].type]}
      FILE
      end
    end
  end
end

#add_fields_to_mutation(model) ⇒ Object



193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/generators/graphql/graphql_helpers.rb', line 193

def add_fields_to_mutation(model)
  columns = model.columns_hash
  columns.keys.each do |k|
    next if %w(id created_at updated_at).include? columns[k].name
    if columns[k].type.present?
      inject_into_file mutation_path(model), after: "name 'Update#{model.to_s}'\n" do <<-FILE
    input_field :#{columns[k].name}, types.#{type_mapper[columns[k].type]}
      FILE
      end

      inject_into_file mutation_path(model), after: "name 'Create#{model.to_s}'\n", :force => true do <<-FILE
    input_field :#{columns[k].name}, !types.#{type_mapper[columns[k].type]}
      FILE
      end
    end
  end
end

#add_methods(model) ⇒ Object



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
87
88
89
# File 'lib/generators/graphql/graphql_helpers.rb', line 52

def add_methods(model)
  associations = model.reflect_on_all_associations(:belongs_to)

  associations.map(&:name).each do |ast|
    association_klass = model.reflect_on_association(ast).class_name

    begin
      if models.include? association_klass.classify.constantize
        if circular_finder[model.to_s].include? association_klass || association_klass == model.to_s
          inject_into_file type_path(model), before: "# End of fields\n" do <<-FILE
  field :#{ast.to_s} do
    type -> { #{model.reflect_on_association(ast).class_name}Type }

    resolve -> (#{singular_route_key(model)}, args, ctx) {
#{singular_route_key(model)}.#{ast.to_s}
    }
  end

          FILE
          end
        else
          inject_into_file type_path(model), before: "# End of fields\n" do <<-FILE
  field :#{ast.to_s} do
    type -> #{model.reflect_on_association(ast).class_name}Type

    resolve -> (#{singular_route_key(model)}, args, ctx) {
#{singular_route_key(model)}.#{ast.to_s}
    }
  end
          FILE
          end
        end
      end
    rescue => ex
      puts ex
    end
  end
end

#add_mutation_query(model) ⇒ Object



211
212
213
214
215
216
217
218
219
# File 'lib/generators/graphql/graphql_helpers.rb', line 211

def add_mutation_query(model)
  inject_into_file "app/graphql/types/mutation_type.rb", after: "name 'MutationType'\n" do <<-FILE
  field :create#{model.to_s}, field: #{model.to_s}Mutations::Create.field
  field :update#{model.to_s}, field: #{model.to_s}Mutations::Update.field
  field :destroy#{model.to_s}, field: #{model.to_s}Mutations::Destroy.field

  FILE
  end
end

#circular_finderObject



237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
# File 'lib/generators/graphql/graphql_helpers.rb', line 237

def circular_finder
  circular_finder = {}

  models.each do |m|
    associations = m.reflect_on_all_associations.map(&:name)
    circular_finder[m.to_s] = [] unless circular_finder[m.to_s].present?

    associations.each do |k|
      association_klass = m.reflect_on_association(k).class_name
      circular_finder[association_klass] = [] unless circular_finder[association_klass].present?

      circular_finder[association_klass] << m.to_s
    end
  end

  @circular_finder ||= circular_finder
end

#create_mutation(model) ⇒ Object



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
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
# File 'lib/generators/graphql/graphql_helpers.rb', line 137

def create_mutation(model)
  create_file mutation_path(model), <<-FILE
module #{model.to_s}Mutations

  Create = GraphQL::Relay::Mutation.define do
    name 'Create#{model.to_s}'

    return_field :#{instance_name(model)}, #{model.to_s}Type

    resolve -> (inputs, ctx) {
root = RootLevel::STATIC
attr = inputs.keys.inject({}) do |memo, key|
  memo[key] = inputs[key] unless key == "clientMutationId"
  memo
end

#{instance_name(model)} = #{model.to_s}.create(attr)

{ #{instance_name(model)}: #{instance_name(model)} }
    }
  end

  Update = GraphQL::Relay::Mutation.define do
    name 'Update#{model.to_s}'

    input_field :id, !types.ID

    return_field :#{instance_name(model)}, #{model.to_s}Type

    resolve -> (inputs, ctx) {
#{instance_name(model)} = NodeIdentification.object_from_id((inputs[:id]), ctx)
attr = inputs.keys.inject({}) do |memo, key|
  memo[key] = inputs[key] unless key == "clientMutationId" || key == 'id'
  memo
end

#{instance_name(model)}.update(attr)
{ #{instance_name(model)}: #{instance_name(model)} }
    }
  end

  Destroy = GraphQL::Relay::Mutation.define do
    name "Destroy#{model.to_s}"

    input_field :id, !types.ID

    resolve -> (inputs, ctx) {
#{instance_name(model)} = NodeIdentification.object_from_id((inputs[:id]), ctx)
#{instance_name(model)}.destroy
{ }
    }
  end
end
  FILE
end

#create_type(model) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/generators/graphql/graphql_helpers.rb', line 23

def create_type(model)
  create_file "app/graphql/types/#{ActiveModel::Naming.singular_route_key(model)}_type.rb", <<-FILE
#{model.to_s}Type = GraphQL::ObjectType.define do
  name '#{model.to_s}'
  description '#{model.to_s} type'

  interfaces [NodeIdentification.interface]

  field :id, field: GraphQL::Relay::GlobalIdField.new('#{model.to_s}')
  # End of fields
end
  FILE
  add_fields(model)
end

#instance_name(model) ⇒ Object



233
234
235
# File 'lib/generators/graphql/graphql_helpers.rb', line 233

def instance_name(model)
  ActiveModel::Naming.singular_route_key(model)
end

#modelsObject



4
5
6
# File 'lib/generators/graphql/graphql_helpers.rb', line 4

def models
  @models ||= ActiveRecord::Base.connection.tables.map{ |x| x.classify.safe_constantize }.compact
end

#mutation_path(model) ⇒ Object



225
226
227
# File 'lib/generators/graphql/graphql_helpers.rb', line 225

def mutation_path(model)
  "app/graphql/mutations/#{singular_route_key(model)}_mutations.rb"
end

#singular_route_key(model) ⇒ Object



221
222
223
# File 'lib/generators/graphql/graphql_helpers.rb', line 221

def singular_route_key(model)
  ActiveModel::Naming.singular_route_key(model)
end

#type_mapperObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/generators/graphql/graphql_helpers.rb', line 8

def type_mapper
  @type_mapper ||= {
      integer:  'Int',
      string:   'String',
      datetime: 'String',
      boolean:  'Boolean',
      float:    'Float',
      date:     'String',
      json:     'String',
      text:     'String',
      decimal:  'Float',
      '' =>     'String',
  }
end

#type_path(model) ⇒ Object



229
230
231
# File 'lib/generators/graphql/graphql_helpers.rb', line 229

def type_path(model)
  "app/graphql/types/#{singular_route_key(model)}_type.rb"
end