From 1a764bfee02bd744362affcc3abcc82820595e10 Mon Sep 17 00:00:00 2001 From: Greg MacWilliam Date: Thu, 28 Dec 2023 20:42:11 -0500 Subject: [PATCH] shift example into an `examples` folder. --- Gemfile | 3 -- Procfile | 3 -- README.md | 31 ++--------------- example/remote1.rb | 26 --------------- example/remote2.rb | 26 --------------- examples/merged_types/Gemfile | 8 +++++ examples/merged_types/Procfile | 3 ++ examples/merged_types/README.md | 33 +++++++++++++++++++ {example => examples/merged_types}/gateway.rb | 9 +++-- .../merged_types}/graphiql.html | 0 examples/merged_types/remote1.rb | 22 +++++++++++++ examples/merged_types/remote2.rb | 22 +++++++++++++ test/test_helper.rb | 1 + 13 files changed, 96 insertions(+), 91 deletions(-) delete mode 100644 Procfile delete mode 100644 example/remote1.rb delete mode 100644 example/remote2.rb create mode 100644 examples/merged_types/Gemfile create mode 100644 examples/merged_types/Procfile create mode 100644 examples/merged_types/README.md rename {example => examples/merged_types}/gateway.rb (88%) rename {example => examples/merged_types}/graphiql.html (100%) create mode 100644 examples/merged_types/remote1.rb create mode 100644 examples/merged_types/remote2.rb diff --git a/Gemfile b/Gemfile index 30721f5c..da0e26be 100644 --- a/Gemfile +++ b/Gemfile @@ -3,9 +3,6 @@ source 'https://rubygems.org' gemspec -gem 'rack' -gem 'rackup' -gem 'foreman' gem 'pry' gem 'pry-byebug' gem 'warning' diff --git a/Procfile b/Procfile deleted file mode 100644 index fe30359f..00000000 --- a/Procfile +++ /dev/null @@ -1,3 +0,0 @@ -gateway: bundle exec ruby example/gateway.rb -remote1: bundle exec ruby example/remote1.rb -remote2: bundle exec ruby example/remote2.rb diff --git a/README.md b/README.md index c2f22ed9..7119ec97 100644 --- a/README.md +++ b/README.md @@ -431,36 +431,11 @@ The [Executor](./docs/executor.md) component builds atop the Ruby fiber-based im - [Stitched errors](./docs/mechanics.md#stitched-errors) - [Null results](./docs/mechanics.md#null-results) -## Example +## Examples -This repo includes a working example of several stitched schemas running across small Rack servers. Try running it: +This repo includes working examples of stitched schemas running across small Rack servers. Clone the repo, `cd` into each example and try running it following its README instructions. -```shell -bundle install -foreman start -``` - -Then visit the gateway service at `http://localhost:3000` and try this query: - -```graphql -query { - storefront(id: "1") { - id - products { - upc - name - price - manufacturer { - name - address - products { upc name } - } - } - } -} -``` - -The above query collects data from all locations, two of which are remote schemas and the third a local schema. The combined graph schema is also stitched in to provide introspection capabilities. +- [Merged types](./examples/merged_types) ## Tests diff --git a/example/remote1.rb b/example/remote1.rb deleted file mode 100644 index b6fe5f7d..00000000 --- a/example/remote1.rb +++ /dev/null @@ -1,26 +0,0 @@ -# frozen_string_literal: true - -require 'rackup' -require 'json' -require 'graphql' -require_relative '../test/schemas/example' - -class FirstRemoteApp - def call(env) - req = Rack::Request.new(env) - case req.path_info - when /graphql/ - params = JSON.parse(req.body.read) - result = Schemas::Example::Storefronts.execute( - query: params["query"], - variables: params["variables"], - operation_name: params["operationName"], - ) - [200, {"content-type" => "application/json"}, [JSON.generate(result)]] - else - [404, {"content-type" => "text/html"}, ["not found"]] - end - end -end - -Rackup::Handler.default.run(FirstRemoteApp.new, :Port => 3001) diff --git a/example/remote2.rb b/example/remote2.rb deleted file mode 100644 index b00e0a3a..00000000 --- a/example/remote2.rb +++ /dev/null @@ -1,26 +0,0 @@ -# frozen_string_literal: true - -require 'rackup' -require 'json' -require 'graphql' -require_relative '../test/schemas/example' - -class SecondRemoteApp - def call(env) - req = Rack::Request.new(env) - case req.path_info - when /graphql/ - params = JSON.parse(req.body.read) - result = Schemas::Example::Manufacturers.execute( - query: params["query"], - variables: params["variables"], - operation_name: params["operationName"], - ) - [200, {"content-type" => "application/json"}, [JSON.generate(result)]] - else - [404, {"content-type" => "text/html"}, ["not found"]] - end - end -end - -Rackup::Handler.default.run(SecondRemoteApp.new, :Port => 3002) diff --git a/examples/merged_types/Gemfile b/examples/merged_types/Gemfile new file mode 100644 index 00000000..38b66f44 --- /dev/null +++ b/examples/merged_types/Gemfile @@ -0,0 +1,8 @@ +# frozen_string_literal: true + +source 'https://rubygems.org' + +gem 'rack' +gem 'rackup' +gem 'foreman' +gem 'graphql' diff --git a/examples/merged_types/Procfile b/examples/merged_types/Procfile new file mode 100644 index 00000000..b089d4a9 --- /dev/null +++ b/examples/merged_types/Procfile @@ -0,0 +1,3 @@ +gateway: bundle exec ruby gateway.rb +remote1: bundle exec ruby remote1.rb +remote2: bundle exec ruby remote2.rb diff --git a/examples/merged_types/README.md b/examples/merged_types/README.md new file mode 100644 index 00000000..fb4a1713 --- /dev/null +++ b/examples/merged_types/README.md @@ -0,0 +1,33 @@ +# Merged types example + +This example demonstrates several stitched schemas running across small Rack servers with types merged across locations. The main "gateway" location stitches its local schema onto two remote endpoints. + +Try running it: + +```shell +cd examples/merged_types +bundle install +foreman start +``` + +Then visit the gateway service at [`http://localhost:3000`](http://localhost:3000) and try this query: + +```graphql +query { + storefront(id: "1") { + id + products { + upc + name + price + manufacturer { + name + address + products { upc name } + } + } + } +} +``` + +The above query collects data from all locations. You can also request introspections that resolve using the combined supergraph schema. diff --git a/example/gateway.rb b/examples/merged_types/gateway.rb similarity index 88% rename from example/gateway.rb rename to examples/merged_types/gateway.rb index 88845fb7..0f914904 100644 --- a/example/gateway.rb +++ b/examples/merged_types/gateway.rb @@ -2,10 +2,9 @@ require 'rackup' require 'json' -require 'byebug' require 'graphql' -require 'graphql/stitching' -require_relative '../test/schemas/example' +require_relative '../../lib/graphql/stitching' +require_relative '../../test/schemas/example' class StitchedApp def initialize @@ -19,11 +18,11 @@ def initialize }, storefronts: { schema: Schemas::Example::Storefronts, - executable: GraphQL::Stitching::HttpExecutable.new(url: "http://localhost:3001/graphql"), + executable: GraphQL::Stitching::HttpExecutable.new(url: "http://localhost:3001"), }, manufacturers: { schema: Schemas::Example::Manufacturers, - executable: GraphQL::Stitching::HttpExecutable.new(url: "http://localhost:3002/graphql"), + executable: GraphQL::Stitching::HttpExecutable.new(url: "http://localhost:3002"), } }) end diff --git a/example/graphiql.html b/examples/merged_types/graphiql.html similarity index 100% rename from example/graphiql.html rename to examples/merged_types/graphiql.html diff --git a/examples/merged_types/remote1.rb b/examples/merged_types/remote1.rb new file mode 100644 index 00000000..5a14f104 --- /dev/null +++ b/examples/merged_types/remote1.rb @@ -0,0 +1,22 @@ +# frozen_string_literal: true + +require 'rackup' +require 'json' +require 'graphql' +require_relative '../../test/schemas/example' + +class FirstRemoteApp + def call(env) + req = Rack::Request.new(env) + params = JSON.parse(req.body.read) + result = Schemas::Example::Storefronts.execute( + query: params["query"], + variables: params["variables"], + operation_name: params["operationName"], + ) + + [200, {"content-type" => "application/json"}, [JSON.generate(result)]] + end +end + +Rackup::Handler.default.run(FirstRemoteApp.new, :Port => 3001) diff --git a/examples/merged_types/remote2.rb b/examples/merged_types/remote2.rb new file mode 100644 index 00000000..41a2ed9d --- /dev/null +++ b/examples/merged_types/remote2.rb @@ -0,0 +1,22 @@ +# frozen_string_literal: true + +require 'rackup' +require 'json' +require 'graphql' +require_relative '../../test/schemas/example' + +class SecondRemoteApp + def call(env) + req = Rack::Request.new(env) + params = JSON.parse(req.body.read) + result = Schemas::Example::Manufacturers.execute( + query: params["query"], + variables: params["variables"], + operation_name: params["operationName"], + ) + + [200, {"content-type" => "application/json"}, [JSON.generate(result)]] + end +end + +Rackup::Handler.default.run(SecondRemoteApp.new, :Port => 3002) diff --git a/test/test_helper.rb b/test/test_helper.rb index b88d2f3d..1fc9ec3e 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -6,6 +6,7 @@ Gem.path.each do |path| # ignore warnings from auto-generated GraphQL lib code. Warning.ignore(/.*mismatched indentations.*/) + Warning.ignore(/.*lib\/graphql\/language\/nodes.rb.*/) end require 'bundler/setup'