graphql_devise 0.11.4 → 0.13.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (172) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +2 -1
  3. data/.travis.yml +7 -0
  4. data/Appraisals +14 -0
  5. data/CHANGELOG.md +56 -0
  6. data/Gemfile +2 -0
  7. data/README.md +219 -20
  8. data/Rakefile +2 -0
  9. data/app/controllers/graphql_devise/application_controller.rb +6 -5
  10. data/app/controllers/graphql_devise/concerns/set_user_by_token.rb +31 -0
  11. data/app/controllers/graphql_devise/graphql_controller.rb +4 -0
  12. data/app/helpers/graphql_devise/application_helper.rb +2 -0
  13. data/app/helpers/graphql_devise/mailer_helper.rb +4 -2
  14. data/app/models/graphql_devise/concerns/model.rb +10 -0
  15. data/app/views/graphql_devise/mailer/confirmation_instructions.html.erb +1 -1
  16. data/app/views/graphql_devise/mailer/reset_password_instructions.html.erb +1 -1
  17. data/config/locales/en.yml +1 -0
  18. data/config/routes.rb +4 -0
  19. data/graphql_devise.gemspec +6 -4
  20. data/lib/generators/graphql_devise/install_generator.rb +30 -5
  21. data/lib/graphql_devise.rb +26 -10
  22. data/lib/graphql_devise/concerns/controller_methods.rb +2 -0
  23. data/lib/graphql_devise/default_operations/mutations.rb +8 -6
  24. data/lib/graphql_devise/default_operations/resolvers.rb +4 -2
  25. data/lib/graphql_devise/engine.rb +2 -0
  26. data/lib/graphql_devise/errors/authentication_error.rb +9 -0
  27. data/lib/graphql_devise/{detailed_user_error.rb → errors/detailed_user_error.rb} +3 -1
  28. data/lib/graphql_devise/errors/error_codes.rb +8 -0
  29. data/lib/graphql_devise/errors/execution_error.rb +6 -0
  30. data/lib/graphql_devise/{user_error.rb → errors/user_error.rb} +3 -1
  31. data/lib/graphql_devise/model/with_email_updater.rb +74 -0
  32. data/lib/graphql_devise/mount_method/operation_preparer.rb +4 -2
  33. data/lib/graphql_devise/mount_method/operation_preparers/custom_operation_preparer.rb +2 -0
  34. data/lib/graphql_devise/mount_method/operation_preparers/default_operation_preparer.rb +8 -2
  35. data/lib/graphql_devise/mount_method/operation_preparers/gql_name_setter.rb +3 -1
  36. data/lib/graphql_devise/mount_method/operation_preparers/mutation_field_setter.rb +5 -2
  37. data/lib/graphql_devise/mount_method/operation_preparers/resolver_type_setter.rb +3 -1
  38. data/lib/graphql_devise/mount_method/operation_preparers/resource_name_setter.rb +4 -2
  39. data/lib/graphql_devise/mount_method/operation_sanitizer.rb +2 -0
  40. data/lib/graphql_devise/mount_method/option_sanitizer.rb +2 -0
  41. data/lib/graphql_devise/mount_method/option_sanitizers/array_checker.rb +2 -0
  42. data/lib/graphql_devise/mount_method/option_sanitizers/class_checker.rb +2 -0
  43. data/lib/graphql_devise/mount_method/option_sanitizers/hash_checker.rb +2 -0
  44. data/lib/graphql_devise/mount_method/option_sanitizers/string_checker.rb +2 -0
  45. data/lib/graphql_devise/mount_method/option_validators/provided_operations_validator.rb +2 -0
  46. data/lib/graphql_devise/mount_method/option_validators/skip_only_validator.rb +2 -0
  47. data/lib/graphql_devise/mount_method/option_validators/supported_operations_validator.rb +2 -0
  48. data/lib/graphql_devise/mount_method/options_validator.rb +2 -0
  49. data/lib/graphql_devise/mount_method/supported_options.rb +2 -0
  50. data/lib/graphql_devise/mutations/base.rb +2 -0
  51. data/lib/graphql_devise/mutations/login.rb +2 -0
  52. data/lib/graphql_devise/mutations/logout.rb +2 -0
  53. data/lib/graphql_devise/mutations/resend_confirmation.rb +5 -5
  54. data/lib/graphql_devise/mutations/send_password_reset.rb +7 -2
  55. data/lib/graphql_devise/mutations/sign_up.rb +5 -6
  56. data/lib/graphql_devise/mutations/update_password.rb +2 -0
  57. data/lib/graphql_devise/rails/routes.rb +7 -72
  58. data/lib/graphql_devise/resolvers/base.rb +2 -0
  59. data/lib/graphql_devise/resolvers/check_password_token.rb +2 -0
  60. data/lib/graphql_devise/resolvers/confirm_account.rb +4 -2
  61. data/lib/graphql_devise/resolvers/dummy.rb +2 -0
  62. data/lib/graphql_devise/resource_loader.rb +89 -0
  63. data/lib/graphql_devise/schema.rb +2 -0
  64. data/lib/graphql_devise/schema_plugin.rb +114 -0
  65. data/lib/graphql_devise/types/authenticatable_type.rb +2 -0
  66. data/lib/graphql_devise/types/credential_type.rb +2 -0
  67. data/lib/graphql_devise/types/mutation_type.rb +2 -0
  68. data/lib/graphql_devise/types/query_type.rb +2 -0
  69. data/lib/graphql_devise/version.rb +3 -1
  70. data/spec/dummy/Rakefile +2 -0
  71. data/spec/dummy/app/controllers/api/v1/graphql_controller.rb +38 -3
  72. data/spec/dummy/app/controllers/application_controller.rb +2 -0
  73. data/spec/dummy/app/graphql/dummy_schema.rb +21 -0
  74. data/spec/dummy/app/graphql/interpreter_schema.rb +11 -0
  75. data/spec/dummy/app/graphql/mutations/login.rb +2 -0
  76. data/spec/dummy/app/graphql/mutations/register_confirmed_user.rb +2 -0
  77. data/spec/dummy/app/graphql/mutations/sign_up.rb +2 -0
  78. data/spec/dummy/app/graphql/mutations/update_user.rb +22 -0
  79. data/spec/dummy/app/graphql/resolvers/public_user.rb +2 -0
  80. data/spec/dummy/app/graphql/resolvers/user_show.rb +2 -0
  81. data/spec/dummy/app/graphql/types/base_object.rb +2 -0
  82. data/spec/dummy/app/graphql/types/custom_admin_type.rb +2 -0
  83. data/spec/dummy/app/graphql/types/mutation_type.rb +4 -1
  84. data/spec/dummy/app/graphql/types/query_type.rb +12 -0
  85. data/spec/dummy/app/graphql/types/user_type.rb +2 -0
  86. data/spec/dummy/app/jobs/application_job.rb +2 -0
  87. data/spec/dummy/app/mailers/application_mailer.rb +2 -0
  88. data/spec/dummy/app/models/admin.rb +2 -0
  89. data/spec/dummy/app/models/application_record.rb +2 -0
  90. data/spec/dummy/app/models/guest.rb +2 -0
  91. data/spec/dummy/app/models/schema_user.rb +13 -0
  92. data/spec/dummy/app/models/user.rb +2 -0
  93. data/spec/dummy/app/models/users.rb +2 -0
  94. data/spec/dummy/app/models/users/customer.rb +2 -0
  95. data/spec/dummy/config.ru +2 -0
  96. data/spec/dummy/config/application.rb +2 -0
  97. data/spec/dummy/config/environment.rb +2 -0
  98. data/spec/dummy/config/environments/development.rb +2 -0
  99. data/spec/dummy/config/environments/production.rb +2 -0
  100. data/spec/dummy/config/environments/test.rb +2 -0
  101. data/spec/dummy/config/initializers/application_controller_renderer.rb +2 -0
  102. data/spec/dummy/config/initializers/backtrace_silencers.rb +2 -0
  103. data/spec/dummy/config/initializers/cors.rb +2 -0
  104. data/spec/dummy/config/initializers/devise_token_auth.rb +2 -0
  105. data/spec/dummy/config/initializers/filter_parameter_logging.rb +2 -0
  106. data/spec/dummy/config/initializers/i18n.rb +2 -0
  107. data/spec/dummy/config/initializers/inflections.rb +2 -0
  108. data/spec/dummy/config/initializers/mime_types.rb +2 -0
  109. data/spec/dummy/config/initializers/wrap_parameters.rb +2 -0
  110. data/spec/dummy/config/puma.rb +2 -0
  111. data/spec/dummy/config/routes.rb +5 -0
  112. data/spec/dummy/config/spring.rb +2 -0
  113. data/spec/dummy/db/migrate/20190815114303_create_users.rb +2 -0
  114. data/spec/dummy/db/migrate/20190824215150_add_auth_available_to_users.rb +2 -0
  115. data/spec/dummy/db/migrate/20190916012505_create_admins.rb +2 -0
  116. data/spec/dummy/db/migrate/20191013213045_create_guests.rb +2 -0
  117. data/spec/dummy/db/migrate/20200321121807_create_users_customers.rb +2 -0
  118. data/spec/dummy/db/migrate/20200621182414_remove_uncofirmed_email_from_admins.rb +7 -0
  119. data/spec/dummy/db/migrate/20200623003142_create_schema_users.rb +46 -0
  120. data/spec/dummy/db/schema.rb +30 -2
  121. data/spec/dummy/db/seeds.rb +2 -0
  122. data/spec/factories/admins.rb +2 -0
  123. data/spec/factories/guests.rb +2 -0
  124. data/spec/factories/schema_users.rb +13 -0
  125. data/spec/factories/users.rb +2 -0
  126. data/spec/factories/users_customers.rb +2 -0
  127. data/spec/generators/graphql_devise/install_generator_spec.rb +23 -0
  128. data/spec/graphql_devise/model/with_email_updater_spec.rb +131 -0
  129. data/spec/graphql_devise_spec.rb +2 -0
  130. data/spec/models/user_spec.rb +2 -0
  131. data/spec/rails_helper.rb +3 -1
  132. data/spec/requests/graphql_controller_spec.rb +82 -0
  133. data/spec/requests/mutations/additional_mutations_spec.rb +2 -0
  134. data/spec/requests/mutations/additional_queries_spec.rb +2 -0
  135. data/spec/requests/mutations/login_spec.rb +2 -0
  136. data/spec/requests/mutations/logout_spec.rb +2 -0
  137. data/spec/requests/mutations/resend_confirmation_spec.rb +46 -29
  138. data/spec/requests/mutations/send_password_reset_spec.rb +42 -12
  139. data/spec/requests/mutations/sign_up_spec.rb +2 -0
  140. data/spec/requests/mutations/update_password_spec.rb +2 -0
  141. data/spec/requests/queries/check_password_token_spec.rb +2 -0
  142. data/spec/requests/queries/confirm_account_spec.rb +9 -1
  143. data/spec/requests/user_controller_spec.rb +237 -23
  144. data/spec/services/mount_method/operation_preparer_spec.rb +10 -3
  145. data/spec/services/mount_method/operation_preparers/custom_operation_preparer_spec.rb +3 -1
  146. data/spec/services/mount_method/operation_preparers/default_operation_preparer_spec.rb +17 -8
  147. data/spec/services/mount_method/operation_preparers/gql_name_setter_spec.rb +2 -0
  148. data/spec/services/mount_method/operation_preparers/mutation_field_setter_spec.rb +20 -4
  149. data/spec/services/mount_method/operation_preparers/resolver_type_setter_spec.rb +2 -0
  150. data/spec/services/mount_method/operation_preparers/resource_name_setter_spec.rb +3 -1
  151. data/spec/services/mount_method/operation_sanitizer_spec.rb +2 -0
  152. data/spec/services/mount_method/option_sanitizer_spec.rb +2 -0
  153. data/spec/services/mount_method/option_sanitizers/array_checker_spec.rb +2 -0
  154. data/spec/services/mount_method/option_sanitizers/class_checker_spec.rb +2 -0
  155. data/spec/services/mount_method/option_sanitizers/hash_checker_spec.rb +2 -0
  156. data/spec/services/mount_method/option_sanitizers/string_checker_spec.rb +2 -0
  157. data/spec/services/mount_method/option_validators/provided_operations_validator_spec.rb +2 -0
  158. data/spec/services/mount_method/option_validators/skip_only_validator_spec.rb +2 -0
  159. data/spec/services/mount_method/option_validators/supported_operations_validator_spec.rb +2 -0
  160. data/spec/services/mount_method/options_validator_spec.rb +2 -0
  161. data/spec/services/resource_loader_spec.rb +84 -0
  162. data/spec/services/schema_plugin_spec.rb +28 -0
  163. data/spec/spec_helper.rb +2 -0
  164. data/spec/support/contexts/graphql_request.rb +2 -0
  165. data/spec/support/factory_bot.rb +2 -0
  166. data/spec/support/matchers/auth_headers_matcher.rb +2 -0
  167. data/spec/support/matchers/not_change_matcher.rb +2 -0
  168. data/spec/support/requests/auth_helpers.rb +2 -0
  169. data/spec/support/requests/json_helpers.rb +2 -0
  170. metadata +120 -87
  171. data/lib/graphql_devise/error_codes.rb +0 -5
  172. data/spec/support/generators/file_helpers.rb +0 -12
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require_relative 'operation_preparers/gql_name_setter'
2
4
  require_relative 'operation_preparers/mutation_field_setter'
