Skip to content

Commit c77457e

Browse files
Merge pull request #2 from graphql-device/add-engine-base-structure
PoC first login mutation
2 parents c980bb5 + cfe7cd8 commit c77457e

27 files changed

Lines changed: 558 additions & 41 deletions

File tree

Gemfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
source "https://rubygems.org"
1+
source 'https://rubygems.org'
22

33
gemspec
44

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
module GraphqlDevise
2+
class ApplicationController < DeviseTokenAuth::ApplicationController
3+
end
4+
end
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
require_dependency 'graphql_devise/application_controller'
2+
3+
module GraphqlDevise
4+
class GraphqlController < ApplicationController
5+
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
49+
end
50+
end
51+
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
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
module GraphqlDevise
2+
module ApplicationHelper
3+
end
4+
end
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
module GraphqlDevise
2+
class ApplicationJob < ActiveJob::Base
3+
end
4+
end

0 commit comments

Comments
 (0)