Skip to content

Instantly share code, notes, and snippets.

View rmosolgo's full-sized avatar
🧀

Robert Mosolgo rmosolgo

🧀
View GitHub Profile
@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:)
@rmosolgo
rmosolgo / introspection_hiding.rb
Created July 13, 2023 15:29
Hiding introspection fields with GraphQL-Ruby
require "bundler/inline"
gemfile do
gem "graphql", "2.0.24"
end
class MySchema < GraphQL::Schema
module CustomIntrospection
module HideIntrospectionByContext
def visible?(ctx)
@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 / legacy_changesets.rb
Created December 5, 2022 15:05
Moving legacy fields to a GraphQL::Enterprise::Changeset
require "bundler/inline"
gemfile do
gem "graphql", path: "./"
gem "graphql-enterprise", source: "https://gems.graphql.pro"
end
class BaseField < GraphQL::Schema::Field
include GraphQL::Enterprise::Changeset::FieldIntegration
end
@rmosolgo
rmosolgo / union_spread.rb
Last active October 11, 2022 12:56
Spreading an inteface fragment inside a union
require "bundler/inline"
gemfile do
gem "graphql", "1.11.2"
end
class Schema < GraphQL::Schema
class Error < GraphQL::Schema::Object
field :message, String, null: false
end
@rmosolgo
rmosolgo / example.js
Last active May 24, 2022 15:45
urql subscription exchange from GraphQL-Ruby pusher subscriptions
import { Client, defaultExchanges, subscriptionExchange } from 'urql'
import Pusher from "pusher"
const pusherClient = new Pusher("your-app-key", { cluster: "us2" })
const forwardSubscriptionToPusher = (operation) => {
// urql will call `.subscribe` on the returned object:
// https://github.com/FormidableLabs/urql/blob/f89cfd06d9f14ae9cb3be10b21bd5cbd12ca275c/packages/core/src/exchanges/subscription.ts#L68-L73
// https://github.com/FormidableLabs/urql/blob/f89cfd06d9f14ae9cb3be10b21bd5cbd12ca275c/packages/core/src/exchanges/subscription.ts#L82-L97
return {
subscribe: ({next, error, complete}) => {
@rmosolgo
rmosolgo / inline_fragments.rb
Last active May 30, 2022 18:27
Inline Fragments
require "bundler/inline"
gemfile do
gem "graphql", "~>2.0"
end
# This visitor will inline any fragment definitions in the document.
# It assumes that the document is valid; it will :boom: if there are
# cycles in the fragment spreads.
@rmosolgo
rmosolgo / caching_test.rb
Created March 5, 2022 14:32
ObjectCache with __typename
require "bundler/inline"
gemfile do
gem "graphql", "1.13.10"
gem "graphql-pro", "1.21.4"
gem "graphql-enterprise", "1.1.3"
end
class CachingSchema < GraphQL::Schema