3
5
  require_relative 'operation_preparers/resolver_type_setter'
@@ -8,10 +10,10 @@ require_relative 'operation_preparers/custom_operation_preparer'
8
10
  module GraphqlDevise
9
11
  module MountMethod
10
12
  class OperationPreparer
11
- def initialize(resource:, selected_operations:, preparer:, custom:, additional_operations:)
13
+ def initialize(mapping_name:, selected_operations:, preparer:, custom:, additional_operations:)
12
14
  @selected_operations = selected_operations
13
15
  @preparer = preparer
14
- @mapping_name = resource.underscore.tr('/', '_')
16
+ @mapping_name = mapping_name
15
17
  @custom = custom
16
18
  @additional_operations = additional_operations
17
19
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module GraphqlDevise
2
4
  module MountMethod
3
5
  module OperationPreparers
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module GraphqlDevise
2
4
  module MountMethod
3
5
  module OperationPreparers
@@ -10,14 +12,18 @@ module GraphqlDevise
10
12
  end
11
13
 
12
14
  def call
13
- @selected_operations.except(*@custom_keys).each_with_object({}) do |(action, operation), result|
15
+ @selected_operations.except(*@custom_keys).each_with_object({}) do |(action, operation_info), result|
14
16
  mapped_action = "#{@mapping_name}_#{action}"
