Last active
August 24, 2019 14:52
-
-
Save markiz/315051ed474706506ef4e1df800797e8 to your computer and use it in GitHub Desktop.
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 | |
source 'https://rubygems.org' | |
gem 'graphql', '1.9.8' | |
gem 'graphql-batch', '0.4.1' | |
gem 'newrelic_rpm', '6.5.0.357' | |
end | |
Product = Struct.new(:name) | |
class BaseConnection < GraphQL::Types::Relay::BaseConnection | |
def self.edge_type(edge_type_class, edge_class: GraphQL::Relay::Edge, node_type: edge_type_class.node_type, nodes_field: true) | |
# Set this connection's graphql name | |
node_type_name = node_type.graphql_name | |
@node_type = node_type | |
@edge_type = edge_type_class | |
@edge_class = edge_class | |
field :edges, [edge_type_class], | |
null: false, | |
description: 'A list of edges.', | |
method: :edge_nodes, | |
edge_class: edge_class | |
if nodes_field | |
field :nodes, [node_type], | |
null: false, | |
description: 'A list of nodes.' | |
end | |
description("The connection type for #{node_type_name}.") | |
end | |
end | |
class BaseObject < GraphQL::Schema::Object | |
connection_type_class BaseConnection | |
end | |
class ProductType < BaseObject | |
field :name, String, null: false | |
end | |
class ProductLoader < GraphQL::Batch::Loader | |
def perform(ids) | |
fulfill(1, Product.new('Product 1')) | |
fulfill(2, Product.new('Product 2')) | |
end | |
end | |
class QueryType < BaseObject | |
field :products, ProductType.connection_type, null: false | |
def products | |
[ | |
ProductLoader.for.load(1), | |
ProductLoader.for.load(2) | |
] | |
end | |
end | |
class GraphSchema < GraphQL::Schema | |
query(QueryType) | |
use(GraphQL::Execution::Interpreter) | |
use(GraphQL::Batch) | |
use(GraphQL::Tracing::NewRelicTracing, set_transaction_name: true) | |
end | |
pp GraphSchema.execute('{ products { nodes { name } } }') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment