-
Notifications
You must be signed in to change notification settings - Fork 45
Take custom mutations when mounting gem in routes #5
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,3 @@ | ||
| source 'https://rubygems.org' | ||
|
|
||
| gemspec | ||
|
|
||
| gem 'listen' | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,9 +4,8 @@ class Login < Base | |
| argument :email, String, required: true | ||
| argument :password, String, required: true | ||
|
|
||
| field :authenticable, GraphqlDevise::Types::AuthenticableType, null: true | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is this removed? |
||
| field :success, Boolean, null: false | ||
| field :errors, [String], null: false | ||
| field :success, Boolean, null: false | ||
| field :errors, [String], null: false | ||
|
|
||
| def resolve(email:, password:) | ||
| resource = resource_class.find_by(email: email) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| module GraphqlDevise | ||
| module Mutations | ||
| class Logout < Base | ||
| field :success, Boolean, null: false | ||
| field :errors, [String], null: false | ||
|
|
||
| def resolve | ||
| if current_resource && client && current_resource.tokens[client] | ||
| current_resource.tokens.delete(client) | ||
| current_resource.save! | ||
|
|
||
| remove_resource | ||
|
|
||
| yield current_resource if block_given? | ||
|
|
||
| { success: true, errors: [], authenticable: current_resource } | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should |
||
| else | ||
| { success: false, errors: [I18n.t('graphql_devise.user_not_found')] } | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,6 @@ | ||
| module GraphqlDevise | ||
| module Types | ||
| class MutationType < GraphQL::Schema::Object | ||
| field :login, mutation: GraphqlDevise::Mutations::Login | ||
| end | ||
| end | ||
| end |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,29 +3,40 @@ | |
| module ActionDispatch::Routing | ||
| class Mapper | ||
| def mount_graphql_devise_for(resource, opts = {}) | ||
| options = { | ||
| skip: [ | ||
| :sessions, | ||
| :registrations, | ||
| :passwords, | ||
| :confirmations, | ||
| :token_validations, | ||
| :omniauth_callbacks, | ||
| :unlocks | ||
| ] | ||
| }.merge(opts) | ||
| mutation_options = opts[:mutations] || {} | ||
|
|
||
| path = opts.fetch(:at, '/') | ||
| mapping_name = resource.underscore.tr('/', '_') | ||
|
|
||
| devise_for( | ||
| resource.pluralize.underscore.tr('/', '_').to_sym, | ||
| class_name: resource, | ||
| module: :devise, | ||
| path: path, | ||
| skip: options[:skip] + [:omniauth_callbacks] | ||
| module: :devise, | ||
| skip: [:sessions, :registrations, :passwords, :confirmations, :omniauth_callbacks, :unlocks] | ||
| ) | ||
|
|
||
| authenticable_type = opts[:authenticable_type] || | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What about this kind indentation, I find it clearer: authenticable_type =
opts[:authenticable_type] ||
GraphqlDevise::Util::ClassGetter.call("Types::#{resource}Type") ||
GraphqlDevise::Types::AuthenticableType |
||
| "Types::#{resource}Type".safe_constantize || | ||
| GraphqlDevise::Types::AuthenticableType | ||
|
|
||
| default_mutations = { | ||
| login: GraphqlDevise::Mutations::Login, | ||
| logout: GraphqlDevise::Mutations::Logout | ||
| }.freeze | ||
|
|
||
| default_mutations.each do |action, mutation| | ||
| used_mutation = if mutation_options[action].present? | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What about this style for indentation? used_mutation =
if mutation_options[action].present?
...
else
...
end |
||
| mutation_options[action] | ||
| else | ||
| new_mutation = Class.new(mutation) | ||
| new_mutation.graphql_name("#{resource}#{action.to_s.titleize}") | ||
| new_mutation.field(:authenticable, authenticable_type, null: true) | ||
|
|
||
| new_mutation | ||
| end | ||
|
|
||
| GraphqlDevise::Types::MutationType.field("#{mapping_name}_#{action}", mutation: used_mutation) | ||
| end | ||
|
|
||
| devise_scope mapping_name.to_sym do | ||
| post "#{path}/graphql_auth", to: 'graphql_devise/graphql#auth' | ||
| end | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| module Mutations | ||
| class Login < GraphqlDevise::Mutations::Login | ||
| field :user, Types::UserType, null: true | ||
|
|
||
| def resolve(email:, password:) | ||
| original_payload = super do |user| | ||
| user.do_something | ||
| user.reload | ||
| end | ||
|
|
||
| original_payload.merge(user: original_payload[:authenticable]) | ||
| end | ||
| end | ||
| end |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| module Types | ||
| class UserType < GraphQL::Schema::Object | ||
| field :email, String, null: false | ||
| field :name, String, null: false | ||
| field :sign_in_count, Int, null: true | ||
| end | ||
| end |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,3 @@ | ||
| #!/usr/bin/env ruby | ||
| APP_PATH = File.expand_path('../config/application', __dir__) | ||
| require_relative '../config/boot' | ||
| require 'rails/commands' |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,3 @@ | ||
| #!/usr/bin/env ruby | ||
| require_relative '../config/boot' | ||
| require 'rake' | ||
| Rake.application.run |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,5 @@ | ||
| Rails.application.routes.draw do | ||
| mount_graphql_devise_for 'User', at: 'api/v1' | ||
| mount_graphql_devise_for 'User', at: 'api/v1', mutations: { | ||
| login: Mutations::Login | ||
| } | ||
| end |
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.
Why is this removed?