17
+ operation = operation_info[:klass]
18
+ options = operation_info.except(:klass)
15
19
 
16
20
  result[mapped_action.to_sym] = [
17
21
  OperationPreparers::GqlNameSetter.new(mapped_action),
18
22
  @preparer,
19
23
  OperationPreparers::ResourceNameSetter.new(@mapping_name)
20
- ].reduce(child_class(operation)) { |prepared_operation, preparer| preparer.call(prepared_operation) }
24
+ ].reduce(child_class(operation)) do |prepared_operation, preparer|
25
+ preparer.call(prepared_operation, **options)
26
+ end
21
27
  end
22
28
  end
23
29
 
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module GraphqlDevise
2
4
  module MountMethod
3
5
  module OperationPreparers
@@ -6,7 +8,7 @@ module GraphqlDevise
6
8
  @mapping_name = mapping_name
7
9
  end
8
10
 
9
- def call(operation)
11
+ def call(operation, **)
10
12
  operation.graphql_name(graphql_name)
11
13
 
12
14
  operation
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module GraphqlDevise
2
4
  module MountMethod
3
5
  module OperationPreparers
@@ -6,9 +8,10 @@ module GraphqlDevise
6
8
  @authenticatable_type = authenticatable_type
7
9
  end
8
10
 
9
- def call(mutation)
10
- mutation.field(:authenticatable, @authenticatable_type, null: false)
11
+ def call(mutation, authenticatable: true)
12
+ return mutation unless authenticatable
11
13
 
