ibrain-core 0.1.8 → 0.2.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +21 -7
- data/app/controllers/concerns/ibrain_handler.rb +1 -0
- data/app/controllers/ibrain/base_controller.rb +1 -9
- data/app/controllers/ibrain/core/graphql_controller.rb +73 -0
- data/app/graphql/ibrain/base_schema.rb +1 -1
- data/app/graphql/ibrain/extentions/session_required.rb +20 -0
- data/app/graphql/ibrain/policies/base_policy.rb +26 -20
- data/app/graphql/ibrain/types/base_api_connection.rb +16 -0
- data/app/graphql/ibrain/types/base_api_edge.rb +10 -0
- data/app/graphql/ibrain/types/base_api_field.rb +15 -0
- data/app/graphql/ibrain/types/base_api_object.rb +14 -0
- data/app/models/ibrain/base.rb +2 -2
- data/config/routes.rb +1 -5
- data/lib/generators/ibrain/graphql/templates/resolver.erb +1 -1
- data/lib/generators/ibrain/graphql/templates/resolvers.erb +1 -1
- data/lib/generators/ibrain/install/templates/config/initializers/ibrain.rb.tt +3 -0
- data/lib/generators/ibrain/install/templates/graphql/types/mutation_type.rb.tt +10 -2
- data/lib/generators/ibrain/install/templates/graphql/types/query_type.rb.tt +4 -2
- data/lib/ibrain/app_configuration.rb +3 -0
- data/lib/ibrain/core/controller_helpers/response.rb +1 -0
- data/lib/ibrain/core/engine.rb +1 -1
- data/lib/ibrain/core/version.rb +2 -2
- metadata +8 -4
- data/app/controllers/ibrain/graphql_controller.rb +0 -72
- data/app/graphql/ibrain/types/base_query_type.rb +0 -14
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0accd0585dbb8b751342cfbeae014e22ef41b75b56bb3aba37f2071813307344
|
4
|
+
data.tar.gz: b5e922dea8ec2515d5ee390271d04e9ba879d88d0b629f54868420d46771386e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: be6f2b3e72bdba22100743dfa74b99b001ab17f5c4ebf3cc4218ab776667fc3761ad7db01326de8ed4c0d9cf5362f35cf66bbde2260fc943e72327e81a8557b0
|
7
|
+
data.tar.gz: 47dd41b984584ca4e3bc820e9a07b18eafcbcd8312f5ccad9c03c322ca49ff56cc2bf43ac1d1360db61f87dca904cfb61edac157b6527ae94ce746e1224a2abf
|
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
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
57
|
+
users(offset: $offset, limit: $limit, filter: $filter) {
|
58
|
+
id
|
59
|
+
first_name
|
60
|
+
}
|
61
61
|
|
62
|
-
|
63
|
-
|
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
|
|
@@ -12,6 +12,7 @@ module IbrainHandler
|
|
12
12
|
rescue_from IbrainErrors::UnknownError, with: :bad_request_handler
|
13
13
|
rescue_from ActionController::InvalidAuthenticityToken, with: :unauthorized_handler
|
14
14
|
rescue_from ActiveSupport::MessageVerifier::InvalidSignature, with: :unauthorized_handler
|
15
|
+
rescue_from GraphQL::ExecutionError, with: :bad_request_handler
|
15
16
|
end
|
16
17
|
|
17
18
|
private
|
@@ -1,7 +1,7 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
module Ibrain
|
4
|
-
class BaseController <
|
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
|
@@ -0,0 +1,73 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Ibrain
|
4
|
+
module Core
|
5
|
+
class GraphqlController < ::Ibrain::BaseController
|
6
|
+
include Devise::Controllers::ScopedViews
|
7
|
+
|
8
|
+
before_action :map_user_class_to_request
|
9
|
+
|
10
|
+
helpers = %w(resource scope_name resource_name signed_in_resource
|
11
|
+
resource_class resource_params devise_mapping)
|
12
|
+
helper_method(*helpers)
|
13
|
+
|
14
|
+
def execute
|
15
|
+
query, variables, operation_name = normalize_entity
|
16
|
+
|
17
|
+
result = schema.execute(
|
18
|
+
query,
|
19
|
+
variables: variables,
|
20
|
+
context: {
|
21
|
+
session: session,
|
22
|
+
current_user: try_ibrain_current_user,
|
23
|
+
controller: self,
|
24
|
+
request: request
|
25
|
+
},
|
26
|
+
operation_name: operation_name
|
27
|
+
)
|
28
|
+
|
29
|
+
render_json_ok(result['data'], nil, result['errors'])
|
30
|
+
end
|
31
|
+
|
32
|
+
protected
|
33
|
+
|
34
|
+
def normalize_entity
|
35
|
+
query = params[:query]
|
36
|
+
operation_name = params[:operationName]
|
37
|
+
variables = prepare_variables(params[:variables])
|
38
|
+
|
39
|
+
[query, variables, operation_name]
|
40
|
+
end
|
41
|
+
|
42
|
+
# Handle variables in form data, JSON body, or a blank value
|
43
|
+
def prepare_variables(variables_param)
|
44
|
+
case variables_param
|
45
|
+
when String
|
46
|
+
if variables_param.present?
|
47
|
+
JSON.parse(variables_param) || {}
|
48
|
+
else
|
49
|
+
{}
|
50
|
+
end
|
51
|
+
when Hash
|
52
|
+
variables_param
|
53
|
+
when ActionController::Parameters
|
54
|
+
variables_param.to_unsafe_hash # GraphQLRuby will validate name and type of incoming variables.
|
55
|
+
when nil
|
56
|
+
{}
|
57
|
+
else
|
58
|
+
raise ArgumentError, "Unexpected parameter: #{variables_param}"
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
def schema
|
63
|
+
Ibrain::Config.graphql_schema.safe_constantize
|
64
|
+
end
|
65
|
+
|
66
|
+
def map_user_class_to_request
|
67
|
+
return if request.env['devise.mapping'].present?
|
68
|
+
|
69
|
+
request.env['devise.mapping'] = Ibrain.user_class
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
@@ -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) {
|
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,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(:contect).try(:fetch, :current_user, nil).blank? && options.try(:fetch, :session_required, false)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -3,26 +3,32 @@
|
|
3
3
|
module Ibrain
|
4
4
|
module Policies
|
5
5
|
class BasePolicy
|
6
|
-
IBRAIN_QUERY_RULES = {
|
7
|
-
'*': {
|
8
|
-
guard: ->(_obj, _args, _ctx) { true }
|
9
|
-
}
|
10
|
-
}
|
11
|
-
|
12
|
-
IBRAIN_MUTATION_RULES = {
|
13
|
-
'*': {
|
14
|
-
guard: ->(_obj, _args, ctx) { roles.include?(ctx[:current_user].try(:role)) }
|
15
|
-
}
|
16
|
-
}
|
17
|
-
|
18
|
-
RULES = {
|
19
|
-
'Query' => IBRAIN_QUERY_RULES,
|
20
|
-
'Mutation' => IBRAIN_MUTATION_RULES
|
21
|
-
}.freeze
|
22
|
-
|
23
6
|
class << self
|
7
|
+
def query_rules
|
8
|
+
{
|
9
|
+
'*': {
|
10
|
+
guard: ->(_obj, _args, _ctx) { false }
|
11
|
+
}
|
12
|
+
}
|
13
|
+
end
|
14
|
+
|
15
|
+
def mutation_rules
|
16
|
+
{
|
17
|
+
'*': {
|
18
|
+
guard: ->(_obj, _args, _ctx) { false }
|
19
|
+
}
|
20
|
+
}
|
21
|
+
end
|
22
|
+
|
23
|
+
def rules
|
24
|
+
{
|
25
|
+
'Types::QueryType' => query_rules,
|
26
|
+
'Types::MutationType' => mutation_rules
|
27
|
+
}.freeze
|
28
|
+
end
|
29
|
+
|
24
30
|
def roles
|
25
|
-
Ibrain
|
31
|
+
Ibrain.user_class.roles.keys
|
26
32
|
end
|
27
33
|
|
28
34
|
def has_permission?(current_user, resource)
|
@@ -33,11 +39,11 @@ module Ibrain
|
|
33
39
|
end
|
34
40
|
|
35
41
|
def guard(type, field)
|
36
|
-
|
42
|
+
rules.dig(type.name, field, :guard)
|
37
43
|
end
|
38
44
|
|
39
45
|
def not_authorized_handler(type, field)
|
40
|
-
|
46
|
+
rules.dig(type, field, :not_authorized) || rules.dig(type, :*, :not_authorized)
|
41
47
|
end
|
42
48
|
end
|
43
49
|
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,15 @@
|
|
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, **kwargs, &block)
|
9
|
+
super(*args, camelize: false, **kwargs, &block)
|
10
|
+
|
11
|
+
extension(Ibrain::Extentions::SessionRequired, session_required: session_required) if session_required
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
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
|
data/app/models/ibrain/base.rb
CHANGED
@@ -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,9 +1,5 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
Ibrain::Core::Engine.routes.draw do
|
4
|
-
|
5
|
-
post '/api/graphql', to: 'graphql#execute'
|
6
|
-
else
|
7
|
-
post "/api/#{::Ibrain::Config.api_version.downcase}/graphql", controller: 'graphql', action: 'execute'
|
8
|
-
end
|
4
|
+
match "/", controller: 'graphql', action: 'execute', via: [:options, :post]
|
9
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
|
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
|
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::
|
4
|
+
class MutationType < Ibrain::Types::BaseApiObject
|
5
5
|
description 'Define all mutation for client'
|
6
6
|
|
7
7
|
# TODO: remove me
|
8
|
-
|
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::
|
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 :
|
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
|
@@ -16,6 +16,7 @@ module Ibrain
|
|
16
16
|
def render_json_error(error, status)
|
17
17
|
e_message = error.try(:record).try(:errors).try(:full_messages).try(:first)
|
18
18
|
e_message = error.try(:message) if e_message.blank?
|
19
|
+
e_message = error.try(:details) if e_message.blank?
|
19
20
|
|
20
21
|
backtrace = error.try(:backtrace).try(:join, "\n")
|
21
22
|
|
data/lib/ibrain/core/engine.rb
CHANGED
data/lib/ibrain/core/version.rb
CHANGED
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.
|
4
|
+
version: 0.2.2
|
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-
|
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
|
@@ -183,9 +183,10 @@ files:
|
|
183
183
|
- app/controllers/concerns/ibrain_errors.rb
|
184
184
|
- app/controllers/concerns/ibrain_handler.rb
|
185
185
|
- app/controllers/ibrain/base_controller.rb
|
186
|
-
- app/controllers/ibrain/graphql_controller.rb
|
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/session_required.rb
|
189
190
|
- app/graphql/ibrain/interfaces/base_interface.rb
|
190
191
|
- app/graphql/ibrain/interfaces/person_interface.rb
|
191
192
|
- app/graphql/ibrain/interfaces/record_interface.rb
|
@@ -198,6 +199,10 @@ files:
|
|
198
199
|
- app/graphql/ibrain/resolvers/base_resolver.rb
|
199
200
|
- app/graphql/ibrain/types/aggregate_type.rb
|
200
201
|
- app/graphql/ibrain/types/attribute_type.rb
|
202
|
+
- app/graphql/ibrain/types/base_api_connection.rb
|
203
|
+
- app/graphql/ibrain/types/base_api_edge.rb
|
204
|
+
- app/graphql/ibrain/types/base_api_field.rb
|
205
|
+
- app/graphql/ibrain/types/base_api_object.rb
|
201
206
|
- app/graphql/ibrain/types/base_argument.rb
|
202
207
|
- app/graphql/ibrain/types/base_connection.rb
|
203
208
|
- app/graphql/ibrain/types/base_edge.rb
|
@@ -207,7 +212,6 @@ files:
|
|
207
212
|
- app/graphql/ibrain/types/base_interface.rb
|
208
213
|
- app/graphql/ibrain/types/base_node.rb
|
209
214
|
- app/graphql/ibrain/types/base_object.rb
|
210
|
-
- app/graphql/ibrain/types/base_query_type.rb
|
211
215
|
- app/graphql/ibrain/types/base_scalar.rb
|
212
216
|
- app/graphql/ibrain/types/base_union.rb
|
213
217
|
- app/graphql/ibrain/types/filter_type.rb
|
@@ -1,72 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module Ibrain
|
4
|
-
class GraphqlController < Ibrain::BaseController
|
5
|
-
include Devise::Controllers::ScopedViews
|
6
|
-
|
7
|
-
before_action :authenticate_user!, unless: :skip_operations
|
8
|
-
before_action :map_user_class_to_request
|
9
|
-
|
10
|
-
helpers = %w(resource scope_name resource_name signed_in_resource
|
11
|
-
resource_class resource_params devise_mapping)
|
12
|
-
helper_method(*helpers)
|
13
|
-
|
14
|
-
def execute
|
15
|
-
query, variables, operation_name = normalize_entity
|
16
|
-
|
17
|
-
result = schema.execute(
|
18
|
-
query,
|
19
|
-
variables: variables,
|
20
|
-
context: {
|
21
|
-
session: session,
|
22
|
-
current_user: try_ibrain_current_user,
|
23
|
-
controller: self,
|
24
|
-
request: request
|
25
|
-
},
|
26
|
-
operation_name: operation_name
|
27
|
-
)
|
28
|
-
|
29
|
-
render_json_ok(result['data'], nil, result['errors'])
|
30
|
-
end
|
31
|
-
|
32
|
-
protected
|
33
|
-
|
34
|
-
def normalize_entity
|
35
|
-
query = params[:query]
|
36
|
-
operation_name = params[:operationName]
|
37
|
-
variables = prepare_variables(params[:variables])
|
38
|
-
|
39
|
-
[query, variables, operation_name]
|
40
|
-
end
|
41
|
-
|
42
|
-
# Handle variables in form data, JSON body, or a blank value
|
43
|
-
def prepare_variables(variables_param)
|
44
|
-
case variables_param
|
45
|
-
when String
|
46
|
-
if variables_param.present?
|
47
|
-
JSON.parse(variables_param) || {}
|
48
|
-
else
|
49
|
-
{}
|
50
|
-
end
|
51
|
-
when Hash
|
52
|
-
variables_param
|
53
|
-
when ActionController::Parameters
|
54
|
-
variables_param.to_unsafe_hash # GraphQLRuby will validate name and type of incoming variables.
|
55
|
-
when nil
|
56
|
-
{}
|
57
|
-
else
|
58
|
-
raise ArgumentError, "Unexpected parameter: #{variables_param}"
|
59
|
-
end
|
60
|
-
end
|
61
|
-
|
62
|
-
def schema
|
63
|
-
Ibrain::Config.graphql_schema.safe_constantize
|
64
|
-
end
|
65
|
-
|
66
|
-
def map_user_class_to_request
|
67
|
-
return if request.env['devise.mapping'].present?
|
68
|
-
|
69
|
-
request.env['devise.mapping'] = Ibrain.user_class
|
70
|
-
end
|
71
|
-
end
|
72
|
-
end
|
@@ -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
|