Skip to content

Instantly share code, notes, and snippets.

View amkisko's full-sized avatar
😺
Hymyillen suora selkä!

Andrei GitHub Makarov amkisko

😺
Hymyillen suora selkä!
  • Kisko Labs
  • Helsinki, Finland
View GitHub Profile
@amkisko
amkisko / rake_task_invoke.rb
Created March 19, 2020 09:55
Invoke rake task from Rails console
require "rake"
RailsApp::Application.load_tasks
Rake::Task["my_task"].invoke
@amkisko
amkisko / basic_schema.rb
Last active April 6, 2020 20:21
Basic schema for graphql-ruby
class BasicSchema < GraphQL::Schema
default_max_page_size 25
use(GraphQL::Execution::Interpreter)
use(GraphQL::Analysis::AST)
query_analyzer(QueryAnalyzer)
use(GraphQL::Execution::Errors)
use(BatchLoader::GraphQL)
@amkisko
amkisko / basic_controller.rb
Created April 6, 2020 20:20
Basic controller for Rails app with graphql-ruby
class ApplicationController < ActionController::Base
include Pundit
skip_forgery_protection
rescue_from(StandardError) do |err|
log_error(err)
if Rails.env.development?
render_error err
else
render_error Error::InternalServerError.new(code: err.class.to_s.sub(/:+/, "_").underscore.upcase)
@amkisko
amkisko / exceptions_middleware.rb
Created April 6, 2020 20:23
Exceptions middleware for handling low-level errors for Rails with graphql-ruby app
# NOTE: Add `config.exceptions_app = ExceptionsMiddleware.new` to application.rb
class ExceptionsMiddleware
def initialize
end
def call(env)
error = env["action_dispatch.exception"]
request = ActionDispatch::Request.new(env)
@amkisko
amkisko / rack_attack.rb
Created April 6, 2020 20:25
Rack::Attack initializer for Rails with graphql-ruby
class Rack::Attack
if Rails.env.development? || Rails.env.test?
$rack_attack_cache ||= ActiveSupport::Cache::MemoryStore.new
Rack::Attack.cache.store = $rack_attack_cache
end
# safelist("allow-localhost") do |req|
# ["127.0.0.1", "::1"].include? req.ip
# end
@amkisko
amkisko / base_object.rb
Created April 6, 2020 20:26
Base object for graphql-ruby
module Objects
class BaseObject < GraphQL::Schema::Object
field_class Fields::BaseField
field :destroyed, Boolean, "Has the record been destroyed?", null: true
def destroyed
object.destroyed? if object.respond_to?(:destroyed?)
end
def batch_load(object_scope, object_id, id_attr)
@amkisko
amkisko / base_mutation.rb
Created April 6, 2020 20:27
Base mutation for graphql-ruby
module Mutations
class BaseMutation < GraphQL::Schema::Mutation
argument_class(Arguments::BaseArgument)
field_class(Fields::BaseField)
object_class(Objects::BaseObject)
private def current_user
context[:current_user]
end
@amkisko
amkisko / base_field.rb
Created April 6, 2020 20:28
Base field for graphql-ruby
module Fields
class BaseField < GraphQL::Schema::Field
argument_class(Arguments::BaseArgument)
attr_accessor :name_suffix
def initialize(
*args, authorize: nil, require_authentication: false, **kwargs, &block
)
@authorize = authorize
@amkisko
amkisko / graphql_user_controller.rb
Created April 6, 2020 20:32
Application controller for GraphQL UserSchema endpoint
class GraphqlUserController < ApplicationController
protect_from_forgery with: :null_session
def execute
variables = ensure_hash(params[:variables])
query = params[:query]
operation_name = params[:operationName]
context = {
request: request,
current_user: current_user,
@amkisko
amkisko / query_analyzer.rb
Created April 6, 2020 20:34
Query analyser with user authentication for graphql-ruby
class QueryAnalyzer < GraphQL::Analysis::AST::Analyzer
QUERY_ALLOWED_DEPTH = 6
def initialize(query)
super
@max_depth = 0
@current_depth = 0
end
def on_enter_field(_node, _parent, _visitor)