Skip to content

Instantly share code, notes, and snippets.

@rmosolgo
rmosolgo / ar-connections-test.rb
Last active November 23, 2021 14:20
GraphQL Connections With ActiveRecord Connection Switching
require "bundler/inline"
gemfile do
gem "graphql", "1.12.20"
gem "graphql-pro", "1.20.2"
gem "rails"
gem "sqlite3"
end
require "active_record"
require "bundler/inline"
require "logger"
gemfile do
gem "graphql", "1.12.19"
gem "graphql-pro", "1.20.1"
gem "redis"
end
# Here's an example of caching parsed AST documents for persisted queries.
require "bundler/inline"
gemfile do
gem "graphql", "1.12.18"
end
class Schema < GraphQL::Schema
class Thing < GraphQL::Schema::Object
field :name, String, null: true
field :other_thing, self, null: true
# frozen_string_literal: true
#
# LRU cache based on Ruby 1.9+ ordered hash inspired by Sam Saffron: https://stackoverflow.com/a/16161783
#
# @example Caching valid query documents in a Rails controller
#
# class GraphqlController < ApplicationController
# # TODO: not thread-safe. See https://github.com/samsaffron/lru_redux for a thread-safe LRU cache
# PARSED_QUERY_CACHE = GraphQLParseAndValidateCache.new(schema: MySchema, max_entries: 20)
#
require "bundler/inline"
gemfile do
gem "graphql", "~>1.12.0"
end
class BaseField < GraphQL::Schema::Field
def initialize(*args, federated: true, **kwargs, &block)
@federated = federated
super(*args, **kwargs, &block)
@rmosolgo
rmosolgo / pundit_example.rb
Created February 19, 2021 12:27
Example of GraphQL::Pro pundit_integration with node field
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 December 25, 2020 18:50
fiber_batch_loader.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 November 12, 2019 15:55
Calculating "complexity" in a duplication-agnostic way
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 September 17, 2020 18:16
A base mutation that checks for selections on `errors`
# 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 April 18, 2019 16:14
Accessing directives in the Schema SDL with GraphQL-Ruby
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")