Struct async_graphql::extensions::NextResolve
source · pub struct NextResolve<'a> { /* private fields */ }
Expand description
The remainder of a extension chain for resolve.
Implementations§
source§impl<'a> NextResolve<'a>
impl<'a> NextResolve<'a>
sourcepub async fn run(
self,
ctx: &ExtensionContext<'_>,
info: ResolveInfo<'_>
) -> ServerResult<Option<Value>>
pub async fn run(
self,
ctx: &ExtensionContext<'_>,
info: ResolveInfo<'_>
) -> ServerResult<Option<Value>>
Call the Extension::resolve function of next extension.
Examples found in repository?
src/extensions/mod.rs (line 389)
383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512
async fn resolve(
&self,
ctx: &ExtensionContext<'_>,
info: ResolveInfo<'_>,
next: NextResolve<'_>,
) -> ServerResult<Option<Value>> {
next.run(ctx, info).await
}
}
/// Extension factory
///
/// Used to create an extension instance.
pub trait ExtensionFactory: Send + Sync + 'static {
/// Create an extended instance.
fn create(&self) -> Arc<dyn Extension>;
}
#[derive(Clone)]
#[doc(hidden)]
pub struct Extensions {
extensions: Vec<Arc<dyn Extension>>,
schema_env: SchemaEnv,
session_data: Arc<Data>,
query_data: Option<Arc<Data>>,
}
#[doc(hidden)]
impl Extensions {
pub(crate) fn new(
extensions: impl IntoIterator<Item = Arc<dyn Extension>>,
schema_env: SchemaEnv,
session_data: Arc<Data>,
) -> Self {
Extensions {
extensions: extensions.into_iter().collect(),
schema_env,
session_data,
query_data: None,
}
}
#[inline]
pub fn attach_query_data(&mut self, data: Arc<Data>) {
self.query_data = Some(data);
}
#[inline]
pub(crate) fn is_empty(&self) -> bool {
self.extensions.is_empty()
}
#[inline]
fn create_context(&self) -> ExtensionContext {
ExtensionContext {
schema_env: &self.schema_env,
session_data: &self.session_data,
query_data: self.query_data.as_deref(),
}
}
pub async fn request(&self, request_fut: RequestFut<'_>) -> Response {
let next = NextRequest {
chain: &self.extensions,
request_fut,
};
next.run(&self.create_context()).await
}
pub fn subscribe<'s>(&self, stream: BoxStream<'s, Response>) -> BoxStream<'s, Response> {
let next = NextSubscribe {
chain: &self.extensions,
};
next.run(&self.create_context(), stream)
}
pub async fn prepare_request(&self, request: Request) -> ServerResult<Request> {
let next = NextPrepareRequest {
chain: &self.extensions,
};
next.run(&self.create_context(), request).await
}
pub async fn parse_query(
&self,
query: &str,
variables: &Variables,
parse_query_fut: ParseFut<'_>,
) -> ServerResult<ExecutableDocument> {
let next = NextParseQuery {
chain: &self.extensions,
parse_query_fut,
};
next.run(&self.create_context(), query, variables).await
}
pub async fn validation(
&self,
validation_fut: ValidationFut<'_>,
) -> Result<ValidationResult, Vec<ServerError>> {
let next = NextValidation {
chain: &self.extensions,
validation_fut,
};
next.run(&self.create_context()).await
}
pub async fn execute(
&self,
operation_name: Option<&str>,
execute_fut: ExecuteFut<'_>,
) -> Response {
let next = NextExecute {
chain: &self.extensions,
execute_fut,
};
next.run(&self.create_context(), operation_name).await
}
pub async fn resolve(
&self,
info: ResolveInfo<'_>,
resolve_fut: ResolveFut<'_>,
) -> ServerResult<Option<Value>> {
let next = NextResolve {
chain: &self.extensions,
resolve_fut,
};
next.run(&self.create_context(), info).await
}
More examples
src/extensions/tracing.rs (line 149)
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
async fn resolve(
&self,
ctx: &ExtensionContext<'_>,
info: ResolveInfo<'_>,
next: NextResolve<'_>,
) -> ServerResult<Option<Value>> {
let span = if !info.is_for_introspection {
Some(span!(
target: "async_graphql::graphql",
Level::INFO,
"field",
path = %info.path_node,
parent_type = %info.parent_type,
return_type = %info.return_type,
))
} else {
None
};
let fut = next.run(ctx, info).inspect_err(|err| {
tracinglib::info!(
target: "async_graphql::graphql",
error = %err.message,
"error",
);
});
match span {
Some(span) => fut.instrument(span).await,
None => fut.await,
}
}
src/extensions/apollo_tracing.rs (line 118)
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
async fn resolve(
&self,
ctx: &ExtensionContext<'_>,
info: ResolveInfo<'_>,
next: NextResolve<'_>,
) -> ServerResult<Option<Value>> {
let path = info.path_node.to_string_vec();
let field_name = info.path_node.field_name().to_string();
let parent_type = info.parent_type.to_string();
let return_type = info.return_type.to_string();
let start_time = Utc::now();
let start_offset = (start_time - self.inner.lock().await.start_time)
.num_nanoseconds()
.unwrap();
let res = next.run(ctx, info).await;
let end_time = Utc::now();
self.inner.lock().await.resolves.push(ResolveState {
path,
field_name,
parent_type,
return_type,
start_time,
end_time,
start_offset,
});
res
}
src/extensions/opentelemetry.rs (line 187)
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
async fn resolve(
&self,
ctx: &ExtensionContext<'_>,
info: ResolveInfo<'_>,
next: NextResolve<'_>,
) -> ServerResult<Option<Value>> {
let span = if !info.is_for_introspection {
let attributes = vec![
KEY_PARENT_TYPE.string(info.parent_type.to_string()),
KEY_RETURN_TYPE.string(info.return_type.to_string()),
];
Some(
self.tracer
.span_builder(info.path_node.to_string())
.with_kind(SpanKind::Server)
.with_attributes(attributes)
.start(&*self.tracer),
)
} else {
None
};
let fut = next.run(ctx, info).inspect_err(|err| {
let current_cx = OpenTelemetryContext::current();
current_cx
.span()
.add_event("error".to_string(), vec![KEY_ERROR.string(err.to_string())]);
});
match span {
Some(span) => {
fut.with_context(OpenTelemetryContext::current_with_span(span))
.await
}
None => fut.await,
}
}