14
+ mutation.field(:authenticatable, @authenticatable_type, null: false)
12
15
  mutation
13
16
  end
14
17
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module GraphqlDevise
2
4
  module MountMethod
3
5
  module OperationPreparers
@@ -6,7 +8,7 @@ module GraphqlDevise
6
8
  @authenticatable_type = authenticatable_type
7
9
  end
8
10
 
9
- def call(resolver)
11
+ def call(resolver, **)
10
12
  resolver.type(@authenticatable_type, null: false)
11
13
 
12
14
  resolver
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module GraphqlDevise
2
4
  module MountMethod
3
5
  module OperationPreparers
@@ -6,8 +8,8 @@ module GraphqlDevise
6
8
  @name = name
7
9
  end
8
10
 
9
- def call(operation)
10
- operation.instance_variable_set(:@resource_name, @name.to_sym)
11
+ def call(operation, **)
12
+ operation.instance_variable_set(:@resource_name, @name)
11
13
 
12
14
  operation
13
15
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module GraphqlDevise
2
4
  module MountMethod
3
5
  class OperationSanitizer
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require_relative 'supported_options'
2
4
 
3
5
  module GraphqlDevise
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module GraphqlDevise
2
4
  module MountMethod
3
5
  module OptionSanitizers
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module GraphqlDevise
2
4
  module MountMethod
