Struct apollo_router::Context

source ยท
pub struct Context { /* private fields */ }
Expand description

A map of arbitrary JSON values, for use by plugins.

Context makes use of DashMap under the hood which tries to handle concurrency by allowing concurrency across threads without requiring locking. This is great for usability but could lead to surprises when updates are highly contested.

Within the router, contention is likely to be highest within plugins which provide crate::services::SubgraphRequest or crate::services::SubgraphResponse processing. At such times, plugins should restrict themselves to the Context::get and Context::upsert functions to minimise the possibility of mis-sequenced updates.

Implementationsยง

sourceยง

impl Context

source

pub fn new() -> Self

Create a new context.

sourceยง

impl Context

source

pub fn extensions(&self) -> &ExtensionsMutex

Returns extensions of the context.

You can use Extensions to pass data between plugins that is not serializable. Such data is not accessible from Rhai or co-processoers.

It is CRITICAL to avoid holding on to the mutex guard for too long, particularly across async calls. Doing so may cause performance degradation or even deadlocks.

See related clippy lint for examples: https://rust-lang.github.io/rust-clippy/master/index.html#/await_holding_lock

source

pub fn contains_key<K>(&self, key: K) -> bool
where K: Into<String>,

Returns true if the context contains a value for the specified key.

source

pub fn get<K, V>(&self, key: K) -> Result<Option<V>, BoxError>
where K: Into<String>, V: for<'de> Deserialize<'de>,

Get a value from the context using the provided key.

Semantics:

  • If the operation fails, thatโ€™s because we canโ€™t deserialize the value.
  • If the operation succeeds, the value is an Option.
source

pub fn insert<K, V>(&self, key: K, value: V) -> Result<Option<V>, BoxError>
where K: Into<String>, V: for<'de> Deserialize<'de> + Serialize,

Insert a value int the context using the provided key and value.

Semantics:

  • If the operation fails, then the pair has not been inserted.
  • If the operation succeeds, the result is the old value as an Option.
source

pub fn insert_json_value<K>(&self, key: K, value: Value) -> Option<Value>
where K: Into<String>,

Insert a value in the context using the provided key and value.

Semantics: the result is the old value as an Option.

source

pub fn get_json_value<K>(&self, key: K) -> Option<Value>
where K: Into<String>,

Get a json value from the context using the provided key.

source

pub fn upsert<K, V>( &self, key: K, upsert: impl FnOnce(V) -> V ) -> Result<(), BoxError>
where K: Into<String>, V: for<'de> Deserialize<'de> + Serialize + Default,

Upsert a value in the context using the provided key and resolving function.

The resolving function must yield a value to be used in the context. It is provided with the current value to use in evaluating which value to yield.

Semantics:

  • If the operation fails, then the pair has not been inserted (or a current value updated).
  • If the operation succeeds, the pair have either updated an existing value or been inserted.
source

pub fn iter(&self) -> impl Iterator<Item = RefMulti<'_, String, Value>> + '_

Iterate over the entries.

source

pub fn iter_mut( &self ) -> impl Iterator<Item = RefMutMulti<'_, String, Value>> + '_

Iterate mutably over the entries.

Trait Implementationsยง

sourceยง

impl Clone for Context

sourceยง

fn clone(&self) -> Context

Returns a copy of the value. Read more
1.0.0 ยท sourceยง

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
sourceยง

impl Debug for Context

sourceยง

fn fmt(&self, __f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
sourceยง

impl Default for Context

sourceยง

fn default() -> Self

Returns the โ€œdefault valueโ€ for a type. Read more
sourceยง

impl<'de> Deserialize<'de> for Context

sourceยง

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
sourceยง

impl Serialize for Context

sourceยง

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more

Auto Trait Implementationsยง

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
sourceยง

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

sourceยง

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

sourceยง

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

sourceยง

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

sourceยง

impl<T> Serialize for T
where T: Serialize + ?Sized,

sourceยง

fn erased_serialize(&self, serializer: &mut dyn Serializer) -> Result<Ok, Error>

sourceยง

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

sourceยง

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> DynClone for T
where T: Clone,

sourceยง

fn __clone_box(&self, _: Private) -> *mut ()

sourceยง

impl<T> From<T> for T

sourceยง

fn from(t: T) -> T

Returns the argument unchanged.

sourceยง

impl<T> FromRef<T> for T
where T: Clone,

sourceยง

fn from_ref(input: &T) -> T

Converts to this type from a reference to the input type.
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<Unshared, Shared> IntoShared<Shared> for Unshared
where Shared: FromUnshared<Unshared>,

sourceยง

fn into_shared(self) -> Shared

Creates a shared type from an unshared type.
sourceยง

impl<T> Same for T

ยง

type Output = T

Should always be Self
sourceยง

impl<T> Serializable for T
where T: Serialize,

sourceยง

fn to_v8<'a>( &mut self, scope: &mut HandleScope<'a> ) -> Result<Local<'a, Value>, Error>

sourceยง

impl<T> ToOwned for T
where T: Clone,

ยง

type Owned = T

The resulting type after obtaining ownership.
sourceยง

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
sourceยง

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.
sourceยง

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

sourceยง

fn vzip(self) -> V

sourceยง

impl<T> Variant for T
where T: Any + Clone + SendSync,

sourceยง

fn as_any(&self) -> &(dyn Any + 'static)

Convert this Variant trait object to &dyn Any.
sourceยง

fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)

Convert this Variant trait object to &mut dyn Any.
sourceยง

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

Convert this Variant trait object to Box<dyn Any>.
sourceยง

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

Get the name of this type.
sourceยง

fn clone_object(&self) -> Box<dyn Variant>

Clone this Variant trait object.
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
sourceยง

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,