graphql-api 0.1.4 → 0.1.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 477daabc28be110eb038b74769ba03c0aafa0362
4
- data.tar.gz: fbe73ba70e1eaf7f31c3b6e63eeb6c3f0f0d5be2
3
+ metadata.gz: 95f514ccff92debe339818cb597bb3be03a46537
4
+ data.tar.gz: 7032b93c8e4c5abefdf09e434ad01ef9f6c2fb01
5
5
  SHA512:
6
- metadata.gz: ad96e49af75b0e1e957d681b07e0654da9f92bddcc9f191e373ac97cb80bc2398cdd346ef229e772a46210d0a176515cdfbe845e072fc9fbbba53cc10a212e55
7
- data.tar.gz: 63d532a93ea3d90fe477d8111e257831ebbc25bce61b840208c8b313d75f13710dd01adba7feae304eceaa6ebc2156ffc34f9d6aa077869115a1e34719d85ca0
6
+ metadata.gz: 207a89f484b7dab769130910326067f0fa08bc0b83b843c28cb577ae952c631b3ce024c5c2b5e160b43107bc82870b2c53567432fa6b4897add62a77e6addb93
7
+ data.tar.gz: bf7290f3e1397193d4d14edc4d456508f4270dc8195bb74701428475a9b27f6f576490947de824470d786f437a95720de66d607ca0e178ae357d0c496b3596ad
data/README.md CHANGED
@@ -149,6 +149,17 @@ GraphQL-Api will load in all models, query objects and commands from the rails
149
149
  app directory automatically. If you store these in a different location
150
150
  you can pass them in directly to the new command.
151
151
 
152
+ ### Policy Objects
153
+
154
+ Policy objects can be instantiated for accessing models. These objects
155
+ should meet the following interface methods.
156
+
157
+ ##### CRUD Policies
158
+
159
+ The policy object should return true or false methods for all of the crud
160
+ options allowing the mutation to take place, ie
161
+
162
+
152
163
  ### Model Objects
153
164
 
154
165
  Model objects are the core return value from GraphQL-Api. They can be a plain
@@ -0,0 +1,37 @@
1
+ require "graphql/api/unauthorized_exception"
2
+
3
+ module GraphQL::Api
4
+ class Policy
5
+ attr_reader :ctx, :model
6
+
7
+ def initialize(ctx, model)
8
+ @model = model
9
+ @ctx = ctx
10
+ end
11
+
12
+ def user
13
+ ctx[:current_user]
14
+ end
15
+
16
+ def create?
17
+ true
18
+ end
19
+
20
+ def update?
21
+ true
22
+ end
23
+
24
+ def destroy?
25
+ true
26
+ end
27
+
28
+ def read?
29
+ true
30
+ end
31
+
32
+ def unauthorized!(msg=nil)
33
+ raise UnauthorizedException.new(msg)
34
+ end
35
+
36
+ end
37
+ end
@@ -5,10 +5,21 @@ module GraphQL::Api
5
5
  def initialize(model, name)
6
6
  @model = model
7
7
  @name = name
8
+ @policy_class = "#{model.name}Policy".safe_constantize
8
9
  end
9
10
 
10
11
  def call(obj, args, ctx)
11
- if obj.respond_to?("access_#{@name}?")
12
+ if @policy_class
13
+ policy = @policy_class.new(ctx[:current_user], obj, ctx)
14
+ return nil unless policy.read?
15
+
16
+ if policy.respond_to?("access_#{@name}?")
17
+ obj.send(@name) if policy.send("access_#{@name}?")
18
+ else
19
+ obj.send(@name)
20
+ end
21
+ obj.send(@name) if policy.respond_to?("access_#{@name}?")
22
+ elsif obj.respond_to?("access_#{@name}?")
12
23
  obj.send(@name) if obj.send("access_#{@name}?", ctx)
13
24
  else
14
25
  obj.send(@name)
@@ -2,13 +2,25 @@ module GraphQL::Api
2
2
  module Resolvers
3
3
  class ModelCreateMutation
4
4
 
5
- def initialize(model)
5
+ def initialize(model, policy=nil)
6
6
  @model = model
7
+ @policy_class = "#{model.name}Policy".safe_constantize
7
8
  end
8
9
 
9
10
  def call(inputs, ctx)
11
+ if @policy_class
12
+ policy = @policy_class.new(ctx[:current_user], nil, ctx)
13
+ unless policy.create?
14
+ return {key => nil}
15
+ end
16
+ end
17
+
10
18
  item = @model.create!(inputs.to_h)
11
- {@model.name.underscore.to_sym => item}
19
+ {key => item}
20
+ end
21
+
22
+ def key
23
+ @model.name.underscore.to_sym
12
24
  end
13
25
 
14
26
  end
@@ -4,12 +4,25 @@ module GraphQL::Api
4
4
 
5
5
  def initialize(model)
6
6
  @model = model
7
+ @policy_class = "#{model.name}Policy".safe_constantize
7
8
  end
8
9
 
9
10
  def call(inputs, ctx)
10
11
  item = @model.find(inputs[:id])
12
+
13
+ if @policy_class
14
+ policy = @policy_class.new(ctx[:current_user], item, ctx)
15
+ unless policy.destroy?
16
+ return {key => nil}
17
+ end
18
+ end
19
+
11
20
  item.destroy!