3
5
  module OptionSanitizers
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module GraphqlDevise
2
4
  module MountMethod
3
5
  module OptionSanitizers
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module GraphqlDevise
2
4
  module MountMethod
3
5
  module OptionSanitizers
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require_relative 'supported_operations_validator'
2
4
 
3
5
  module GraphqlDevise
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module GraphqlDevise
2
4
  module MountMethod
3
5
  module OptionValidators
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module GraphqlDevise
2
4
  module MountMethod
3
5
  module OptionValidators
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require_relative 'option_validators/skip_only_validator'
2
4
  require_relative 'option_validators/provided_operations_validator'
3
5
 
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require_relative 'option_sanitizers/array_checker'
2
4
  require_relative 'option_sanitizers/hash_checker'
3
5
  require_relative 'option_sanitizers/string_checker'
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'devise_token_auth/version'
2
4
 
3
5
  module GraphqlDevise
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module GraphqlDevise
2
4
  module Mutations
3
5
  class Login < Base
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module GraphqlDevise
2
4
  module Mutations
3
5
  class Logout < Base
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module GraphqlDevise
2
4
  module Mutations
3
5
  class ResendConfirmation < Base
@@ -19,13 +21,11 @@ module GraphqlDevise
19
21
 
20
22
  resource.send_confirmation_instructions(
21
23
  redirect_url: redirect_url,
22
- template_path: ['graphql_devise/mailer']
24
+ template_path: ['graphql_devise/mailer'],
25
+ schema_url: controller.full_url_without_params
23
26
  )
24
27
 
25
- {
26
- authenticatable: resource,
27
- message: I18n.t('graphql_devise.confirmations.send_instructions', email: email)
28
- }
28
+ { message: I18n.t('graphql_devise.confirmations.send_instructions', email: email) }
29
29
  else
