Skip to content

Instantly share code, notes, and snippets.

@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
@rmosolgo
rmosolgo / resolver_example.rb
Created December 9, 2024 14:45
GraphQL-Ruby: using the same resolver with different return types (list vs. connection)
require "bundler/inline"
gemfile do
gem "graphql", "2.4.7"
end
class MySchema < GraphQL::Schema
class GetItems < GraphQL::Schema::Resolver
def resolve
[
@rmosolgo
rmosolgo / top_level_argument.rb
Created November 27, 2024 17:47
Get a top-level argument from a nested field in GraphQL-Ruby
require "bundler/inline"
gemfile do
gem "graphql", "2.4.4"
end
class MySchema < GraphQL::Schema
class NestedThingInput < GraphQL::Schema::InputObject
argument :env, String
end
@rmosolgo
rmosolgo / aync_graphql_auth.rb
Last active November 19, 2024 15:06
GraphQL-Ruby async authorization example
require "bundler/inline"
gemfile do
gem "graphql", "2.4.4"
gem "graphql-pro", "1.29.4"
gem "async"
gem "pundit"
end
class ThingPolicy
@rmosolgo
rmosolgo / auto_dataloader.rb
Last active November 7, 2024 12:10
Automatically applying Dataloader to GraphQL fields
require "bundler/inline"
gemfile do
gem "graphql"
gem "sqlite3"
gem "activerecord"
end
# Set up the database for the example
require "active_record"
@rmosolgo
rmosolgo / devise_cache.rb
Created October 15, 2024 15:30
GraphQL ObjectCache + GraphqlDevise
# This is a demonstration for using GraphQL-Enterprise's ObjectCache
# along with GraphqlDevise.
#
# The trick is that, by default, GraphqlDevise provides its own query type
# which uses its own BaseField. So, to mix in `GraphQL::Enterprise::ObjectCache::FieldIntegration`,
# you have to make your own BaseField class and include GraphqlDevise's `FieldAuthentication` module,
# then do the "existing schema" setup as described here:
# https://github.com/graphql-devise/graphql_devise/tree/master?tab=readme-ov-file#mounting-operations-in-an-existing-schema
#
require "bundler/inline"
@rmosolgo
rmosolgo / argument_example.rb
Last active September 24, 2024 14:11
Required input argument example
require "bundler/inline"
gemfile do
gem "graphql", "2.3.16"
gem "graphql-enterprise", "1.5.2", source: "https://gems.graphql.pro"
end
class MySchema < GraphQL::Schema
class MakeInputsRequired < GraphQL::Enterprise::Changeset
release "2024-01-01"
@rmosolgo
rmosolgo / underscore_page_info.rb
Created September 6, 2024 14:35
GraphQL-Ruby underscore-cased page info field for connections
require "bundler/inline"
gemfile do
gem "graphql"
end
class MySchema < GraphQL::Schema
class BaseField < GraphQL::Schema::Field
def initialize(*args, camelize: false, **kwargs, &block)
super
@rmosolgo
rmosolgo / remove_unsubscribed.rb
Last active August 28, 2024 13:54
Remove orphaned subscriptions
# This module will add a method
# to remove unsubscribed subscriptions:
module RemoveUnsubscribedSubscriptionsExtension
def remove_unsubscribed
all_topics, _topics_count = topics(limit: nil, offset: nil)
all_topics.each do |topic|
active_ids = []
# This method will remove any subscriptions where are listed by the topic
# but not actually present in the DB (if this happens, it's a data consistency issue)
each_subscription_id(topic.name) do |id|