graphql-rails-api 0.1.3 → 0.1.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 106d6fbb14638d87cf01f586da3eac910f9287e7
4
- data.tar.gz: 4ce1326fb1e76cb6ca8d9ea265fc6f817f08edae
2
+ SHA256:
3
+ metadata.gz: ad933edeeaa5ed8f37ea3b5ba376670976ef245fff20d33fc7fbdfdf7c5b4fda
4
+ data.tar.gz: 54fa2e6d034e4ff46124f21980e9a3aa947cbd22208872a0595a651ad0f3e403
5
5
  SHA512:
6
- metadata.gz: 233e224a5adbdbfe1e40c7cb1e21e892a1846c4df5dbd7fe25665573fb6c0c78167fdf9f71807a5cfa61609c46df8d7daacfb9637252f8bd642fca9af11e1b0f
7
- data.tar.gz: 2a5134db639e3659c46859603f9aabf912385e14d713369db9da1ee24c6955fe13e6a90b8620f409e22d708eab551f4dc9913406135070d5bc166cc2af40ed85
6
+ metadata.gz: 21cbb6dd1a71aa888b3fc3b6b0fc71ae09ead22dece2d4a1a85b61ea6f0eaf44b39e3fab8687f2bc25e08faaade7b8a1e6cdbead36b7a501d252e07349ee7976
7
+ data.tar.gz: 9e806b335391d28de6a3b5561d312730c1fdaab8e76edb501d3cdb313dbb018d79c573e1ce3b324d8256ba2db32aab20a3906ff8273339e5fdde8a0fc00e58f5
@@ -21,11 +21,24 @@ module GraphqlRailsApi
21
21
  write_channel if options.action_cable_subs?
22
22
  write_initializer
23
23
  write_require_application_rb
24
+ write_route
24
25
  write_uuid_extensions_migration if options.pg_uuid?
25
26
  end
26
27
 
27
28
  private
28
29
 
30
+ def write_route
31
+ route_file = File.read('config/routes.rb')
32
+ return if route_file.include?('graphql')
33
+ File.write(
34
+ 'config/routes.rb',
35
+ route_file.gsub(
36
+ "Rails.application.routes.draw do\n",
37
+ "Rails.application.routes.draw do\n post '/graphql', to: 'graphql#execute'\n"
38
+ )
39
+ )
40
+ end
41
+
29
42
  def write_application_record_methods
30
43
  lines_count = File.read('app/models/application_record.rb').lines.count
31
44
 
@@ -51,7 +64,7 @@ module GraphqlRailsApi
51
64
  'config/application.rb',
52
65
  File.read('config/application.rb').gsub(
53
66
  "require 'rails/all'",
54
- "require 'rails/all'\nrequire 'graphql/hydrate_query'\n"
67
+ "require 'rails/all'\nrequire 'graphql/hydrate_query'\nrequire 'graphql/visibility_hash'\n"
55
68
  )
56
69
  )
57
70
  end
@@ -329,18 +342,16 @@ module GraphqlRailsApi
329
342
  end
330
343
 
331
344
  def index
332
- Graphql::HydrateQuery.new(model.visible_for(user: @user), @context).run
345
+ Graphql::HydrateGraphqlQuery.new(model.all, @context, user: user).run
333
346
  end
334
347
 
335
348
  def show
336
- puts 'SHOW'
337
- object = Graphql::HydrateQuery.new(model.visible_for(user: @user), @context, id: params[:id]).run
349
+ object = Graphql::HydrateQuery.new(model.all, @context, user: user, id: params[:id]).run
338
350
  return not_allowed if object.blank?
339
351
  object
340
352
  end
341
353
 
342
354
  def create
343
- puts 'CREATE'
344
355
  object = model.new(params.select { |p| model.new.respond_to?(p) })
345
356
  if object.save
346
357
  object
@@ -350,7 +361,6 @@ module GraphqlRailsApi
350
361
  end
351
362
 
352
363
  def destroy
353
- puts 'DESTROY'
354
364
  object = model.find_by(id: params[:id])
355
365
  return not_allowed if write_not_allowed
356
366
  if object.destroy
@@ -361,7 +371,6 @@ module GraphqlRailsApi
361
371
  end
362
372
 
363
373
  def update
364
- puts "UPDATE, #{params}, #{object}"
365
374
  return not_allowed if write_not_allowed
366
375
  if object.update_attributes(params)
367
376
  object
@@ -1,10 +1,11 @@
1
1
  module Graphql
2
2
  class HydrateQuery
3
3
 
4
- def initialize(model, context, id: nil)
4
+ def initialize(model, context, id: nil, user: nil)
5
5
  @fields = context&.irep_node&.scoped_children&.values&.first
6
6
  @model = model
