Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
0600615
refactoring the examples folder + rhai version of forbid anonymous op…
lleadbet Oct 27, 2022
b003e36
fixing bad replace
lleadbet Oct 27, 2022
a7c279f
adding sample to list
lleadbet Oct 27, 2022
58f4b06
Merge branch 'dev' into examples/nesting-examples-for-better-rhai
lleadbet Oct 27, 2022
88dbd02
Update examples/README.md
lleadbet Oct 28, 2022
e19556c
pulling in new supergraph_sdl cargo.toml
lleadbet Nov 10, 2022
1da9778
improve the deduplication logic (#2014)
Oct 27, 2022
25cc762
fix(deps): update rust crate miette to 5.4.1
renovate[bot] Oct 28, 2022
5c72335
fix logic in argument processing for diy docker build script (#2032)
Oct 31, 2022
2539c8f
fixing router healthcheck formatting
dariuszkuc Oct 31, 2022
cd19ce7
allow router installer to install aarch64 binaries (#2033)
Nov 1, 2022
fa07b85
add dhat features to help resolving memory issues (#1829)
Nov 1, 2022
89826a8
reduce CI bill and improve build speed (#2037)
Nov 1, 2022
6da850e
fix(deps): update all non-major packages >= 1.0
renovate[bot] Nov 1, 2022
e2a6542
Exports `Endpoint` struct (#2009)
scottdouglas1989 Nov 2, 2022
bc144ac
validate default values for input object fields (#2003)
Nov 2, 2022
6eb1306
reload the configuration when receiving the SIGHUP signal (#2015)
Nov 2, 2022
f20665d
chore(deps): update openzipkin/zipkin docker tag to v2.23.19 (#1969)
renovate[bot] Nov 2, 2022
6783551
apply tower best practices to services when cloning inner services (#…
Nov 2, 2022
bf5184e
fix(deps): update dependency @apollo/server to v4.1.0 [security]
renovate[bot] Nov 2, 2022
fae1ef0
remove references to git submodules from DEVELOPMENT.md (#2045)
Nov 3, 2022
3e8856c
chore: Remove unused docs infrastructure (#1894)
abernix Nov 3, 2022
b51297a
(fix) #2036 Otel default endpoint incorrectly uses https. (#2048)
BrynCooke Nov 7, 2022
0e12236
Update apollo-parser to 0.3 (#2051)
SimonSapin Nov 7, 2022
f654f58
apply traffic shaping on subgraph directly (#2034)
Nov 7, 2022
6d4a293
docs: add a note about --dev mode config equivalent (#1940)
lennyburdette Nov 7, 2022
a39e51d
Update to apollo-smith 0.2 (#2062)
SimonSapin Nov 8, 2022
5f29eca
fix(deps): update all non-major packages >= 1.0
renovate[bot] Nov 8, 2022
69413d6
do not expose the trace_id in response header right now (#2061)
bnjjj Nov 8, 2022
1bde0a8
Prepare for v1.3.0 (#2065)
abernix Nov 9, 2022
00da8c7
Merge branch 'main' into examples/nesting-examples-for-better-rhai
lleadbet Nov 10, 2022
f50f118
Merge branch 'dev' into examples/nesting-examples-for-better-rhai
lleadbet Nov 10, 2022
02ed083
updating cargo lockfile
lleadbet Nov 10, 2022
3e75c13
actually updating cargo lockfile per xtask
lleadbet Nov 10, 2022
eb321c5
adding updated insta
lleadbet Nov 10, 2022
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
1,413 changes: 647 additions & 766 deletions Cargo.lock

Large diffs are not rendered by default.

33 changes: 17 additions & 16 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,23 @@ members = [
"apollo-router",
"apollo-router-benchmarks",
"apollo-router-scaffold",
"examples/add-timestamp-header",
"examples/async-auth",
"examples/cookies-to-headers",
"examples/embedded",
"examples/context",
"examples/forbid-anonymous-operations",
"examples/hello-world",
"examples/status-code-propagation",
"examples/op-name-to-header",
"examples/rhai-logging",
"examples/rhai-data-response-mutate",
"examples/rhai-error-response-mutate",
"examples/rhai-subgraph-request-log",
"examples/rhai-surrogate-cache-key",
"examples/supergraph_sdl",
"examples/jwt-auth",
"examples/add-timestamp-header/rhai",
"examples/async-auth/rust",
"examples/cookies-to-headers/rhai",
"examples/embedded/rust",
"examples/context/rust",
"examples/forbid-anonymous-operations/rust",
"examples/forbid-anonymous-operations/rhai",
"examples/hello-world/rust",
"examples/status-code-propagation/rust",
"examples/op-name-to-header/rhai",
"examples/logging/rhai",
"examples/data-response-mutate/rhai",
"examples/error-response-mutate/rhai",
"examples/subgraph-request-log/rhai",
"examples/surrogate-cache-key/rhai",
"examples/supergraph-sdl/rust",
"examples/jwt-auth/rust",
"fuzz",
"xtask",
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -679,10 +679,21 @@ expression: "&schema"
"format": "double"
},
{
"type": "string",
"enum": [
"always_on",
"always_off"
"oneOf": [
{
"description": "Always sample",
"type": "string",
"enum": [
"always_on"
]
},
{
"description": "Never sample",
"type": "string",
"enum": [
"always_off"
]
}
]
}
],
Expand Down Expand Up @@ -1958,10 +1969,21 @@ expression: "&schema"
"format": "double"
},
{
"type": "string",
"enum": [
"always_on",
"always_off"
"oneOf": [
{
"description": "Always sample",
"type": "string",
"enum": [
"always_on"
]
},
{
"description": "Never sample",
"type": "string",
"enum": [
"always_off"
]
}
]
}
],
Expand Down Expand Up @@ -2028,11 +2050,28 @@ expression: "&schema"
"properties": {
"compression": {
"description": "Enable compression for subgraphs (available compressions are deflate, br, gzip)",
"type": "string",
"enum": [
"gzip",
"deflate",
"br"
"oneOf": [
{
"description": "gzip",
"type": "string",
"enum": [
"gzip"
]
},
{
"description": "deflate",
"type": "string",
"enum": [
"deflate"
]
},
{
"description": "brotli",
"type": "string",
"enum": [
"br"
]
}
],
"nullable": true
},
Expand Down Expand Up @@ -2120,11 +2159,28 @@ expression: "&schema"
"properties": {
"compression": {
"description": "Enable compression for subgraphs (available compressions are deflate, br, gzip)",
"type": "string",
"enum": [
"gzip",
"deflate",
"br"
"oneOf": [
{
"description": "gzip",
"type": "string",
"enum": [
"gzip"
]
},
{
"description": "deflate",
"type": "string",
"enum": [
"deflate"
]
},
{
"description": "brotli",
"type": "string",
"enum": [
"br"
]
}
],
"nullable": true
},
Expand Down
49 changes: 29 additions & 20 deletions examples/README.md
Original file line number Diff line number Diff line change
@@ -1,41 +1,50 @@
# Examples

This directory contains many examples on how to use and extend the Apollo Router.
This directory contains many examples on how to use and extend the Apollo Router. Each folder may contain `rhai` and/or `rust`, which dictates the kind of plugin it is. For example, [the forbid anonymous operations example](./forbid-anonymous-operations/) has both a Rhai and Rust version available, whereas [the logging example](./logging) is only demonstrated in Rhai. For those without a subfolder, it is intended to be a config-only example, such as the [telemetry samples](./telemetry).

Make sure to look at the docs for:

## Yaml configuration ([documentation](https://www.apollographql.com/docs/router/configuration/overview))

Built in functionality in the Apollo Router.

* [Header manipulation](./header-manipulation)
* [Telemetry](./telemetry)
* [Forbid mutations](./forbid_mutations)
* [Unix sockets](./unix-sockets)
- [Header manipulation](./header-manipulation)
- [Telemetry](./telemetry)
- [Forbid mutations](./forbid-mutations)
- [Unix sockets](./unix-sockets)

## Customization ([documentation](https://www.apollographql.com/docs/router/customizations/overview))

Extending the functionality of the Apollo Router.

### Rhai

Scripting support.
* [Time request and add to header](./add-timestamp-header)
* [Cookies to headers](./cookies-to-headers)
* [Operation Name to headers](./op-name-to-header)
* [Logging](./rhai-logging)
* [Response data modification](./rhai-data-response-mutate)
* [Response errors modification](./rhai-error-response-mutate)
* [Subgraph request logging](./rhai-subgraph-request-log)
* [Surrogate cache key creation](./rhai-surrogate-cache-key)

- [Time request and add to header](./add-timestamp-header/rhai)
- [Cookies to headers](./cookies-to-headers/rhai)
- [Forbid anonymous operations](./forbid-anonymous-operations/rhai)
- [Operation Name to headers](./op-name-to-header/rhai)
- [Logging](./logging/rhai)
- [Response data modification](./data-response-mutate/rhai)
- [Response errors modification](./error-response-mutate/rhai)
- [Subgraph request logging](./subgraph-request-log/rhai)
- [Surrogate cache key creation](./surrogate-cache-key/rhai)

### Native Rust Plugins

Writing your own plugins in rust!
* [Hello world](./hello-world)
* [Context](./context)
* [Async auth](./async-auth)
* [Jwt auth](./jwt-auth)
* [Forbid mutations](./forbid_mutations)
* [Status code propagation](./status-code-propagation)

- [Hello world](./hello-world/rust)
- [Context](./context/rust)
- [Async auth](./async-auth/rust)
- [Jwt auth](./jwt-auth/rust)
- [Forbid mutations](./forbid-mutations/rust)
- [Status code propagation](./status-code-propagation/rust)
- [Forbid anonymous operations](./forbid-anonymous-operations/rust)

### Advanced usage

Customize the router for embedding in a different web server.
* [Embedded](./embedded)

- [Embedded](./embedded/rust)
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
anyhow = "1"
apollo-router = { path = "../../apollo-router" }
apollo-router = { path = "../../../apollo-router" }
futures = "0.3"
http = "0.2"
serde_json = "1"
Expand Down
9 changes: 9 additions & 0 deletions examples/add-timestamp-header/rhai/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Rhai script

Demonstrates header and context manipulation via Rhai script.

Usage:

```bash
cargo run -- -s ../../graphql/supergraph.graphql -c ./router.yaml
```
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

use anyhow::Result;

// `cargo run -- -s ../graphql/supergraph.graphql -c ./router.yaml`
// `cargo run -- -s ../../graphql/supergraph.graphql -c ./router.yaml`
fn main() -> Result<()> {
apollo_router::main()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
anyhow = "1"
apollo-router = { path = "../../apollo-router" }
apollo-router = { path = "../../../apollo-router" }
async-trait = "0.1"
futures = "0.3"
http = "0.2"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
# Async authentication

Demonstrates use of `checkpoint_async` to perform authentication that depends on an asynchronous call.

## Usage

```bash
cargo run -- -s ../graphql/supergraph.graphql -c ./router.yaml
cargo run -- -s ../../graphql/supergraph.graphql -c ./router.yaml
```

## Implementation

In this example a file is read to check for an ID, but it could be any async call, for example to an external
In this example a file is read to check for an ID, but it could be any async call, for example to an external
authentication server.

`checkpoint` and `checkpoint_async` allow you to halt request and return immediately. This is particularly useful for authentication.
Expand All @@ -19,7 +21,7 @@ authentication server.
service: router::BoxService,
) -> router::BoxService {
ServiceBuilder::new()
.checkpoint_async(...) // Authentication happens here
.checkpoint_async(...) // Authentication happens here
.buffer(20_000) // Required, see note below
.service(service)
.boxed()
Expand All @@ -28,4 +30,3 @@ authentication server.

Note that layers that require a service to be moved across an `await` point, e.g. `checkpoint_async` or `filter_async`
must be followed by a call to buffer, as they require the downstream service to be `Clone`.

Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ mod allow_client_id_from_file;

use anyhow::Result;

// `cargo run -- -s ../graphql/supergraph.graphql -c ./router.yaml`
// `cargo run -- -s ../../graphql/supergraph.graphql -c ./router.yaml`
fn main() -> Result<()> {
apollo_router::main()
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
anyhow = "1"
apollo-router = { path = "../../apollo-router" }
apollo-router = { path = "../../../apollo-router" }
async-trait = "0.1"
futures = "0.3"
http = "0.2"
Expand Down
17 changes: 12 additions & 5 deletions examples/context/README.md → examples/context/rust/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,18 @@
Demonstrate use of context to pass information between request lifecycle stages.

## Usage

```bash
cargo run -- -s ../graphql/supergraph.graphql -c ./router.yaml
cargo run -- -s ../../graphql/supergraph.graphql -c ./router.yaml
```

## Implementation

Context can be used to pass information between services. It's useful for storing things like authentication data or
collecting cache control information.

The request lifecycle looks like this:

```mermaid
sequenceDiagram
actor Client
Expand All @@ -22,41 +25,45 @@ sequenceDiagram

Client->>SupergraphService: request
SupergraphService->>QueryPlannerService: plan
QueryPlannerService-->>SupergraphService:
QueryPlannerService-->>SupergraphService:
SupergraphService->>ExecutionService: execute
ExecutionService-)SubgraphService(s): sub-request
SubgraphService(s)--)ExecutionService:
SubgraphService(s)--)ExecutionService:
ExecutionService-)SubgraphService(s): sub-request
SubgraphService(s)--)ExecutionService:
SubgraphService(s)--)ExecutionService:
ExecutionService-)SubgraphService(s): sub-request
SubgraphService(s)--)ExecutionService:
SubgraphService(s)--)ExecutionService:
ExecutionService-->>SupergraphService: response
SupergraphService-->>Client: response
```

For each request a single instance of `Context` is created and maintained .

In this example we:

1. Place some information in context at the incoming request of the router service. (world!)
2. Pick up and print it out at subgraph request. (Hello world!)
3. For each subgraph response merge some information into the `Context`. (response_count)
4. Pick up and print it out at router response. (response_count)

Context can store anything that is `Serde` compatible. For instance, all simple types or a custom type.

```rust
# Insert
context.insert("key1", 1)?;
```

Serialization and deserialization happen automatically. You may sometimes need to specify the type in the case
where the rust compiler cannot figure it out by itself.

```rust
# Get
let value : u32 = context.get("key1")?;
```

Upserts are guaranteed to happen serially. This is useful at the subgraph service where multiple requests are happening
in parallel.

```rust
# Upsert
context.upsert("present", |v| v + 1, || 0)?;
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use anyhow::Result;
// you can use the plugin by adding it to `router.yaml`
mod context_data;

// `cargo run -- -s ../graphql/supergraph.graphql -c ./router.yaml`
// `cargo run -- -s ../../graphql/supergraph.graphql -c ./router.yaml`
fn main() -> Result<()> {
apollo_router::main()
}
Loading