Skip to content

Commit 62e9d1e

Browse files
committed
Add first POC login mutation
1 parent 9bcf881 commit 62e9d1e

6 files changed

Lines changed: 103 additions & 0 deletions

File tree

app/controllers/graphql_devise/graphql_controller.rb

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,49 @@
33
module GraphqlDevise
44
class GraphqlController < ApplicationController
55
def auth
6+
result = if params[:_json]
7+
GraphqlDevise::Schema.multiplex(
8+
params[:_json].map do |param|
9+
{ query: param[:query] }.merge(execute_params(param))
10+
end
11+
)
12+
else
13+
GraphqlDevise::Schema.execute(params[:query], execute_params(params))
14+
end
15+
16+
render json: result
17+
end
18+
19+
private
20+
21+
def execute_params(item, user = current_user)
22+
{
23+
operation_name: item[:operationName],
24+
variables: ensure_hash(item[:variables]),
25+
context: {
26+
current_user: user,
27+
request: request,
28+
response: response,
29+
resource_class: resource_class
30+
}
31+
}
32+
end
33+
34+
def ensure_hash(ambiguous_param)
35+
case ambiguous_param
36+
when String
37+
if ambiguous_param.present?
38+
ensure_hash(JSON.parse(ambiguous_param))
39+
else
40+
{}
41+
end
42+
when Hash, ActionController::Parameters
43+
ambiguous_param
44+
when nil
45+
{}
46+
else
47+
raise ArgumentError, "Unexpected parameter: #{ambiguous_param}"
48+
end
649
end
750
end
851
end
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
module GraphqlDevise
2+
module Mutations
3+
class Login < GraphQL::Schema::Mutation
4+
argument :email, String, required: true
5+
argument :password, String, required: true
6+
7+
field :authenticable, GraphqlDevise::Types::AuthenticableType, null: true
8+
9+
def resolve(email:, password:)
10+
resource = context[:resource_class].find_by(email: email)
11+
12+
if resource && (!resource.respond_to?(:active_for_authentication?) || resource.active_for_authentication?)
13+
valid_password = resource.valid_password?(password)
14+
if (resource.respond_to?(:valid_for_authentication?) && !resource.valid_for_authentication? { valid_password }) || !valid_password
15+
return {}
16+
end
17+
18+
auth_headers = resource.create_new_auth_token
19+
context[:response].headers.merge!(auth_headers)
20+
21+
{ authenticable: resource }
22+
elsif resource && !(!resource.respond_to?(:active_for_authentication?) || resource.active_for_authentication?)
23+
if resource.respond_to?(:locked_at) && resource.locked_at
24+
return {}
25+
else
26+
return {}
27+
end
28+
else
29+
return {}
30+
end
31+
end
32+
end
33+
end
34+
end
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module GraphqlDevise
2+
class Schema < GraphQL::Schema
3+
mutation(GraphqlDevise::Types::MutationType)
4+
query(GraphqlDevise::Types::QueryType)
5+
end
6+
end
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module GraphqlDevise
2+
module Types
3+
class AuthenticableType < GraphQL::Schema::Object
4+
field :email, String, null: false
5+
end
6+
end
7+
end
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module GraphqlDevise
2+
module Types
3+
class MutationType < GraphQL::Schema::Object
4+
field :login, mutation: GraphqlDevise::Mutations::Login
5+
end
6+
end
7+
end
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module GraphqlDevise
2+
module Types
3+
class QueryType < GraphQL::Schema::Object
4+
end
5+
end
6+
end

0 commit comments

Comments
 (0)