apollographql / router

A configurable, high-performance routing runtime for Apollo Federation 🚀
https://www.apollographql.com/docs/router/
Other
800 stars 268 forks source link

HTTP snapshot server #6032

Open pubmodmatt opened 1 week ago

pubmodmatt commented 1 week ago

This version of HTTP snapshots differs from #5921 by implementing a snapshot server. This can be used similarly to a wiremock server. It listens on a local port, and proxies requests to an upstream HTTP API. Responses are stored in a local file, and subsequent matching requests return the stored response.

This has several advantages:

An existing integration test and unit test are migrated to the snapshot server to demonstrate how it can be used.

The implementation is behind a snapshot feature flag.

Below is an example of running a standalone server and connecting to it from Sandbox:

preview_connectors:
  subgraphs:
    posts:
      sources:
        jsonPlaceholder:
          override_url: http://localhost:5280
> cargo run --bin snapshot --features="snapshot" -- --snapshot-path /tmp/snapshots.json --url https://jsonPlaceholder.typicode.com/ --offline --port 5280
...
2024-09-20T01:54:43.377471Z  INFO apollo_router::test_harness::http_snapshot: Running in offline mode ⛔️
2024-09-20T01:54:43.380146Z  INFO apollo_router::test_harness::http_snapshot: Loaded 11 snapshots
2024-09-20T01:54:43.502781Z  INFO apollo_router::test_harness::http_snapshot: Snapshot server listening on port 5280
2024-09-20T01:55:16.314233Z  INFO apollo_router::test_harness::http_snapshot: Found existing snapshot url=https://jsonPlaceholder.typicode.com/users method=GET
2024-09-20T01:55:16.323124Z  INFO apollo_router::test_harness::http_snapshot: Found existing snapshot url=https://jsonPlaceholder.typicode.com/users/1/posts method=GET
2024-09-20T01:55:16.323451Z  INFO apollo_router::test_harness::http_snapshot: Found existing snapshot url=https://jsonPlaceholder.typicode.com/users/2/posts method=GET
2024-09-20T01:55:16.323723Z  INFO apollo_router::test_harness::http_snapshot: Found existing snapshot url=https://jsonPlaceholder.typicode.com/users/8/posts method=GET
2024-09-20T01:55:16.323957Z  INFO apollo_router::test_harness::http_snapshot: Found existing snapshot url=https://jsonPlaceholder.typicode.com/users/4/posts method=GET
2024-09-20T01:55:16.324189Z  INFO apollo_router::test_harness::http_snapshot: Found existing snapshot url=https://jsonPlaceholder.typicode.com/users/3/posts method=GET
2024-09-20T01:55:16.324437Z  INFO apollo_router::test_harness::http_snapshot: Found existing snapshot url=https://jsonPlaceholder.typicode.com/users/5/posts method=GET
2024-09-20T01:55:16.324641Z  INFO apollo_router::test_harness::http_snapshot: Found existing snapshot url=https://jsonPlaceholder.typicode.com/users/10/posts method=GET
2024-09-20T01:55:16.324788Z  INFO apollo_router::test_harness::http_snapshot: Found existing snapshot url=https://jsonPlaceholder.typicode.com/users/6/posts method=GET
2024-09-20T01:55:16.324930Z  INFO apollo_router::test_harness::http_snapshot: Found existing snapshot url=https://jsonPlaceholder.typicode.com/users/7/posts method=GET
2024-09-20T01:55:16.325033Z  INFO apollo_router::test_harness::http_snapshot: Found existing snapshot url=https://jsonPlaceholder.typicode.com/users/9/posts method=GET

 


Checklist

Complete the checklist (and note appropriate exceptions) before the PR is marked ready-for-review.

Exceptions

Notes

[^1]: It may be appropriate to bring upcoming changes to the attention of other (impacted) groups. Please endeavour to do this before seeking PR approval. The mechanism for doing this will vary considerably, so use your judgement as to how and when to do this. [^2]: Configuration is an important part of many changes. Where applicable please try to document configuration examples. [^3]: Tick whichever testing boxes are applicable. If you are adding Manual Tests, please document the manual testing (extensively) in the Exceptions.