pub struct TestHarness<'a> { /* private fields */ }
Expand description

Builder for the part of an Apollo Router that handles GraphQL requests, as a tower::Service.

This allows tests, benchmarks, etc to manipulate request and response objects in memory without going over the network on the supergraph side.

On the subgraph side, this test harness never makes network requests to subgraphs unless with_subgraph_network_requests is called.

Compared to running a full RouterHttpServer, this test harness is lacking:

  • Custom endpoints from plugins
  • The health check endpoint
  • CORS (FIXME: should this include CORS?)
  • HTTP compression

Example making a single request:

use apollo_router::services::supergraph;
use apollo_router::TestHarness;
use tower::util::ServiceExt;

let config = serde_json::json!({"supergraph": { "introspection": false }});
let request = supergraph::Request::fake_builder()
    // Request building here
    .build()
    .unwrap();
let response = TestHarness::builder()
    .configuration_json(config)?
    .build_router()
    .await?
    .oneshot(request.try_into().unwrap())
    .await?
    .next_response()
    .await
    .unwrap();

Implementationsยง

sourceยง

impl<'a> TestHarness<'a>

source

pub fn builder() -> Self

Creates a new builder.

source

pub fn log_level(self, log_level: &'a str) -> Self

Specifies the logging level. Note that this function may not be called more than once. log_level is in RUST_LOG format.

source

pub fn try_log_level(self, log_level: &'a str) -> Self

Specifies the logging level. Note that this function will silently fail if called more than once. log_level is in RUST_LOG format.

source

pub fn schema(self, schema: &'a str) -> Self

Specifies the (static) supergraph schema definition.

Panics if called more than once.

If this isnโ€™t called, a default โ€œcannedโ€ schema is used. It can be found in the Router repository at apollo-router/testing_schema.graphql. In that case, subgraph responses are overridden with some โ€œcannedโ€ data.

source

pub fn configuration(self, configuration: Arc<Configuration>) -> Self

Specifies the (static) router configuration.

source

pub fn configuration_json(self, configuration: Value) -> Result<Self, Error>

Specifies the (static) router configuration as a JSON value, such as from the serde_json::json! macro.

source

pub fn configuration_yaml( self, configuration: &'a str ) -> Result<Self, ConfigurationError>

Specifies the (static) router configuration as a YAML string, such as from the serde_json::json! macro.

source

pub fn extra_plugin<P: Plugin>(self, plugin: P) -> Self

Adds an extra, already instanciated plugin.

May be called multiple times. These extra plugins are added after plugins specified in configuration.

source

pub fn extra_unstable_plugin<P: PluginUnstable>(self, plugin: P) -> Self

Adds an extra, already instantiated unstable plugin.

May be called multiple times. These extra plugins are added after plugins specified in configuration.

source

pub fn router_hook( self, callback: impl Fn(BoxService) -> BoxService + Send + Sync + 'static ) -> Self

Adds a callback-based hook similar to Plugin::router_service

source

pub fn supergraph_hook( self, callback: impl Fn(BoxService) -> BoxService + Send + Sync + 'static ) -> Self

Adds a callback-based hook similar to Plugin::supergraph_service

source

pub fn execution_hook( self, callback: impl Fn(BoxService) -> BoxService + Send + Sync + 'static ) -> Self

Adds a callback-based hook similar to Plugin::execution_service

source

pub fn subgraph_hook( self, callback: impl Fn(&str, BoxService) -> BoxService + Send + Sync + 'static ) -> Self

Adds a callback-based hook similar to Plugin::subgraph_service

source

pub fn with_subgraph_network_requests(self) -> Self

Enables this test harness to make network requests to subgraphs.

If this is not called, all subgraph requests get an empty response by default (unless schema is also not called). This behavior can be changed by implementing Plugin::subgraph_service on a plugin given to extra_plugin.

source

pub async fn build(self) -> Result<BoxCloneService, BoxError>

๐Ÿ‘ŽDeprecated: use build_supergraph instead

Builds the supergraph service

source

pub async fn build_supergraph(self) -> Result<BoxCloneService, BoxError>

Builds the supergraph service

source

pub async fn build_router(self) -> Result<BoxCloneService, BoxError>

Builds the router service

Auto Trait Implementationsยง

ยง

impl<'a> !RefUnwindSafe for TestHarness<'a>

ยง

impl<'a> Send for TestHarness<'a>

ยง

impl<'a> Sync for TestHarness<'a>

ยง

impl<'a> Unpin for TestHarness<'a>

ยง

impl<'a> !UnwindSafe for TestHarness<'a>

Blanket Implementationsยง

sourceยง

impl<T> Any for T
where T: 'static + ?Sized,

sourceยง

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
ยง

impl<T> Any for T
where T: Any,

ยง

fn into_any(self: Box<T>) -> Box<dyn Any>

ยง

fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>

ยง

fn type_name(&self) -> &'static str

ยง

impl<T> AnySync for T
where T: Any + Send + Sync,

ยง

fn into_any_arc(self: Arc<T>) -> Arc<dyn Any + Sync + Send>

sourceยง

impl<T> Borrow<T> for T
where T: ?Sized,

sourceยง

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
sourceยง

impl<T> BorrowMut<T> for T
where T: ?Sized,

sourceยง

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
sourceยง

impl<T> From<T> for T

sourceยง

fn from(t: T) -> T

Returns the argument unchanged.

sourceยง

impl<T> FutureExt for T

sourceยง

fn with_context(self, otel_cx: Context) -> WithContext<Self>

Attaches the provided Context to this type, returning a WithContext wrapper. Read more
sourceยง

fn with_current_context(self) -> WithContext<Self>

Attaches the current Context to this type, returning a WithContext wrapper. Read more
sourceยง

impl<T> Instrument for T

sourceยง

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
sourceยง

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
sourceยง

impl<T> Instrument for T

sourceยง

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
sourceยง

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
sourceยง

impl<T, U> Into<U> for T
where U: From<T>,

sourceยง

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

sourceยง

impl<T> IntoRequest<T> for T

sourceยง

fn into_request(self) -> Request<T>

Wrap the input message T in a tonic::Request
sourceยง

impl<T> Same for T

ยง

type Output = T

Should always be Self
sourceยง

impl<T, U> TryFrom<U> for T
where U: Into<T>,

ยง

type Error = Infallible

The type returned in the event of a conversion error.
sourceยง

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
sourceยง

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

ยง

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
sourceยง

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
ยง

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

ยง

fn vzip(self) -> V

sourceยง

impl<T> WithSubscriber for T

sourceยง

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
sourceยง

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
sourceยง

impl<T> WithSubscriber for T

sourceยง

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
sourceยง

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more