Skip to content

Instantly share code, notes, and snippets.

@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|
@rmosolgo
rmosolgo / custom_argument_connections.rb
Created July 23, 2024 15:54
GraphQL-Ruby Connection with Custom Arguments
# GraphQL-Ruby's connection system automatically adds first, last, after and before arguments.
# If you don't want all of those, you have to disable the system with `connection: false`
# in the field definition, then re-enable it manually.
#
# In this case, to create a field without the `last` argument, I created a custom subclass of `ConnectionExtension`
# which doesn't add any arguments. Then I add the specific arguments I want in the field definition.
# (I could also add those arguments in `def apply` using `field.argument ...`.)
require "bundler/inline"
@rmosolgo
rmosolgo / lookahead_example.rb
Created July 3, 2024 14:23
GraphQL-Ruby lookahead to plan sql queries by alias
require "bundler/inline"
gemfile do
gem "graphql", "2.3.7"
gem "sqlite3", "~>1.4"
gem "activerecord", require: "active_record"
end
ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: ":memory:")
ActiveRecord::Schema.define do
@rmosolgo
rmosolgo / lookahead_active_record_sql_example.rb
Created July 3, 2024 13:15
Only query for selected columns with ActiveRecord and GraphQL
require "bundler/inline"
gemfile do
gem "graphql", "2.3.7"
gem "sqlite3", "~>1.4"
gem "activerecord", require: "active_record"
end
ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: ":memory:")
ActiveRecord::Schema.define do
@rmosolgo
rmosolgo / empty_list.rb
Created May 27, 2024 13:48
Optional list investigation
require "bundler/inline"
gemfile do
gem "racc"
# gem "graphql", "2.0.16"
gem "graphql", "2.2.3"
end
class MySchema < GraphQL::Schema
class Query < GraphQL::Schema::Object
@rmosolgo
rmosolgo / can_can_graphql.rb
Created May 22, 2024 21:22
GraphQL-Pro CanCan integration example
require "bundler/inline"
gemfile do
gem "graphql", "2.3.4"
gem "graphql-pro", "1.27.5"
gem "cancancan", "3.5.0"
end
class Ability
include CanCan::Ability
@rmosolgo
rmosolgo / defer_example.rb
Last active May 16, 2024 21:01
GraphQL-Pro `@defer` on Fragments when context flag is present
require "bundler/inline"
gemfile do
gem "graphql", "~>2.2.0"
# or: gem "graphql", "~>1.13.0"
gem "graphql-batch", "0.6.0"
gem "graphql-pro", "~>1.27.0"
end
class Schema < GraphQL::Schema
# This module will add a method
# to clean up empty topics:
module RemoveEmptyTopicsStorageExtension
def remove_empty_topics
all_topics, _topics_count = topics(limit: nil, offset: nil)
topics_to_remove = all_topics.select { |t| t.subscriptions_count == 0 }
if topics_to_remove.any?
topic_names = topics_to_remove.map(&:name)
fingerprint_key = topic_names.map { |t| fingerprints_key(t) }
with_redis do |r|
@rmosolgo
rmosolgo / broadcast_debug.rb
Created April 18, 2024 18:44
A query analyzer for debugging the broadcastability of GraphQL-Ruby subscriptions
require "bundler/inline"
gemfile do
gem "graphql", path: "./" # "2.3.0"
end
class MySchema < GraphQL::Schema
class SubThing < GraphQL::Schema::Object
field :name, String, broadcastable: true
field :viewer_can_edit, Boolean # not broadcastable