Skip to content

Instantly share code, notes, and snippets.

Avatar

Vladimir Dementyev palkan

View GitHub Profile
@palkan
palkan / bulletify.rb
Created Sep 7, 2020
Bulletify: RSpec helpers to run Bullet in tests
View bulletify.rb
RSpec.shared_context "bullet" do
before(:each) do
Bullet.enable = true
Bullet.bullet_logger = true
Bullet.raise = true # raise an error if N+1 query occurs
Bullet.start_request
end
after(:each) do
Bullet.perform_out_of_channel_notifications if Bullet.notification?
@palkan
palkan / compile.c
Created Jun 28, 2020
compile.c ADD_STACKPRINT
View compile.c
#define ADD_STACKPRINT(label, depth) do {\
ADD_INSN1(ret, line, putobject, rb_fstring_lit(label));\
ADD_SEND(ret, line, rb_intern("putme"), INT2FIX(0));\
ADD_INSN(ret, line, pop);\
for (int x = 0; x < depth ; x++ ){\
ADD_INSN1(ret, line, topn, INT2FIX(x));\
ADD_SEND(ret, line, rb_intern("putme"), INT2FIX(0));\
ADD_INSN(ret, line, pop);\
}\
} while(0)
@palkan
palkan / connection.rb
Last active Feb 26, 2021
Action/AnyCable + Apartment
View connection.rb
module ApplicationCable
class Connection < ActionCable::Base::Connection
# we need to keep tenant information for subsequent messages,
# so let's store it as an identifier
identified_by :tenant
def connect
# assuming you store current tenant in session
self.tenant = request.session[:current_tenant]
reject_unauthorized_connection unless tenant
@palkan
palkan / Dockefile
Last active May 14, 2020
hybrid dockerfile
View Dockefile
ARG RUBY_VERSION
# === Base image ===
FROM ruby:${RUBY_VERSION}-slim-buster as base
ARG NODE_MAJOR
ARG POSTGRES_VERSION
ARG BUNDLER_VERSION
ARG YARN_VERSION
# Common dependencies
@palkan
palkan / .rubocop_strict.yml
Created Mar 18, 2020
rubocop-strict config
View .rubocop_strict.yml
# Inherit from TODO here to make sure we enforce the rules below
# (and TODO is ignored)
inherit_from:
- .rubocop_todo.yml
Lint/Debugger: # don't leave binding.pry
Enabled: true
Exclude: []
RSpec/Focus: # run ALL tests on CI
@palkan
palkan / .rubocop_rails.yml
Created Mar 18, 2020
rubocop-rails config
View .rubocop_rails.yml
# Based on removed standard configuration:
# https://github.com/testdouble/standard/commit/94d133f477a5694084ac974d5ee01e8a66ce777e#diff-65478e10d5b2ef41c7293a110c0e6b7c
require:
- rubocop-rails
Rails/ActionFilter:
Enabled: true
EnforcedStyle: action
Include:
@palkan
palkan / .rubocop_rspec.yml
Created Mar 18, 2020
rubocop-rspec config
View .rubocop_rspec.yml
require:
- rubocop-rspec
RSpec/Focus:
Enabled: true
RSpec/EmptyExampleGroup:
Enabled: true
RSpec/EmptyLineAfterExampleGroup:
@palkan
palkan / tracer.rb
Created Feb 13, 2020
[GraphQL] verify batch load
View tracer.rb
# frozen_string_literal: true
module Graphql
# This module implements the functionality to track whether the batch loading is
# required for a field to solve N+1 problem.
#
# If you want to check whether a particular field doesn't use batch loading in vain,
# mark it with `verify_batch_load: true` option.
#
# By default, it logs the following information when detects a possible N+1 (thus, confirms that batch loading is required).
@palkan
palkan / pp.rb
Created Feb 13, 2020
[GraphQL] @pp profiling
View pp.rb
# frozen_string_literal: true
module Directives
class Pp < GraphQL::Schema::Directive
class Profiler < Types::BaseEnum
graphql_name "PpProfiler"
description "Available profilers for @pp directive"
value :mem
value :stack
@palkan
palkan / 00_Readme.md
Last active Sep 23, 2021
graphql-ruby fragment caching
View 00_Readme.md

PoC: GraphQL Ruby fragment caching

This example demonstrates how we can cache the response fragments in graphql-ruby.

Existing solutions only allow caching resolved values but not all the GraphQL machinery (validation, coercion, whatever).

Caching response parts (in case of Ruby, sub-Hashes) is much more efficient.

Benchmarks