7
7
  @id = id
8
+ @user = user
8
9
  end
9
10
 
10
11
  def run
@@ -16,6 +17,7 @@ module Graphql
16
17
  res2d = pluck_to_hash_with_ids(join_model, pluckable_attributes(selectable_values))
17
18
  joins_with_root = { model_name.to_sym => remove_keys_with_nil_values(Marshal.load(Marshal.dump(hash))) }
18
19
  ir = nest(joins_with_root, res2d).first
20
+ @visibility_hash = Graphql::VisibilityHash.new(joins_with_root, @user).run
19
21
  @id ? ir_to_output(ir).first : ir_to_output(ir)
20
22
  end
21
23
 
@@ -46,18 +48,19 @@ module Graphql
46
48
  def ir_to_output(inter_result)
47
49
  model_name = inter_result&.first&.first&.first&.first&.to_s
48
50
  return [] if model_name.blank?
49
- if singular?(model_name)
50
- ir_node_to_output(inter_result.first)
51
- else
52
- inter_result.map do |ir_node|
53
- ir_node_to_output(ir_node) if ir_node
54
- end
55
- end
51
+ res = if singular?(model_name)
52
+ ir_node_to_output(inter_result.first)
53
+ else
54
+ inter_result.map { |ir_node| ir_node_to_output(ir_node) if ir_node }
55
+ end
56
+ res.compact! if res.is_a?(Array)
57
+ res
56
58
  end
57
59
 
58
60
  def ir_node_to_output(ir_node)
61
+ model_name = ir_node.keys.reject { |key| key == :results }.first.first.to_s
59
62
  t = ir_node[:results].first.each_with_object({}) do |(attribute, v), h|
60
- h[attribute.gsub(ir_node.keys.reject { |key| key == :results }.first.first.to_s.pluralize + '.', '')] = v
63
+ h[attribute.gsub(model_name.pluralize + '.', '')] = v
61
64
  end
62
65
  relations = ir_node.values&.first&.map { |e| e&.first&.first&.first&.first }
63
66
  relations.zip(ir_node[ir_node.keys.reject { |key| key == :results }&.first]).to_h.map do |key, value|
@@ -65,6 +68,7 @@ module Graphql
65
68
  t[key] = res if value
66
69
  t[key].compact! if t[key].is_a?(Array)
67
70
  end
71
+ return unless @visibility_hash[model_name.singularize.to_sym]&.include?(t['id'])
68
72
  Struct.new(*t.keys.map(&:to_sym)).new(*t.values) if !t.keys.blank? && !t.values.compact.blank?
69
73
  end
70
74
 
@@ -1,7 +1,7 @@
1
1
  module Graphql
2
2
  module Rails
3
3
  module Api
4
- VERSION = '0.1.3'.freeze
4
+ VERSION = '0.1.4'.freeze
5
5
  end
6
6
  end
7
7
  end
@@ -0,0 +1,26 @@
1
+ module Graphql
2
+ class VisibilityHash
3
+
4
+ def initialize(joins, user)
5
+ @joins = joins
6
+ @user = user
7
+ end
8
+
9
+ def run
10
+ fetch_models(@joins).each_with_object({}) do |model, hash|
11
+ hash[model.to_s.underscore.to_sym] = model.visible_for(user: @user).pluck(:id)
12
+ end
13
+ end
14
+
15
+ def fetch_models(hash)
16
+ model_names = hash.each_with_object([]) do |(k, v), keys|
17
+ keys << k
18
+ keys.concat(fetch_models(v)) if v.is_a? Hash
19
+ end.map { |m| m.to_s.singularize.camelize }.uniq
20
+ model_names.select do |m|
21
+ Object.const_defined?(m) && m.constantize.ancestors.include?(ActiveRecord::Base)
22
+ end.map(&:constantize)
23
+ end
24
+
25
+ end
26
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: graphql-rails-api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - poilon
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-05-27 00:00:00.000000000 Z
11
+ date: 2018-06-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: graphql
@@ -56,6 +56,7 @@ files:
56
56
  - lib/graphql/hydrate_query.rb
57
57
  - lib/graphql/rails/api/config.rb
58
58
  - lib/graphql/rails/api/version.rb
59
+ - lib/graphql/visibility_hash.rb
59
60
  - lib/tasks/graphql/rails/api_tasks.rake
60
61
  homepage: https://github.com/poilon/graphql-rails-api
61
62
  licenses:
@@ -77,7 +78,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
77
78
  version: '0'
78
79
  requirements: []
79
80
  rubyforge_project:
80
- rubygems_version: 2.6.14
81
+ rubygems_version: 2.7.3
81
82
  signing_key:
82
83
  specification_version: 4
83
84
  summary: Graphql rails api framework to create easily graphql api with rails