Skip to content

Instantly share code, notes, and snippets.

Avatar
🍄

Robert Mosolgo rmosolgo

🍄
View GitHub Profile
@rmosolgo
rmosolgo / pundit_example.rb
Created Feb 19, 2021
Example of GraphQL::Pro pundit_integration with node field
View pundit_example.rb
require "bundler/inline"
gemfile do
gem "pundit", "2.1.0"
gem "graphql", "1.12.5"
source "https://gems.graphql.pro" do
gem "graphql-pro", "1.17.6"
end
end
@rmosolgo
rmosolgo / fiber_test.rb
Created Dec 25, 2020
fiber_batch_loader.rb
View fiber_test.rb
require "fiber"
# Set up a pretend database
DATA = {
1 => "Hannah Coulter",
2 => "Jayber Crow",
3 => "That Distant Land"
}
# This loader will fetch items in batches of IDs
@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 Mar 23, 2021
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
# ```
#