-
Notifications
You must be signed in to change notification settings - Fork 45
Mount auth operations in main GQL schema #96
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 4 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
d8ea28d
ApplicationControler does not inherit from DTA
mcelicalderon c1f9570
Fix resource name for namespaced models in mailer helper
mcelicalderon 2e63cda
Move schema mount login into separate class
mcelicalderon 6898d21
Add SchemaPlugin
mcelicalderon 7cb6072
Rename controller concern, add context method, add more test scenarios
mcelicalderon da575ba
Change README for SchemaPlugin instructions
mcelicalderon 03f2c3c
Revert renaming SetUserByToken concern
mcelicalderon 687ebaf
Generator can mount operations in main app schema
mcelicalderon File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,7 @@ | ||
| module GraphqlDevise | ||
| class ApplicationController < DeviseTokenAuth::ApplicationController | ||
| private | ||
|
|
||
| def verify_authenticity_token | ||
| end | ||
| ApplicationController = if Rails::VERSION::MAJOR >= 5 | ||
| Class.new(ActionController::API) | ||
| else | ||
| Class.new(ActionController::Base) | ||
| end | ||
| end |
14 changes: 14 additions & 0 deletions
14
app/controllers/graphql_devise/concerns/set_user_by_token.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,19 @@ | ||
| module GraphqlDevise | ||
| module Concerns | ||
| SetUserByToken = DeviseTokenAuth::Concerns::SetUserByToken | ||
|
|
||
| SetUserByToken.module_eval do | ||
| attr_accessor :client_id, :token, :resource | ||
|
|
||
| def build_redirect_headers(access_token, client, redirect_header_options = {}) | ||
| { | ||
| DeviseTokenAuth.headers_names[:"access-token"] => access_token, | ||
| DeviseTokenAuth.headers_names[:client] => client, | ||
| :config => params[:config], | ||
| :client_id => client, | ||
| :token => access_token | ||
| }.merge(redirect_header_options) | ||
| end | ||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,80 +1,13 @@ | ||
| module ActionDispatch::Routing | ||
| class Mapper | ||
| DEVISE_OPERATIONS = [ | ||
| :sessions, | ||
| :registrations, | ||
| :passwords, | ||
| :confirmations, | ||
| :omniauth_callbacks, | ||
| :unlocks, | ||
| :invitations | ||
| ].freeze | ||
|
|
||
| def mount_graphql_devise_for(resource, options = {}) | ||
| default_operations = GraphqlDevise::DefaultOperations::MUTATIONS.merge(GraphqlDevise::DefaultOperations::QUERIES) | ||
|
|
||
| # clean_options responds to all keys defined in GraphqlDevise::MountMethod::SUPPORTED_OPTIONS | ||
| clean_options = GraphqlDevise::MountMethod::OptionSanitizer.new(options).call! | ||
|
|
||
| GraphqlDevise::MountMethod::OptionsValidator.new( | ||
| [ | ||
| GraphqlDevise::MountMethod::OptionValidators::SkipOnlyValidator.new(options: clean_options), | ||
| GraphqlDevise::MountMethod::OptionValidators::ProvidedOperationsValidator.new( | ||
| options: clean_options, supported_operations: default_operations | ||
| ) | ||
| ] | ||
| ).validate! | ||
|
|
||
| devise_for( | ||
| resource.pluralize.underscore.tr('/', '_').to_sym, | ||
| module: :devise, | ||
| class_name: resource, | ||
| skip: DEVISE_OPERATIONS | ||
| clean_options = GraphqlDevise::ResourceLoader.new(resource, options, true).call( | ||
| GraphqlDevise::Types::QueryType, | ||
| GraphqlDevise::Types::MutationType | ||
| ) | ||
|
|
||
| devise_scope resource.underscore.tr('/', '_').to_sym do | ||
| post clean_options.at, to: 'graphql_devise/graphql#auth' | ||
| get clean_options.at, to: 'graphql_devise/graphql#auth' | ||
| end | ||
|
|
||
| # Avoid routes reload done by Devise | ||
| return if GraphqlDevise.resource_mounted?(resource) | ||
|
|
||
| authenticatable_type = clean_options.authenticatable_type.presence || | ||
| "Types::#{resource}Type".safe_constantize || | ||
| GraphqlDevise::Types::AuthenticatableType | ||
|
|
||
| prepared_mutations = GraphqlDevise::MountMethod::OperationPreparer.new( | ||
| resource: resource, | ||
| custom: clean_options.operations, | ||
| additional_operations: clean_options.additional_mutations, | ||
| preparer: GraphqlDevise::MountMethod::OperationPreparers::MutationFieldSetter.new(authenticatable_type), | ||
| selected_operations: GraphqlDevise::MountMethod::OperationSanitizer.call( | ||
| default: GraphqlDevise::DefaultOperations::MUTATIONS, only: clean_options.only, skipped: clean_options.skip | ||
| ) | ||
| ).call | ||
|
|
||
| prepared_mutations.each do |action, mutation| | ||
| GraphqlDevise::Types::MutationType.field(action, mutation: mutation) | ||
| end | ||
|
|
||
| prepared_queries = GraphqlDevise::MountMethod::OperationPreparer.new( | ||
| resource: resource, | ||
| custom: clean_options.operations, | ||
| additional_operations: clean_options.additional_queries, | ||
| preparer: GraphqlDevise::MountMethod::OperationPreparers::ResolverTypeSetter.new(authenticatable_type), | ||
| selected_operations: GraphqlDevise::MountMethod::OperationSanitizer.call( | ||
| default: GraphqlDevise::DefaultOperations::QUERIES, only: clean_options.only, skipped: clean_options.skip | ||
| ) | ||
| ).call | ||
|
|
||
| prepared_queries.each do |action, resolver| | ||
| GraphqlDevise::Types::QueryType.field(action, resolver: resolver) | ||
| end | ||
|
|
||
| Devise.mailer.helper(GraphqlDevise::MailerHelper) | ||
|
|
||
| GraphqlDevise.mount_resource(resource) | ||
| post clean_options.at, to: 'graphql_devise/graphql#auth' | ||
| get clean_options.at, to: 'graphql_devise/graphql#auth' | ||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| module GraphqlDevise | ||
| class ResourceLoader | ||
| def initialize(resource, options, routing = false) | ||
| @resource = resource | ||
| @options = options | ||
| @routing = routing | ||
| @default_operations = GraphqlDevise::DefaultOperations::MUTATIONS.merge(GraphqlDevise::DefaultOperations::QUERIES) | ||
| end | ||
|
|
||
| def call(query, mutation) | ||
| mapping_name = @resource.to_s.underscore.tr('/', '_').to_sym | ||
|
|
||
| # clean_options responds to all keys defined in GraphqlDevise::MountMethod::SUPPORTED_OPTIONS | ||
| clean_options = GraphqlDevise::MountMethod::OptionSanitizer.new(@options).call! | ||
|
|
||
| return clean_options if GraphqlDevise.resource_mounted?(mapping_name) && @routing | ||
|
|
||
| validate_options!(clean_options) | ||
|
|
||
| authenticatable_type = clean_options.authenticatable_type.presence || | ||
| "Types::#{@resource}Type".safe_constantize || | ||
| GraphqlDevise::Types::AuthenticatableType | ||
|
|
||
| prepared_mutations = prepare_mutations(mapping_name, clean_options, authenticatable_type) | ||
|
|
||
| if prepared_mutations.any? && mutation.blank? | ||
| raise GraphqlDevise::Error, 'You need to provide a mutation type unless all mutations are skipped' | ||
| end | ||
|
|
||
| prepared_mutations.each do |action, prepared_mutation| | ||
| mutation.field(action, mutation: prepared_mutation, authenticate: false) | ||
| end | ||
|
|
||
| prepared_resolvers = prepare_resolvers(mapping_name, clean_options, authenticatable_type) | ||
|
|
||
| prepared_resolvers.each do |action, resolver| | ||
| query.field(action, resolver: resolver, authenticate: false) | ||
| end | ||
|
|
||
| GraphqlDevise.add_mapping(mapping_name, @resource) | ||
| GraphqlDevise.mount_resource(mapping_name) if @routing | ||
|
|
||
| clean_options | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def prepare_resolvers(mapping_name, clean_options, authenticatable_type) | ||
| GraphqlDevise::MountMethod::OperationPreparer.new( | ||
| mapping_name: mapping_name, | ||
| custom: clean_options.operations, | ||
| additional_operations: clean_options.additional_queries, | ||
| preparer: GraphqlDevise::MountMethod::OperationPreparers::ResolverTypeSetter.new(authenticatable_type), | ||
| selected_operations: GraphqlDevise::MountMethod::OperationSanitizer.call( | ||
| default: GraphqlDevise::DefaultOperations::QUERIES, only: clean_options.only, skipped: clean_options.skip | ||
| ) | ||
| ).call | ||
| end | ||
|
|
||
| def prepare_mutations(mapping_name, clean_options, authenticatable_type) | ||
| GraphqlDevise::MountMethod::OperationPreparer.new( | ||
| mapping_name: mapping_name, | ||
| custom: clean_options.operations, | ||
| additional_operations: clean_options.additional_mutations, | ||
| preparer: GraphqlDevise::MountMethod::OperationPreparers::MutationFieldSetter.new(authenticatable_type), | ||
| selected_operations: GraphqlDevise::MountMethod::OperationSanitizer.call( | ||
| default: GraphqlDevise::DefaultOperations::MUTATIONS, only: clean_options.only, skipped: clean_options.skip | ||
| ) | ||
| ).call | ||
| end | ||
|
|
||
| def validate_options!(clean_options) | ||
| GraphqlDevise::MountMethod::OptionsValidator.new( | ||
| [ | ||
| GraphqlDevise::MountMethod::OptionValidators::SkipOnlyValidator.new(options: clean_options), | ||
| GraphqlDevise::MountMethod::OptionValidators::ProvidedOperationsValidator.new( | ||
| options: clean_options, supported_operations: @default_operations | ||
| ) | ||
| ] | ||
| ).validate! | ||
| end | ||
| end | ||
| end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since rails is a dependency, did you considered
try?