This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require "bundler/inline" | |
| gemfile do | |
| gem "pundit", "2.1.0" | |
| gem "graphql", "1.12.5" | |
| source "https://gems.graphql.pro" do | |
| gem "graphql-pro", "1.17.6" | |
| end | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require "fiber" | |
| # Set up a pretend database | |
| DATA = { | |
| 1 => "Hannah Coulter", | |
| 2 => "Jayber Crow", | |
| 3 => "That Distant Land" | |
| } | |
| # This loader will fetch items in batches of IDs |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| inputs = [ | |
| # These inputs are "like" GraphQL queries because: | |
| # - Some keys are scalars, others are nested hashes | |
| # - Keys may be repeated | |
| # | |
| # Pretend they're like `{ field => complexity }` pairs. | |
| [{ a: 1, b: 1 }], | |
| [{ a: 1, b: 1 }, { b: 1 }], | |
| [{ a: 1, b: 1, c: { d: 1 } }, { c: { d: 1, e: 1 } }] | |
| ] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # A base mutation that adds an errors field to all subclasses, and before resolving, it checks to make sure that the caller selected `errors`. | |
| # | |
| # (You could use `GraphQL::Schema::Mutation` as a base class, too.) | |
| class Mutations::BaseMutation < GraphQL::Schema::RelayClassicMutation | |
| # Add the errors field to all mutations | |
| field :errors, [Types::MutationError], null: false | |
| # Inject `lookahead` to the resolve method | |
| extras [:lookahead] | |
| def resolve_with_support(lookahead:, **kwargs) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require "graphql" | |
| # A schema definition with field-level directives | |
| schema = GraphQL::Schema.from_definition <<-GRAPHQL | |
| type Query { | |
| totalScore: Int! @mock | |
| } | |
| type Mutation { | |
| incrementScore(by: Int = 1): Int! @mock(with: "SomeClassName") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # This is a full-blown offset-based pagination system modelled after GraphQL-Ruby's | |
| # built-in connections. It has a few different elements: | |
| # | |
| # - `::Page`, a plain ol' Ruby class for modeling _pages_ of things. | |
| # This class handles applying pagination arguments to lists (Arrays and AR::Relations) | |
| # and provides metadata about pagination. (Similar to `will_paginate`.) | |
| # - `Schema::BasePage` is a generic GraphQL-Ruby object type. It's never used directly, | |
| # but it can generate subclasses which wrap _specific_ object types in the schema. | |
| # - `Schema::BaseObject.page_type` is a convenience method for generating page types | |
| # from your object types. You could leave this out and make subclasses with plain ol' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # frozen_string_literal: true | |
| # QueryPrinter is a custom GraphQL Ruby | |
| # printer used to print sanitized queries. It inlines provided variables | |
| # within the query for facilitate logging and analysis of queries. | |
| # The printer assumes the query is valid. | |
| # | |
| # Since the GraphQL Ruby AST for a GraphQL query doesnt contain any reference | |
| # on the type of fields or arguments, we have to track the current object, field |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| source 'https://rubygems.org' | |
| gem "graphql", github: "rmosolgo/graphql-ruby", branch: "subscriptions" | |
| gem "sinatra" | |
| gem "thin" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ~/code/graphql $ ruby -v | |
| ruby 2.3.3p222 (2016-11-21 revision 56859) [x86_64-darwin14] | |
| ~/code/graphql $ ruby struct_bench.rb | |
| ======================================== | |
| Create and Access | |
| ======================================== | |
| Warming up -------------------------------------- | |
| Hash#[] 118.707k i/100ms | |
| Hash#fetch 113.228k i/100ms | |
| Struct 195.640k i/100ms |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # An untested idea to add performance tracking to graphql queries | |
| # | |
| # TODO: what's the best way of tracking time in the app? `Time.now.to_f`? | |
| # | |
| # When you define your schema, you could add this middleware: | |
| # | |
| # ``` | |
| # MySchema.middleware << PerformanceTrackingMiddleware.new | |
| # ``` | |
| # |