30
30
  raise_user_error(I18n.t('graphql_devise.confirmations.user_not_found', email: email))
31
31
  end
@@ -1,9 +1,13 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module GraphqlDevise
2
4
  module Mutations
3
5
  class SendPasswordReset < Base
4
6
  argument :email, String, required: true
5
7
  argument :redirect_url, String, required: true
6
8
 
9
+ field :message, String, null: false
10
+
7
11
  def resolve(email:, redirect_url:)
8
12
  resource = find_resource(:email, get_case_insensitive_field(:email, email))
9
13
 
@@ -14,11 +18,12 @@ module GraphqlDevise
14
18
  email: email,
15
19
  provider: 'email',
16
20
  redirect_url: redirect_url,
17
- template_path: ['graphql_devise/mailer']
21
+ template_path: ['graphql_devise/mailer'],
22
+ schema_url: controller.full_url_without_params
18
23
  )
19
24
 
20
25
  if resource.errors.empty?
21
- { authenticatable: resource }
26
+ { message: I18n.t('graphql_devise.passwords.send_instructions') }
22
27
  else
23
28
  raise_user_error_list(I18n.t('graphql_devise.invalid_resource'), errors: resource.errors.full_messages)
24
29
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module GraphqlDevise
2
4
  module Mutations
3
5
  class SignUp < Base
@@ -27,7 +29,8 @@ module GraphqlDevise
27
29
  unless resource.confirmed?
28
30
  resource.send_confirmation_instructions(
29
31
  redirect_url: confirm_success_url,
30
- template_path: ['graphql_devise/mailer']
32
+ template_path: ['graphql_devise/mailer'],
33
+ schema_url: controller.full_url_without_params
31
34
  )
32
35
  end
33
36
 
@@ -35,7 +38,7 @@ module GraphqlDevise
35
38
 
36
39
  { authenticatable: resource }
37
40
  else
