Skip to content

Instantly share code, notes, and snippets.

@rmosolgo
rmosolgo / custom_client_mutation_id.rb
Last active February 7, 2024 17:01
Custom clientMutationId directives in GraphQL-Ruby
require "bundler/inline"
gemfile do
gem "graphql", "2.2.6"
end
class MySchema < GraphQL::Schema
class Undocumented < GraphQL::Schema::Directive
locations(FIELD_DEFINITION, INPUT_FIELD_DEFINITION)
description "This should be removed our generated docs"
@rmosolgo
rmosolgo / validate_schema_example.rb
Created January 31, 2024 18:43
Validate using one GraphQL schema, but execute using another schema
require "bundler/inline"
gemfile do
gem "graphql", "2.2.6"
end
class NewSchema < GraphQL::Schema
class Query < GraphQL::Schema::Object
field :old_field, Integer
def old_field = 1
@rmosolgo
rmosolgo / deprecation_warnings.rb
Created January 11, 2024 14:57
GraphQL-Ruby 1.12 field deprecation warnings example
require "bundler/inline"
gemfile do
gem "graphql", "1.12.24"
end
# Newer graphql-ruby versions have `context.repsonse_extensions` which automatically
# adds data to the result["extensions"] field. But for 1.12, you have to add it manually.
# @see https://graphql-ruby.org/queries/response_extensions.html
class MySchema < GraphQL::Schema
@rmosolgo
rmosolgo / parallel_graphql.rb
Last active December 6, 2023 14:43
Parallel Dataloader Execution in GraphQL-Ruby
require "bundler/inline"
gemfile do
gem "graphql"
gem "concurrent-ruby"
end
class MySchema < GraphQL::Schema
# This source assumes that each record requires an expensive database query of its *own*.
# If that's not the case, then remove `def initialize` below
@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:)