Skip to content

Instantly share code, notes, and snippets.

@rmosolgo
rmosolgo / list_of_nodes_example.rb
Created April 1, 2025 22:24
Avoiding N+1 loads in different GraphQL subtrees
require "bundler/inline"
gemfile do
gem "graphql", "2.5.0"
gem "activerecord", require: "active_record"
gem "sqlite3"
gem "logger"
end
# Set up the database for the example
@rmosolgo
rmosolgo / search_object_lookahead_connection_example.rb
Created March 24, 2025 17:29
SearchObjectGraphQL lookahead example
require 'bundler/inline'
gemfile do
gem "graphql", "2.4.15"
gem "search_object_graphql", "1.0.5"
gem "activerecord", require: "active_record"
gem "sqlite3"
end
require 'search_object'
require 'search_object/plugin/graphql'
@rmosolgo
rmosolgo / complexity_example.rb
Created March 12, 2025 01:03
Exposting GraphQL-Ruby Query Complexity Value to Clients
require "bundler/inline"
gemfile do
gem "graphql", "2.4.12"
end
# This example shows three ways of showing a query's complexity to clients.
# All three cases require a customized analyzer which adds the value to be used later.
class MySchema < GraphQL::Schema
class MaxComplexityWithReport < GraphQL::Analysis::MaxQueryComplexity
@rmosolgo
rmosolgo / example_custom_connection_system.rb
Last active March 25, 2025 09:23
Implementing a new, custom connection system in GraphQL-Ruby and releasing it in a new API version
require "bundler/inline"
gemfile do
gem "diffy", "3.4.3"
gem "graphql", "2.4.11"
gem "graphql-enterprise", "1.5.6", source: "https://gems.graphql.pro"
end
# dummy data
THINGS = [{name: "Airplane"}, {name: "Bagel"}, {name: "Calendar"}, {name: "Daffodil"}]
@rmosolgo
rmosolgo / changeset_directive.rb
Created February 5, 2025 13:38
Assigning a GraphQL-Ruby changeset version from a directive in the query
require "bundler/inline"
gemfile do
gem "graphql", "2.4.8", source: "https://rubygems.org"
gem "graphql-enterprise", source: "https://gems.graphql.pro"
end
class MySchema < GraphQL::Schema
class SwitchFields < GraphQL::Enterprise::Changeset
release "2025-02-01"
@rmosolgo
rmosolgo / unsorted_enum_values.rb
Last active January 18, 2025 15:36
Custom GraphQL-Ruby Schema Printer Which Doesn't Sort Enum Values
require "bundler/inline"
gemfile do
gem "graphql", "2.4.8"
end
class MySchema < GraphQL::Schema
class Vegetable < GraphQL::Schema::Enum
value :ZUCCHINI
value :ARTICHOKE
@rmosolgo
rmosolgo / connection_scoping.rb
Last active January 8, 2025 00:07
Removing unauthorized items from a connection in GraphQL-Ruby using scope_items
require "bundler/inline"
gemfile do
gem "graphql", "2.4.8"
end
THINGS = [
{ name: "Camcorder" },
{ name: "Magnifying Glass" },
{ name: "Disappearing Ink" },
@rmosolgo
rmosolgo / non_null_edge_node.rb
Created January 3, 2025 14:40
Make Edge.node non-null in GraphQL-Ruby
require "bundler/inline"
gemfile do
gem "graphql", "2.4.7"
end
class MySchema < GraphQL::Schema
class BaseEdge < GraphQL::Types::Relay::BaseEdge
node_nullable(false)
end
@rmosolgo
rmosolgo / custom_connection_class_examples.rb
Created December 27, 2024 14:46
Ways to customize automatically generated connection types in GraphQL-Ruby
require "bundler/inline"
gemfile do
gem "graphql", "2.4.8"
end
class Thing < GraphQL::Schema::Object
field :name, String
# One option is to extend this method to rename the returned type.
@rmosolgo
rmosolgo / cache_example.rb
Last active December 10, 2024 21:57
Caching top-level lists with GraphQL-Ruby when new items are created
require "bundler/inline"
gemfile do
gem "graphql", "2.4.7"
gem "graphql-enterprise", source: "https://gems.graphql.pro"
gem "activerecord"
gem "sqlite3"
end
# Set up the database for the example