38
- clean_up_passwords(resource)
41
+ resource.try(:clean_up_passwords)
39
42
  raise_user_error_list(
40
43
  I18n.t('graphql_devise.registration_failed'),
41
44
  errors: resource.errors.full_messages
@@ -48,10 +51,6 @@ module GraphqlDevise
48
51
  def build_resource(attrs)
49
52
  resource_class.new(attrs)
50
53
  end
51
-
52
- def clean_up_passwords(resource)
53
- controller.send(:clean_up_passwords, resource)
54
- end
55
54
  end
56
55
  end
57
56
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module GraphqlDevise
2
4
  module Mutations
3
5
  class UpdatePassword < Base
@@ -1,80 +1,15 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ActionDispatch::Routing
2
4
  class Mapper
3
- DEVISE_OPERATIONS = [
4
- :sessions,
5
- :registrations,
6
- :passwords,
7
- :confirmations,
8
- :omniauth_callbacks,
9
- :unlocks,
10
- :invitations
11
- ].freeze
12
-
13
5
  def mount_graphql_devise_for(resource, options = {})
14
- default_operations = GraphqlDevise::DefaultOperations::MUTATIONS.merge(GraphqlDevise::DefaultOperations::QUERIES)
15
-
16
- # clean_options responds to all keys defined in GraphqlDevise::MountMethod::SUPPORTED_OPTIONS
17
- clean_options = GraphqlDevise::MountMethod::OptionSanitizer.new(options).call!
18
-
19
- GraphqlDevise::MountMethod::OptionsValidator.new(
20
- [
21
- GraphqlDevise::MountMethod::OptionValidators::SkipOnlyValidator.new(options: clean_options),
22
- GraphqlDevise::MountMethod::OptionValidators::ProvidedOperationsValidator.new(
23
- options: clean_options, supported_operations: default_operations
24
- )
25
- ]
26
- ).validate!
27
-
28
- devise_for(
29
- resource.pluralize.underscore.tr('/', '_').to_sym,
30
- module: :devise,
31
- class_name: resource,
32
- skip: DEVISE_OPERATIONS
6
+ clean_options = GraphqlDevise::ResourceLoader.new(resource, options, true).call(
7
+ GraphqlDevise::Types::QueryType,
8
+ GraphqlDevise::Types::MutationType
33
9
  )
34
10
 
35
- devise_scope resource.underscore.tr('/', '_').to_sym do
36
- post clean_options.at, to: 'graphql_devise/graphql#auth'
37
- get clean_options.at, to: 'graphql_devise/graphql#auth'
38
- end
39
-
40
- # Avoid routes reload done by Devise
41
- return if GraphqlDevise.resource_mounted?(resource)
42
-
43
- authenticatable_type = clean_options.authenticatable_type.presence ||
44
- "Types::#{resource}Type".safe_constantize ||
45
- GraphqlDevise::Types::AuthenticatableType
46
-
47
- prepared_mutations = GraphqlDevise::MountMethod::OperationPreparer.new(
48
- resource: resource,
49
- custom: clean_options.operations,
50
- additional_operations: clean_options.additional_mutations,
51
- preparer: GraphqlDevise::MountMethod::OperationPreparers::MutationFieldSetter.new(authenticatable_type),
52
- selected_operations: GraphqlDevise::MountMethod::OperationSanitizer.call(
53
- default: GraphqlDevise::DefaultOperations::MUTATIONS, only: clean_options.only, skipped: clean_options.skip
54
- )
55
- ).call
56
-
57
- prepared_mutations.each do |action, mutation|
58
- GraphqlDevise::Types::MutationType.field(action, mutation: mutation)
59
- end
60
-
61
- prepared_queries = GraphqlDevise::MountMethod::OperationPreparer.new(
62
- resource: resource,
63
- custom: clean_options.operations,
64
- additional_operations: clean_options.additional_queries,
65
- preparer: GraphqlDevise::MountMethod::OperationPreparers::ResolverTypeSetter.new(authenticatable_type),
66
- selected_operations: GraphqlDevise::MountMethod::OperationSanitizer.call(
67
- default: GraphqlDevise::DefaultOperations::QUERIES, only: clean_options.only, skipped: clean_options.skip
68
- )
69
- ).call
70
-
71
- prepared_queries.each do |action, resolver|
72
- GraphqlDevise::Types::QueryType.field(action, resolver: resolver)
73
- end
74
-
75
- Devise.mailer.helper(GraphqlDevise::MailerHelper)
76
-
77
- GraphqlDevise.mount_resource(resource)
11
+ post clean_options.at, to: 'graphql_devise/graphql#auth'
12
+ get clean_options.at, to: 'graphql_devise/graphql#auth'
78
13
  end
79
14
  end
80
15
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'devise_token_auth/version'
2
4
 
3
5
  module GraphqlDevise
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module GraphqlDevise
2
4
  module Resolvers
3
5
  class CheckPasswordToken < Base
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module GraphqlDevise
2
4
  module Resolvers
3
5
  class ConfirmAccount < Base
@@ -13,10 +15,10 @@ module GraphqlDevise
13
15
  redirect_header_options = { account_confirmation_success: true }
14
16
 
15
17
  redirect_to_link = if controller.signed_in?(resource_name)
16
- signed_in_resource.build_auth_url(
18
+ resource.build_auth_url(
17
19
  redirect_url,
18
20
  redirect_headers(
19
- client_and_token(controller.signed_in_resource.create_token),
21
+ client_and_token(resource.create_token),
20
22
  redirect_header_options
21
23
  )
22
24
  )
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module GraphqlDevise
2
4
  module Resolvers
3
5
  class Dummy < Base
@@ -0,0 +1,89 @@
1
+ # frozen_string_literal: true
2
+
3
+ module GraphqlDevise
4
+ class ResourceLoader
5
+ def initialize(resource, options = {}, routing = false)
6
+ @resource = resource
7
+ @options = options
8
+ @routing = routing
9
+ @default_operations = GraphqlDevise::DefaultOperations::MUTATIONS.merge(GraphqlDevise::DefaultOperations::QUERIES)
10
+ end
11
+
12
+ def call(query, mutation)
13
+ mapping_name = @resource.to_s.underscore.tr('/', '_').to_sym
14
+
15
+ # clean_options responds to all keys defined in GraphqlDevise::MountMethod::SUPPORTED_OPTIONS
16
+ clean_options = GraphqlDevise::MountMethod::OptionSanitizer.new(@options).call!
17
+
18
+ return clean_options if GraphqlDevise.resource_mounted?(mapping_name) && @routing
19
+
20
+ validate_options!(clean_options)
21
+
22
+ authenticatable_type = clean_options.authenticatable_type.presence ||
23
+ "Types::#{@resource}Type".safe_constantize ||
24
+ GraphqlDevise::Types::AuthenticatableType
25
+
26
+ prepared_mutations = prepare_mutations(mapping_name, clean_options, authenticatable_type)
27
+
28
+ if prepared_mutations.any? && mutation.blank?
29
+ raise GraphqlDevise::Error, 'You need to provide a mutation type unless all mutations are skipped'
30
+ end
31
+
32
+ prepared_mutations.each do |action, prepared_mutation|
33
+ mutation.field(action, mutation: prepared_mutation, authenticate: false)
34
+ end
35
+
36
+ prepared_resolvers = prepare_resolvers(mapping_name, clean_options, authenticatable_type)
37
+
38
+ if prepared_resolvers.any? && query.blank?
39
+ raise GraphqlDevise::Error, 'You need to provide a query type unless all queries are skipped'
40
+ end
41
+
42
+ prepared_resolvers.each do |action, resolver|
43
+ query.field(action, resolver: resolver, authenticate: false)
44
+ end
45
+
46
+ GraphqlDevise.add_mapping(mapping_name, @resource)
47
+ GraphqlDevise.mount_resource(mapping_name) if @routing
48
+
49
+ clean_options
50
+ end
51
+
52
+ private
53
+
54
+ def prepare_resolvers(mapping_name, clean_options, authenticatable_type)
55
+ GraphqlDevise::MountMethod::OperationPreparer.new(
56
+ mapping_name: mapping_name,
57
+ custom: clean_options.operations,
58
+ additional_operations: clean_options.additional_queries,
59
+ preparer: GraphqlDevise::MountMethod::OperationPreparers::ResolverTypeSetter.new(authenticatable_type),
60
+ selected_operations: GraphqlDevise::MountMethod::OperationSanitizer.call(
61
+ default: GraphqlDevise::DefaultOperations::QUERIES, only: clean_options.only, skipped: clean_options.skip
62
+ )
63
+ ).call
64
+ end
65
+
66
+ def prepare_mutations(mapping_name, clean_options, authenticatable_type)
67
+ GraphqlDevise::MountMethod::OperationPreparer.new(
68
+ mapping_name: mapping_name,
69
+ custom: clean_options.operations,
70
+ additional_operations: clean_options.additional_mutations,
71
+ preparer: GraphqlDevise::MountMethod::OperationPreparers::MutationFieldSetter.new(authenticatable_type),
72
+ selected_operations: GraphqlDevise::MountMethod::OperationSanitizer.call(
73
+ default: GraphqlDevise::DefaultOperations::MUTATIONS, only: clean_options.only, skipped: clean_options.skip
74
+ )
75
+ ).call
76
+ end
77
+
78
+ def validate_options!(clean_options)
79
+ GraphqlDevise::MountMethod::OptionsValidator.new(
80
+ [
81
+ GraphqlDevise::MountMethod::OptionValidators::SkipOnlyValidator.new(options: clean_options),
82
+ GraphqlDevise::MountMethod::OptionValidators::ProvidedOperationsValidator.new(
83
+ options: clean_options, supported_operations: @default_operations
84
+ )
85
+ ]
86
+ ).validate!
87
+ end
88
+ end
89
+ end