Skip to content

Commit c4ce4a1

Browse files
Merge pull request #5 from graphql-device/take-options-route-mount
Take custom mutations when mounting gem in routes
2 parents ce3eb43 + a3e3876 commit c4ce4a1

29 files changed

Lines changed: 179 additions & 64 deletions

.rubocop.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,17 @@ Rails:
55
Enabled: true
66

77
AllCops:
8-
TargetRubyVersion: 2.3
8+
TargetRubyVersion: 2.2
99
DisplayCopNames: true
1010
Exclude:
1111
- bin/**/*
1212
- db/**/*
1313
- vendor/**/*
1414
- tmp/**/*
1515

16+
Rails/HttpPositionalArguments:
17+
Enabled: false
18+
1619
Rails/HasAndBelongsToMany:
1720
Enabled: false
1821

Gemfile

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
11
source 'https://rubygems.org'
22

33
gemspec
4-
5-
gem 'listen'

app/controllers/graphql_devise/graphql_controller.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ def auth
1818
render json: result
1919
end
2020

21+
attr_accessor :client_id, :token, :resource
22+
2123
private
2224

2325
def execute_params(item)

app/graphql/graphql_devise/mutations/base.rb

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,26 @@
1+
require 'devise_token_auth/version'
2+
13
module GraphqlDevise
24
module Mutations
35
class Base < GraphQL::Schema::Mutation
46
private
57

8+
def remove_resource
9+
controller.resource = nil
10+
controller.client_id = nil
11+
controller.token = nil
12+
end
13+
614
def single_error_object(error)
715
{ success: false, errors: [error] }
816
end
917

1018
def request
11-
context[:controller].request
19+
controller.request
1220
end
1321

1422
def response
15-
context[:controller].response
23+
controller.response
1624
end
1725

1826
def controller
@@ -26,6 +34,14 @@ def resource_class
2634
def current_resource
2735
context[:current_resource]
2836
end
37+
38+
def client
39+
if Gem::Version.new(DeviseTokenAuth::VERSION) <= Gem::Version.new('1.1.0')
40+
controller.client_id
41+
else
42+
controller.token.client if controller.token.present?
43+
end
44+
end
2945
end
3046
end
3147
end

app/graphql/graphql_devise/mutations/login.rb

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,8 @@ class Login < Base
44
argument :email, String, required: true
55
argument :password, String, required: true
66

7-
field :authenticable, GraphqlDevise::Types::AuthenticableType, null: true
8-
field :success, Boolean, null: false
9-
field :errors, [String], null: false
7+
field :success, Boolean, null: false
8+
field :errors, [String], null: false
109

1110
def resolve(email:, password:)
1211
resource = resource_class.find_by(email: email)
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
module GraphqlDevise
2+
module Mutations
3+
class Logout < Base
4+
field :success, Boolean, null: false
5+
field :errors, [String], null: false
6+
7+
def resolve
8+
if current_resource && client && current_resource.tokens[client]
9+
current_resource.tokens.delete(client)
10+
current_resource.save!
11+
12+
remove_resource
13+
14+
yield current_resource if block_given?
15+
16+
{ success: true, errors: [], authenticable: current_resource }
17+
else
18+
{ success: false, errors: [I18n.t('graphql_devise.user_not_found')] }
19+
end
20+
end
21+
end
22+
end
23+
end
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
module GraphqlDevise
22
module Types
33
class MutationType < GraphQL::Schema::Object
4-
field :login, mutation: GraphqlDevise::Mutations::Login
54
end
65
end
76
end

config/locales/en.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
en:
22
graphql_devise:
3+
user_not_found: "User was not found or was not logged in."
34
sessions:
45
bad_credentials: "Invalid login credentials. Please try again."
56
not_confirmed: "A confirmation email was sent to your account at '%{email}'. You must follow the instructions in the email before your account can be activated"

gemfiles/rails4.2_graphql1.8.gemfile

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
source "https://rubygems.org"
44

5-
gem "listen"
65
gem "bundler", "~> 1.17"
76
gem "rails", github: "rails/rails", branch: "4-2-stable"
87
gem "graphql", "~> 1.8.0"

gemfiles/rails5.0_graphql1.8.gemfile

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
source "https://rubygems.org"
44

5-
gem "listen"
65
gem "rails", github: "rails/rails", branch: "5-0-stable"
76
gem "graphql", "~> 1.8.0"
87
gem "devise_token_auth", "0.1.37"

0 commit comments

Comments
 (0)