Skip to content

Instantly share code, notes, and snippets.

View rmosolgo's full-sized avatar
🧀

Robert Mosolgo rmosolgo

🧀
View GitHub Profile
@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 / 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" },