Skip to content

Instantly share code, notes, and snippets.


Robert Mosolgo rmosolgo

Block or report user

Report or block rmosolgo

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
rmosolgo / example.rb
Created Apr 18, 2019
Accessing directives in the Schema SDL with GraphQL-Ruby
View example.rb
require "graphql"
# A schema definition with field-level directives
schema = GraphQL::Schema.from_definition <<-GRAPHQL
type Query {
totalScore: Int! @mock
type Mutation {
incrementScore(by: Int = 1): Int! @mock(with: "SomeClassName")
rmosolgo / page_example.rb
Created Apr 10, 2019
Generic page number / per-page pagination with GraphQL-Ruby
View page_example.rb
# This is a full-blown offset-based pagination system modelled after GraphQL-Ruby's
# built-in connections. It has a few different elements:
# - `::Page`, a plain ol' Ruby class for modeling _pages_ of things.
# This class handles applying pagination arguments to lists (Arrays and AR::Relations)
# and provides metadata about pagination. (Similar to `will_paginate`.)
# - `Schema::BasePage` is a generic GraphQL-Ruby object type. It's never used directly,
# but it can generate subclasses which wrap _specific_ object types in the schema.
# - `Schema::BaseObject.page_type` is a convenience method for generating page types
# from your object types. You could leave this out and make subclasses with plain ol'
rmosolgo / query_printer.rb
Created Mar 11, 2019
A query printer for GraphQL-Ruby that redacts strings
View query_printer.rb
# frozen_string_literal: true
# QueryPrinter is a custom GraphQL Ruby
# printer used to print sanitized queries. It inlines provided variables
# within the query for facilitate logging and analysis of queries.
# The printer assumes the query is valid.
# Since the GraphQL Ruby AST for a GraphQL query doesnt contain any reference
# on the type of fields or arguments, we have to track the current object, field
rmosolgo / Gemfile
Last active Jun 8, 2019
GraphQL Ruby Subscriptions
View Gemfile
source ''
gem "graphql", github: "rmosolgo/graphql-ruby", branch: "subscriptions"
gem "sinatra"
gem "thin"
rmosolgo / output-2.3.txt
Created Feb 20, 2017
Compare creation & access times for key-value storage in Ruby (Class, Struct, OpenStruct, Hash)
View output-2.3.txt
~/code/graphql $ ruby -v
ruby 2.3.3p222 (2016-11-21 revision 56859) [x86_64-darwin14]
~/code/graphql $ ruby struct_bench.rb
Create and Access
Warming up --------------------------------------
Hash#[] 118.707k i/100ms
Hash#fetch 113.228k i/100ms
Struct 195.640k i/100ms
rmosolgo / performance_tracking_middleware.rb
Created Jul 7, 2016
An idea for tracking fields in GraphQL
View performance_tracking_middleware.rb
# An untested idea to add performance tracking to graphql queries
# TODO: what's the best way of tracking time in the app? ``?
# When you define your schema, you could add this middleware:
# ```
# MySchema.middleware <<
# ```
rmosolgo / graphql_state_machine.rb
Created Jun 2, 2016
Return an error message in a GraphQL field with graphql-ruby
View graphql_state_machine.rb
# This object returns the _new_ state and any error message during the transition.
# So in the response, you could check for the presence of an error message.
# (In the case of an error, presumably the state would not change.)
StateMachineTransitionType = GraphQL::ObjectType.define do
name "StateMachineTransition"
field :error_message, types.String
field :state, types.String # Could also define StateMachineStateEnum
MutationType = GraphQL::ObjectType.define do

Keybase proof

I hereby claim:

  • I am rmosolgo on github.
  • I am rmosolgo ( on keybase.
  • I have a public key whose fingerprint is 77C7 1234 2A6A C5E7 CF1F 2F1E 949B E1E2 5F48 E7AE

To claim this, I am signing this object:

rmosolgo / func_bench.rb
Last active Jun 5, 2017
Calling a pure function in Ruby (benchmark)
View func_bench.rb
require "benchmark/ips"
# Let's say you want pure functions in Ruby.
# Which form of "packaging" is the fastest?
pure_func_proc = -> (a, b) { a + b }
pure_func_lambda = lambda { |a, b| a + b }
def pure_func_global(a, b)
a + b
View Chrome "Form Data" for request.xml
printerName:DYMO LabelWriter 450 Turbo
labelXml:<?xml version="1.0" encoding="utf-8"?>
<DieCutLabel Version="8.0" Units="twips">
<PaperName>30256 Shipping</PaperName>
<RoundRectangle X="0" Y="0" Width="3331" Height="5715" Rx="270" Ry="270"/>
You can’t perform that action at this time.