-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #330 from platanus/f/graphql
Feat/ Beta support for graphql
- Loading branch information
Showing
21 changed files
with
395 additions
and
11 deletions.
There are no files selected for viewing
This file contains 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 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,55 @@ | ||
class GraphqlController < ApplicationController | ||
# If accessing from outside this domain, nullify the session | ||
# This allows for outside API access while preventing CSRF attacks, | ||
# but you'll have to authenticate your user separately | ||
# protect_from_forgery with: :null_session | ||
|
||
def execute | ||
variables = prepare_variables(params[:variables]) | ||
query = params[:query] | ||
operation_name = params[:operationName] | ||
context = { current_user: get_current_user } | ||
result = GqlSampleSchema.execute(query, variables: variables, context: context, operation_name: operation_name) | ||
render json: result | ||
rescue => e | ||
raise e unless Rails.env.development? | ||
handle_error_in_development e | ||
end | ||
|
||
private | ||
|
||
# Handle variables in form data, JSON body, or a blank value | ||
def prepare_variables(variables_param) | ||
case variables_param | ||
when String | ||
if variables_param.present? | ||
JSON.parse(variables_param) || {} | ||
else | ||
{} | ||
end | ||
when Hash | ||
variables_param | ||
when ActionController::Parameters | ||
variables_param.to_unsafe_hash # GraphQL-Ruby will validate name and type of incoming variables. | ||
when nil | ||
{} | ||
else | ||
raise ArgumentError, "Unexpected parameter: #{variables_param}" | ||
end | ||
end | ||
|
||
def handle_error_in_development(e) | ||
logger.error e.message | ||
logger.error e.backtrace.join("\n") | ||
|
||
render json: { errors: [{ message: e.message, backtrace: e.backtrace }], data: {} }, status: 500 | ||
end | ||
|
||
def get_current_user | ||
if request.headers['Authorization'] | ||
_, token = request.headers['Authorization'].split | ||
decoded_token = JWT.decode token, ENV['HMAC_SECRET'], true, { algorithm: 'HS256' } | ||
User.find(decoded_token.first["id"]) | ||
end | ||
end | ||
end |
23 changes: 23 additions & 0 deletions
23
lib/potassium/assets/app/graphql/mutations/login_mutation.rb
This file contains 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,23 @@ | ||
require 'jwt' | ||
|
||
class Mutations::LoginMutation < Mutations::BaseMutation | ||
null true | ||
|
||
argument :email, String, required: true | ||
argument :password, String, required: true | ||
|
||
|
||
field :token, String, null: true | ||
|
||
def resolve(email:, password:) | ||
user = User.find_by(email: email) | ||
if user&.valid_password?(password) | ||
payload = { id: user.id, email: user.email, exp: (Time.zone.now + 24.hours).to_i } | ||
token = JWT.encode payload, ENV['HMAC_SECRET'], 'HS256' | ||
return { token: token } | ||
end | ||
GraphQL::ExecutionError.new("User or Password invalid") | ||
rescue ActiveRecord::RecordInvalid => e | ||
GraphQL::ExecutionError.new("Invalid input: #{e.record.errors.full_messages.join(', ')}") | ||
end | ||
end |
This file contains 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,4 @@ | ||
module Queries | ||
class BaseQuery < GraphQL::Schema::Resolver | ||
end | ||
end |
This file contains 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,4 @@ | ||
module Types::Base | ||
class BaseArgument < GraphQL::Schema::Argument | ||
end | ||
end |
This file contains 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,4 @@ | ||
module Types::Base | ||
class BaseEnum < GraphQL::Schema::Enum | ||
end | ||
end |
This file contains 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,5 @@ | ||
module Types::Base | ||
class BaseField < GraphQL::Schema::Field | ||
argument_class Types::Base::BaseArgument | ||
end | ||
end |
5 changes: 5 additions & 0 deletions
5
lib/potassium/assets/app/graphql/types/base/base_input_object.rb
This file contains 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,5 @@ | ||
module Types::Base | ||
class BaseInputObject < GraphQL::Schema::InputObject | ||
argument_class Types::Base::BaseArgument | ||
end | ||
end |
7 changes: 7 additions & 0 deletions
7
lib/potassium/assets/app/graphql/types/base/base_interface.rb
This file contains 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,7 @@ | ||
module Types::Base | ||
module BaseInterface | ||
include GraphQL::Schema::Interface | ||
|
||
field_class Types::Base::BaseField | ||
end | ||
end |
This file contains 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,5 @@ | ||
module Types::Base | ||
class BaseObject < GraphQL::Schema::Object | ||
field_class Types::Base::BaseField | ||
end | ||
end |
This file contains 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,4 @@ | ||
module Types::Base | ||
class BaseScalar < GraphQL::Schema::Scalar | ||
end | ||
end |
This file contains 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,4 @@ | ||
module Types::Base | ||
class BaseUnion < GraphQL::Schema::Union | ||
end | ||
end |
This file contains 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,10 @@ | ||
module Types | ||
class MutationType < Types::Base::BaseObject | ||
# TODO: remove me | ||
field :test_field, String, null: false, | ||
description: "An example field added by the generator" | ||
def test_field | ||
"Hello World" | ||
end | ||
end | ||
end |
This file contains 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,13 @@ | ||
module Types | ||
class QueryType < Types::Base::BaseObject | ||
# Add root-level fields here. | ||
# They will be entry points for queries on your schema. | ||
|
||
# TODO: remove me | ||
field :test_field, String, null: false, | ||
description: "An example field added by the generator" | ||
def test_field | ||
"Hello World!" | ||
end | ||
end | ||
end |
This file contains 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,20 @@ | ||
# config/initializers/graphql_playground.rb | ||
# All config options have a default that should work out of the box | ||
if Rails.env.development? | ||
GraphqlPlayground::Rails.configure do |config| | ||
# config.headers = { | ||
# 'X-Auth-Header' => ->(view_context) { "123" } | ||
# } | ||
# config.title = "Playground" | ||
# config.csrf = true | ||
# config.playground_version = "latest" | ||
# # Ideally the assets would be added to your projects `vendor/assets` directories | ||
# config.favicon = "/assets/playground.ico" | ||
# config.playground_js_url = "/assets/playground.js" | ||
# config.playground_css_url = "/assets/playground.css" | ||
# # see: https://github.com/prisma-labs/graphql-playground#settings | ||
config.settings = { | ||
"schema.polling.enable": false | ||
} | ||
end | ||
end |
This file contains 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 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 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
Oops, something went wrong.