Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Shift example into an examples directory #110

Merged
merged 1 commit into from
Dec 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@
source 'https://rubygems.org'
gemspec

gem 'rack'
gem 'rackup'
gem 'foreman'
gem 'pry'
gem 'pry-byebug'
gem 'warning'
3 changes: 0 additions & 3 deletions Procfile

This file was deleted.

31 changes: 3 additions & 28 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
26 changes: 0 additions & 26 deletions example/remote1.rb

This file was deleted.

26 changes: 0 additions & 26 deletions example/remote2.rb

This file was deleted.

8 changes: 8 additions & 0 deletions examples/merged_types/Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# frozen_string_literal: true

source 'https://rubygems.org'

gem 'rack'
gem 'rackup'
gem 'foreman'
gem 'graphql'
3 changes: 3 additions & 0 deletions examples/merged_types/Procfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
gateway: bundle exec ruby gateway.rb
remote1: bundle exec ruby remote1.rb
remote2: bundle exec ruby remote2.rb
33 changes: 33 additions & 0 deletions examples/merged_types/README.md
Original file line number Diff line number Diff line change
@@ -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.
9 changes: 4 additions & 5 deletions example/gateway.rb → examples/merged_types/gateway.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
File renamed without changes.
22 changes: 22 additions & 0 deletions examples/merged_types/remote1.rb
Original file line number Diff line number Diff line change
@@ -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)
22 changes: 22 additions & 0 deletions examples/merged_types/remote2.rb
Original file line number Diff line number Diff line change
@@ -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)
1 change: 1 addition & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down