12
- {"#{@model.name.underscore.to_sym}_id".to_sym => inputs[:id]}
21
+ {key => item}
22
+ end
23
+
24
+ def key
25
+ @model.name.underscore.to_sym
13
26
  end
14
27
 
15
28
  end
@@ -4,14 +4,24 @@ module GraphQL::Api
4
4
 
5
5
  def initialize(model)
6
6
  @model = model
7
+ @policy_class = "#{model.name}Policy".safe_constantize
7
8
  end
8
9
 
9
10
  def call(obj, args, ctx)
10
11
  if @model.respond_to?(:graph_find)
11
- @model.graph_find(args, ctx)
12
+ item = @model.graph_find(args, ctx)
12
13
  else
13
- @model.find_by!(args.to_h)
14
+ item = @model.find_by!(args.to_h)
14
15
  end
16
+
17
+ if @policy_class
18
+ policy = @policy_class.new(ctx[:current_user], item, ctx)
19
+ unless policy.read?
20
+ return nil
21
+ end
22
+ end
23
+
24
+ item
15
25
  end
16
26
 
17
27
  end
@@ -4,12 +4,25 @@ module GraphQL::Api
4
4
 
5
5
  def initialize(model)
6
6
  @model = model
7
+ @policy_class = "#{model.name}Policy".safe_constantize
7
8
  end
8
9
 
9
10
  def call(inputs, ctx)
10
11
  item = @model.find(inputs[:id])
12
+
13
+ if @policy_class
14
+ policy = @policy_class.new(ctx[:current_user], item, ctx)
15
+ unless policy.destroy?
16
+ return {key => nil}
17
+ end
18
+ end
19
+
11
20
  item.update!(inputs.to_h)
12
- {@model.name.underscore.to_sym => item}
21
+ {key => item}
22
+ end
23
+
24
+ def key
25
+ @model.name.underscore.to_sym
13
26
  end
14
27
 
15
28
  end
@@ -139,13 +139,15 @@ module GraphQL::Api
139
139
  def delete_mutation(model_class)
140
140
  return nil unless model_class < ActiveRecord::Base
141
141
 
142
+ object_types = @types
143
+
142
144
  GraphQL::Relay::Mutation.define do
143
145
  name "Delete#{model_class.name}"
144
146
  description "Delete #{model_class.name}"
145
147
 
146
148
  input_field :id, !types.ID
147
149
 
148
- return_field "#{model_class.name.underscore}_id".to_sym, types.ID
150
+ return_field model_class.name.underscore.to_sym, object_types[model_class]
149
151
  resolve Resolvers::ModelDeleteMutation.new(model_class)
150
152
  end
151
153
  end
@@ -0,0 +1,4 @@
1
+ module GraphQL::Api
2
+ class UnauthorizedException < Exception
3
+ end
4
+ end
@@ -1,5 +1,5 @@
1
1
  module GraphQL
2
2
  module Api
3
- VERSION = '0.1.4'
3
+ VERSION = '0.1.5'
4
4
  end
5
5
  end
data/lib/graphql/api.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  require "graphql/api/version"
2
+ require "graphql/api/policy"
2
3
  require "graphql/api/schema"
3
4
 
4
5
  module GraphQL
metadata CHANGED
@@ -1,47 +1,41 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: graphql-api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Colin Walker
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-10-03 00:00:00.000000000 Z
11
+ date: 2016-11-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - "~>"
18
- - !ruby/object:Gem::Version
19
- version: 5.0.0
20
17
  - - ">="
21
18
  - !ruby/object:Gem::Version
22
- version: 5.0.0.1
19
+ version: 4.2.6
23
20
  type: :runtime
24
21
  prerelease: false
25
22
  version_requirements: !ruby/object:Gem::Requirement
26
23
  requirements:
27
- - - "~>"
28
- - !ruby/object:Gem::Version
29
- version: 5.0.0
30
24
  - - ">="
31
25
  - !ruby/object:Gem::Version
32
- version: 5.0.0.1
26
+ version: 4.2.6
33
27
  - !ruby/object:Gem::Dependency
34
28
  name: graphql
35
29
  requirement: !ruby/object:Gem::Requirement
36
30
  requirements:
37
- - - "~>"
31
+ - - '='
38
32
  - !ruby/object:Gem::Version
39
33
  version: 0.19.0
40
34
  type: :runtime
41
35
  prerelease: false
42
36
  version_requirements: !ruby/object:Gem::Requirement
43
37
  requirements:
44
- - - "~>"
38
+ - - '='
45
39
  - !ruby/object:Gem::Version
46
40
  version: 0.19.0
47
41
  - !ruby/object:Gem::Dependency
@@ -71,6 +65,7 @@ files:
71
65
  - lib/graphql/api.rb
72
66
  - lib/graphql/api/command_type.rb
73
67
  - lib/graphql/api/helpers.rb
68
+ - lib/graphql/api/policy.rb
74
69
  - lib/graphql/api/query_type.rb
75
70
  - lib/graphql/api/resolvers/command_mutation.rb
76
71
  - lib/graphql/api/resolvers/field.rb
@@ -82,6 +77,7 @@ files:
82
77
  - lib/graphql/api/resolvers/query_object_query.rb
83
78
  - lib/graphql/api/schema.rb
84
79
  - lib/graphql/api/schema_error.rb
80
+ - lib/graphql/api/unauthorized_exception.rb
85
81
  - lib/graphql/api/version.rb
86
82
  homepage: https://github.com/coldog/graphql-api
87
83
  licenses: