Skip to content

Instantly share code, notes, and snippets.

View rmosolgo's full-sized avatar
🧀

Robert Mosolgo rmosolgo

🧀
View GitHub Profile
@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
@rmosolgo
rmosolgo / nodes_auth.rb
Created March 15, 2024 20:35
Removing items from `nodes` after it has been paginated
# In this approach `nodes` are removed from a connection _after_ pagination has been applied.
#
# This approach has some downsides:
# - It may leak data about unauthorized nodes. hasNextPage and hasPrevPage will
# indicate that items were removed from the list; depending on sort and filter options,
# a client could learn about the attributes of the removed nodes.
# - pagination metadata may be wrong, since the list of nodes was modified after it was calculated.
# (some pagination info may be calculated _while_ the list of nodes is prepared.)
#
# The best alternative approach is to use `scope_items` instead, but I share this
require "bundler/inline"
gemfile do
gem "graphql", "1.12.17"
# gem "graphql", "1.12.18"
end
class MySchema < GraphQL::Schema
class Model < GraphQL::Schema::Object
field :name, String, null: false
@rmosolgo
rmosolgo / example.rb
Last active February 21, 2024 20:32
GraphQL-Ruby in-memory query cache with OperationStore and a custom analyzer
require "bundler/inline"
require "logger"
gemfile do
gem "graphql", "2.2.10"
gem "graphql-pro", "1.26.3"
gem "redis"
end
# In-memory cache: populate the cache after queries are run.
require "bundler/inline"
require "logger"
gemfile do
gem "graphql", "1.12.19"
gem "graphql-pro", "1.20.1"
gem "redis"
end
# Here's an example of caching parsed AST documents for persisted queries.
@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 / 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 / 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