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
require "rake" | |
RailsApp::Application.load_tasks | |
Rake::Task["my_task"].invoke |
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
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) |
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
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) |
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
# 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) |
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
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 |
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
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) |
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
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 |
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
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 |
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
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, |
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
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) |