ibrain-core 0.1.9 → 0.2.3

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
2
  SHA256:
3
- metadata.gz: 197d83360e5b4281c1580976c1e5fc56617cfbc62cb19771935c4e1d11eded46
4
- data.tar.gz: 824c0ca8095c991c4ce31dac6e6fbad27ac48f8ab6855bb4165de78bf9cb7b29
3
+ metadata.gz: 9c01fb318f3aa91812d1bfa1e71f956260a51929f05bfe5a6ae642149f8219ba
4
+ data.tar.gz: d15d09f9e9b6761d2ab5424a04b1c12185898493034683a5394a482041e59f73
5
5
  SHA512:
6
- metadata.gz: f17faebbf900b04bbd9deb8ac26ba3a92efe935294a97bdf045f39fbd168d5a5db00638d24b01663124dc710329454453c37631d7983a86b629f48b105daf475
7
- data.tar.gz: d91616aa9dc3eba73f3a9a21dba11a5eea20cfec45abc17e0e47e0254678ecf1b7ab5df53e109b49e1502a57eaf889f32a88e4136e2582e997d14b08506d988d
6
+ metadata.gz: 4fd557d32f5ccdedcf916bccc2b3fd9d9af87a344276b9142946b9008b5b9681343801e80579a3564055b78c784cadf864a02faf014f1adddb423ad8d0c9b4da
7
+ data.tar.gz: b2c18c0af1ab23dab608e93e8a6634d23a5c90d21eb734320aabf9a6b3e4f99afc01557b5c7633b5cf10eecb1ec0a827e133bdd65e3124544bd6cd83566ae5a7
data/README.md CHANGED
@@ -54,20 +54,34 @@ bundle exec rails generate ibrain:graphql:resolvers users --model=User
54
54
  For pagination please using aggregate body query, something like
55
55
  ```
56
56
  query users($offset: Int, $limit: Int, $filter: Filter) {
57
- users(offset: $offset, limit: $limit, filter: $filter) {
58
- id
59
- first_name
60
- }
57
+ users(offset: $offset, limit: $limit, filter: $filter) {
58
+ id
59
+ first_name
60
+ }
61
61
 
62
- users_aggregate(filter: $filter) {
63
- total_count
64
- }
62
+ users_aggregate(filter: $filter) {
63
+ total_count
64
+ }
65
65
  }
66
66
  ```
67
67
  To generate graphql mutation to insert, update, delete user
68
68
  ```bash
69
69
  bundle exec rails generate ibrain:graphql:mutation insert_user --model=User
70
70
  ```
71
+
72
+ Default all operation will be rejected if you not have Authorization Token at request header, so to skip authenticate please change `parent_controller` at `ibrain.rb`
73
+ ```
74
+ config.parent_controller = "<Your parent controller>"
75
+ ```
76
+ then create method skip_operations at this parent_controller
77
+ ```
78
+ class ApplicationController < BaseController::API
79
+ def skip_operations
80
+ %w[sign_in].include?(operation_name)
81
+ end
82
+ end
83
+ ```
84
+
71
85
  ## Contributing
72
86
  Contribution directions go here.
73
87
 
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Ibrain
4
- class BaseController < ActionController::API
4
+ class BaseController < Ibrain::Config.parent_controller.constantize
5
5
  include ActionController::Helpers
6
6
  include Ibrain::Core::ControllerHelpers::Response
7
7
  include Ibrain::Core::ControllerHelpers::StrongParameters
@@ -13,14 +13,6 @@ module Ibrain
13
13
 
14
14
  protected
15
15
 
16
- def operation_name
17
- params[:operationName]
18
- end
19
-
20
- def skip_operations
21
- %w[sign_in].include?(operation_name)
22
- end
23
-
24
16
  def cryptor
25
17
  Ibrain::Encryptor.new
26
18
  end
@@ -5,7 +5,6 @@ module Ibrain
5
5
  class GraphqlController < ::Ibrain::BaseController
