Skip to content

Instantly share code, notes, and snippets.

Avatar
🍄

Robert Mosolgo rmosolgo

🍄
View GitHub Profile
@rmosolgo
rmosolgo / analyzer.rb
Last active Nov 12, 2019
Calculating "complexity" in a duplication-agnostic way
View analyzer.rb
inputs = [
# These inputs are "like" GraphQL queries because:
# - Some keys are scalars, others are nested hashes
# - Keys may be repeated
#
# Pretend they're like `{ field => complexity }` pairs.
[{ a: 1, b: 1 }],
[{ a: 1, b: 1 }, { b: 1 }],
[{ a: 1, b: 1, c: { d: 1 } }, { c: { d: 1, e: 1 } }]
]
@rmosolgo
rmosolgo / base_mutation.rb
Last active Sep 17, 2020
A base mutation that checks for selections on `errors`
View base_mutation.rb
# A base mutation that adds an errors field to all subclasses, and before resolving, it checks to make sure that the caller selected `errors`.
#
# (You could use `GraphQL::Schema::Mutation` as a base class, too.)
class Mutations::BaseMutation < GraphQL::Schema::RelayClassicMutation
# Add the errors field to all mutations
field :errors, [Types::MutationError], null: false
# Inject `lookahead` to the resolve method
extras [:lookahead]
def resolve_with_support(lookahead:, **kwargs)
@rmosolgo
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
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
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
rmosolgo / Gemfile
Last active Apr 28, 2020
GraphQL Ruby Subscriptions
View Gemfile
source 'https://rubygems.org'
gem "graphql", github: "rmosolgo/graphql-ruby", branch: "subscriptions"
gem "sinatra"
gem "thin"
@rmosolgo
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
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? `Time.now.to_f`?
#
# When you define your schema, you could add this middleware:
#
# ```
# MySchema.middleware << PerformanceTrackingMiddleware.new
# ```
#
@rmosolgo
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
end
MutationType = GraphQL::ObjectType.define do
View keybase.md

Keybase proof

I hereby claim:

  • I am rmosolgo on github.
  • I am rmosolgo (https://keybase.io/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:

You can’t perform that action at this time.