Skip to content

Instantly share code, notes, and snippets.

@rmosolgo
rmosolgo / defer_example.rb
Created December 4, 2023 15:47
@defer inside fragments
require "bundler/inline"
gemfile do
gem "graphql", "1.12.24"
gem "graphql-pro", "1.24.15"
end
class Schema < GraphQL::Schema
class Thing < GraphQL::Schema::Object
field :name, String, null: false
@rmosolgo
rmosolgo / defer_example.rb
Last active November 20, 2023 17:20
Using GraphQL-Ruby 1.12 with @defer and { backtrace: true }
require "bundler/inline"
gemfile do
gem "graphql", "1.12.18"
gem "graphql-pro", "1.24.13"
gem "graphql-batch"
end
# Monkey-patch this to avoid deleting keys from multiplex.context
# in the ensure block below.
@rmosolgo
rmosolgo / single_selection.rb
Created November 14, 2023 15:02
Only allow one root selection with GraphQL-Ruby
require "bundler/inline"
gemfile do
gem "graphql", "2.1.6"
end
class MySchema < GraphQL::Schema
class Query < GraphQL::Schema::Object
field :f1, Int
field :f2, Int
@rmosolgo
rmosolgo / multi_tenant_dataloader.rb
Created October 19, 2023 18:15
Multi-tenant querying with GraphQL::Dataloader
require "bundler/inline"
gemfile do
gem "graphql"
end
# Here's a pretend DB with three tenants:
DATA = {
"food_lion" => [
{ id: 1, name: "Mayo" },
@rmosolgo
rmosolgo / custom_connection_type.rb
Created October 19, 2023 17:43
Custom connection-like pagination system in GraphQL-Ruby
require "bundler/inline"
gemfile do
gem "graphql"
end
class Schema < GraphQL::Schema
# In your base field class, create a new field extension
# and configure your fields to use that one instead of GraphQL-Ruby's default.
@rmosolgo
rmosolgo / dataloader_example.rb
Last active August 1, 2023 18:13
Parallel I/O with GraphQL::Dataloader
require "bundler/inline"
gemfile do
gem "graphql"
gem "libev_scheduler"
end
class MySchema < GraphQL::Schema
class FetchThings < GraphQL::Dataloader::Source
def initialize(starting_at:)
@rmosolgo
rmosolgo / introspection_hiding.rb
Created July 13, 2023 15:29
Hiding introspection fields with GraphQL-Ruby
require "bundler/inline"
gemfile do
gem "graphql", "2.0.24"
end
class MySchema < GraphQL::Schema
module CustomIntrospection
module HideIntrospectionByContext
def visible?(ctx)
@rmosolgo
rmosolgo / runtime_benchmark.rb
Last active January 2, 2024 14:35
GraphQL-Ruby runtime benchmark
# This benchmark aims to test GraphQL-Ruby's runtime performance
# _except_ for parsing.
require "bundler/inline"
gemfile(true) do
source "https://rubygems.org"
# Pick a GraphQL version:
# gem "graphql", "1.13.19"
gem "graphql", "~>2.0"
gem "benchmark-ips"
@rmosolgo
rmosolgo / legacy_changesets.rb
Created December 5, 2022 15:05
Moving legacy fields to a GraphQL::Enterprise::Changeset
require "bundler/inline"
gemfile do
gem "graphql", path: "./"
gem "graphql-enterprise", source: "https://gems.graphql.pro"
end
class BaseField < GraphQL::Schema::Field
include GraphQL::Enterprise::Changeset::FieldIntegration
end
@rmosolgo
rmosolgo / union_spread.rb
Last active October 11, 2022 12:56
Spreading an inteface fragment inside a union
require "bundler/inline"
gemfile do
gem "graphql", "1.11.2"
end
class Schema < GraphQL::Schema
class Error < GraphQL::Schema::Object
field :message, String, null: false
end