6
6
  include Devise::Controllers::ScopedViews
7
7
 
8
- before_action :authenticate_user!, unless: :skip_operations
9
8
  before_action :map_user_class_to_request
10
9
 
11
10
  helpers = %w(resource scope_name resource_name signed_in_resource
@@ -6,7 +6,7 @@ module Ibrain
6
6
 
7
7
  use GraphQL::Guard.new(
8
8
  policy_object: ::Ibrain::Config.graphql_policy.safe_constantize,
9
- not_authorized: ->(type, field) { raise IbrainErrors::UnknownError.new("Not authorized to access #{type}.#{field}") }
9
+ not_authorized: ->(type, field) { raise IbrainErrors::PermissionError.new("You not have permission to access #{type}.#{field}") }
10
10
  )
11
11
 
12
12
  # Union and Interface Resolution
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Ibrain
4
+ module Extentions
5
+ class Roles < GraphQL::Schema::FieldExtension
6
+ def after_resolve(object:, arguments:, **rest)
7
+ raise IbrainErrors::PermissionError.new("You not have permission to access #{field&.name}") if is_invalid_role(object)
8
+
9
+ # yield the current time as `memo`
10
+ yield(object, arguments, rest)
11
+ end
12
+
13
+ private
14
+
15
+ def is_invalid_role(object)
16
+ roles = options.try(:fetch, :roles, [])
17
+ current_user = object.try(:context).try(:fetch, :current_user, nil)
18
+ role = current_user.try(:role) || current_user.try(:graphql_role)
19
+
20
+ return if roles.blank?
21
+ return true if current_user.blank?
22
+ return false if roles.include?(role)
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Ibrain
4
+ module Extentions
5
+ class SessionRequired < GraphQL::Schema::FieldExtension
6
+ def resolve(object:, arguments:, **rest)
7
+ raise ActionController::InvalidAuthenticityToken, I18n.t('ibrain.errors.session.invalid_session') if is_invalid_session(object)
8
+
9
+ # yield the current time as `memo`
10
+ yield(object, arguments, rest)
11
+ end
12
+
13
+ private
14
+
15
+ def is_invalid_session(object)
16
+ object.try(:context).try(:fetch, :current_user, nil).blank? && options.try(:fetch, :session_required, false)
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Ibrain
4
+ module Types
5
+ class BaseApiConnection < Types::BaseObject
6
+ # add `nodes` and `pageInfo` fields, as well as `edge_type(...)` and `node_nullable(...)` overrides
7
+ include GraphQL::Types::Relay::ConnectionBehaviors
8
+
9
+ field :total_count, Integer, null: false, camelize: false
10
+
11
+ def total_count
12
+ object.items.size
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Ibrain
4
+ module Types
5
+ class BaseApiEdge < Types::BaseObject
6
+ # add `node` and `cursor` fields, as well as `node_type(...)` override
7
+ include GraphQL::Types::Relay::EdgeBehaviors
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Ibrain
4
+ module Types
5
+ class BaseApiField < GraphQL::Schema::Field
6
+ argument_class ::Ibrain::Types::BaseArgument
7
+
8
+ def initialize(*args, session_required: true, roles: nil, **kwargs, &block)
9
+ super(*args, camelize: false, **kwargs, &block)
10
+
11
+ extension(Ibrain::Extentions::SessionRequired, session_required: session_required) if session_required
12
+ extension(Ibrain::Extentions::Roles, roles: roles) if roles
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Ibrain
4
+ module Types
5
+ class BaseApiObject < GraphQL::Schema::Object
6
+ include GraphQL::Relay::Node
7
+
8
+ edge_type_class(Ibrain::Types::BaseApiEdge)
9
+ connection_type_class(Ibrain::Types::BaseApiConnection)
10
+
11
+ field_class Ibrain::Types::BaseApiField
12
+ end
13
+ end
14
+ end
@@ -5,10 +5,11 @@ module Ibrain
5
5
  class BaseField < GraphQL::Schema::Field
6
6
  argument_class ::Ibrain::Types::BaseArgument
7
7
 
8
- def initialize(*args, default_value: nil, **kwargs, &block)
8
+ def initialize(*args, default_value: nil, roles: nil, **kwargs, &block)
9
9
  super(*args, camelize: false, **kwargs, &block)
10
10
 
11
11
  extension(::Ibrain::Extentions::DefaultValue, default_value: default_value) unless default_value.nil?
12
+ extension(Ibrain::Extentions::Roles, roles: roles) if roles
12
13
  end
13
14
  end
14
15
  end
@@ -1,10 +1,10 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- IGNORE_ATTRIBUTES = %w(id created_at updated_at)
4
-
5
3
  class Ibrain::Base < Ibrain::ApplicationRecord
6
4
  include ActionView::Helpers::DateHelper
7
5
 
6
+ IGNORE_ATTRIBUTES = %w(id created_at updated_at)
7
+
8
8
  self.abstract_class = true
9
9
 
10
10
  def string_id
data/config/routes.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  Ibrain::Core::Engine.routes.draw do
4
- post "/", controller: 'graphql', action: 'execute'
4
+ match "/", controller: 'graphql', action: 'execute', via: [:options, :post]
5
5
  end
@@ -7,7 +7,7 @@ module Resolvers
7
7
  # description
8
8
 
9
9
  # TODO: define return fields
10
- type Types::Objects::<%= model_name.capitalize %>Type, null: false, description: 'Define data type will be response to client'
10
+ type Types::Objects::<%= model_name.capitalize %>Type, null: false
11
11
 
12
12
  argument :id, ID, required: false, description: 'TODO: describe about this argument'
13
13
 
@@ -7,7 +7,7 @@ module Resolvers
7
7
  # description
8
8
 
9
9
  # TODO: define return fields
10
- type [Types::Objects::<%= model_name.capitalize %>Type], null: false, description: 'Define data type will be response to client'
10
+ type [Types::Objects::<%= model_name.capitalize %>Type], null: false
11
11
 
12
12
  # TODO: define resolve method
13
13
  def resolve(args)
@@ -41,6 +41,9 @@ Ibrain.config do |config|
41
41
 
42
42
  # Graphql Encryptor key
43
43
  # config.ibrain_encryptor_key = Rails.application.secrets.secret_key_base.byteslice(0..31)
44
+
45
+ # Parent controller
46
+ # config.parent_controller = 'ActionController::API'
44
47
  end
45
48
 
46
49
  <% if defined?(Ibrain::Api::Engine) -%>
@@ -1,14 +1,22 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Types
4
- class MutationType < Ibrain::Types::BaseObject
4
+ class MutationType < Ibrain::Types::BaseApiObject
5
5
  description 'Define all mutation for client'
6
6
 
7
7
  # TODO: remove me
8
- field :test_field, String, null: false,
8
+ # Default session_required is true, set false if yout want to skip authenticated
9
+ field :test_authenticated_field, , String, null: false,
9
10
  description: 'An example field added by the generator'
11
+
12
+ field :test_field, String, null: false,
13
+ description: 'An example field added by the generator', session_required: false
10
14
  def test_field
11
15
  'Hello World'
12
16
  end
17
+
18
+ def test_authenticated_field
19
+ 'Im logged in!'
20
+ end
13
21
  end
14
22
  end
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Types
4
- class QueryType < Ibrain::Types::BaseObject
4
+ class QueryType < Ibrain::Types::BaseApiObject
5
5
  description 'Define all resolver for client'
6
6
 
7
7
  # Add `node(id: ID!) and `nodes(ids: [ID!]!)`
@@ -9,9 +9,11 @@ module Types
9
9
  include GraphQL::Types::Relay::HasNodesField
10
10
 
11
11
  # Add root-level fields here.
12
+ # Default session_required is true, set false if yout want to skip authenticated
12
13
  # They will be entry points for queries on your schema.
13
14
 
14
15
  # Example with user resolvers
15
- # field :users, resolver: Resolvers::UsersResolver
16
+ # field :authenticated_users, resolver: Resolvers::UsersResolver
17
+ # field :users, resolver: Resolvers::UsersResolver, session_required: false
16
18
  end
17
19
  end
@@ -48,6 +48,9 @@ module Ibrain
48
48
  # Graphql Encryptor key
49
49
  preference :ibrain_encryptor_key, :string, default: nil
50
50
 
51
+ # Parent controller
52
+ preference :parent_controller, :string, default: 'ActionController::API'
53
+
51
54
  def static_model_preferences
52
55
  @static_model_preferences ||= Ibrain::Preferences::StaticModelPreferences.new
53
56
  end
@@ -1,14 +1,14 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Ibrain
4
- VERSION = "0.1.9"
4
+ VERSION = "0.2.3"
5
5
 
6
6
  def self.ibrain_version
7
7
  VERSION
8
8
  end
9
9
 
10
10
  def self.previous_ibrain_minor_version
11
- '0.1.8'
11
+ '0.2.2'
12
12
  end
13
13
 
14
14
  def self.ibrain_gem_version
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ibrain-core
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.9
4
+ version: 0.2.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tai Nguyen Van
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-01-05 00:00:00.000000000 Z
11
+ date: 2022-01-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord-session_store
@@ -186,6 +186,8 @@ files:
186
186
  - app/controllers/ibrain/core/graphql_controller.rb
187
187
  - app/graphql/ibrain/base_schema.rb
188
188
  - app/graphql/ibrain/extentions/default_value.rb
189
+ - app/graphql/ibrain/extentions/roles.rb
190
+ - app/graphql/ibrain/extentions/session_required.rb
189
191
  - app/graphql/ibrain/interfaces/base_interface.rb
190
192
  - app/graphql/ibrain/interfaces/person_interface.rb
191
193
  - app/graphql/ibrain/interfaces/record_interface.rb
@@ -198,6 +200,10 @@ files:
198
200
  - app/graphql/ibrain/resolvers/base_resolver.rb
199
201
  - app/graphql/ibrain/types/aggregate_type.rb
200
202
  - app/graphql/ibrain/types/attribute_type.rb
203
+ - app/graphql/ibrain/types/base_api_connection.rb
204
+ - app/graphql/ibrain/types/base_api_edge.rb
205
+ - app/graphql/ibrain/types/base_api_field.rb
206
+ - app/graphql/ibrain/types/base_api_object.rb
201
207
  - app/graphql/ibrain/types/base_argument.rb
202
208
  - app/graphql/ibrain/types/base_connection.rb
203
209
  - app/graphql/ibrain/types/base_edge.rb
@@ -207,7 +213,6 @@ files:
207
213
  - app/graphql/ibrain/types/base_interface.rb
208
214
  - app/graphql/ibrain/types/base_node.rb
209
215
  - app/graphql/ibrain/types/base_object.rb
210
- - app/graphql/ibrain/types/base_query_type.rb
211
216
  - app/graphql/ibrain/types/base_scalar.rb
212
217
  - app/graphql/ibrain/types/base_union.rb
213
218
  - app/graphql/ibrain/types/filter_type.rb
@@ -1,14 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Ibrain
4
- module Types
5
- class BaseQueryType < Types::BaseObject
6
- # Add `node(id: ID!) and `nodes(ids: [ID!]!)`
7
- include GraphQL::Types::Relay::HasNodeField
8
- include GraphQL::Types::Relay::HasNodesField
9
-
10
- # Add root-level fields here.
11
- # They will be entry points for queries on your schema.
12
- end
13
- end
14
- end