Struct async_graphql::Schema
source · pub struct Schema<Query, Mutation, Subscription>(_);
Expand description
GraphQL schema.
Cloning a schema is cheap, so it can be easily shared.
Implementations§
source§impl<Query, Mutation, Subscription> Schema<Query, Mutation, Subscription>where
Query: ObjectType + 'static,
Mutation: ObjectType + 'static,
Subscription: SubscriptionType + 'static,
impl<Query, Mutation, Subscription> Schema<Query, Mutation, Subscription>where
Query: ObjectType + 'static,
Mutation: ObjectType + 'static,
Subscription: SubscriptionType + 'static,
sourcepub fn build(
query: Query,
mutation: Mutation,
subscription: Subscription
) -> SchemaBuilder<Query, Mutation, Subscription>
pub fn build(
query: Query,
mutation: Mutation,
subscription: Subscription
) -> SchemaBuilder<Query, Mutation, Subscription>
Create a schema builder
The root object for the query and Mutation needs to be specified.
If there is no mutation, you can use EmptyMutation
.
If there is no subscription, you can use EmptySubscription
.
sourcepub fn build_with_ignore_name_conflicts<I, T>(
query: Query,
mutation: Mutation,
subscription: Subscription,
ignore_name_conflicts: I
) -> SchemaBuilder<Query, Mutation, Subscription>where
I: IntoIterator<Item = T>,
T: Into<String>,
pub fn build_with_ignore_name_conflicts<I, T>(
query: Query,
mutation: Mutation,
subscription: Subscription,
ignore_name_conflicts: I
) -> SchemaBuilder<Query, Mutation, Subscription>where
I: IntoIterator<Item = T>,
T: Into<String>,
Create a schema builder and specifies a list to ignore type conflict detection.
NOTE: It is not recommended to use it unless you know what it does.
sourcepub fn new(
query: Query,
mutation: Mutation,
subscription: Subscription
) -> Schema<Query, Mutation, Subscription>
pub fn new(
query: Query,
mutation: Mutation,
subscription: Subscription
) -> Schema<Query, Mutation, Subscription>
Create a schema
sourcepub fn sdl_with_options(&self, options: SDLExportOptions) -> String
pub fn sdl_with_options(&self, options: SDLExportOptions) -> String
Returns SDL(Schema Definition Language) of this schema with options.
sourcepub fn names(&self) -> Vec<String> ⓘ
pub fn names(&self) -> Vec<String> ⓘ
Get all names in this schema
Maybe you want to serialize a custom binary protocol. In order to minimize message size, a dictionary is usually used to compress type names, field names, directive names, and parameter names. This function gets all the names, so you can create this dictionary.
sourcepub async fn execute(&self, request: impl Into<Request>) -> Response
pub async fn execute(&self, request: impl Into<Request>) -> Response
Execute a GraphQL query.
Examples found in repository?
520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608
pub async fn execute_batch(&self, batch_request: BatchRequest) -> BatchResponse {
match batch_request {
BatchRequest::Single(request) => BatchResponse::Single(self.execute(request).await),
BatchRequest::Batch(requests) => BatchResponse::Batch(
futures_util::stream::iter(requests.into_iter())
.then(|request| self.execute(request))
.collect()
.await,
),
}
}
/// Execute a GraphQL subscription with session data.
pub fn execute_stream_with_session_data(
&self,
request: impl Into<Request>,
session_data: Arc<Data>,
) -> impl Stream<Item = Response> + Send + Unpin {
let schema = self.clone();
let request = request.into();
let extensions = self.create_extensions(session_data.clone());
let stream = futures_util::stream::StreamExt::boxed({
let extensions = extensions.clone();
let env = self.0.env.clone();
async_stream::stream! {
let (env, cache_control) = match prepare_request(
extensions, request, session_data, &env.registry,
schema.0.validation_mode, schema.0.recursive_depth, schema.0.complexity, schema.0.depth
).await {
Ok(res) => res,
Err(errors) => {
yield Response::from_errors(errors);
return;
}
};
if env.operation.node.ty != OperationType::Subscription {
yield schema.execute_once(env).await.cache_control(cache_control);
return;
}
let ctx = env.create_context(
&schema.0.env,
None,
&env.operation.node.selection_set,
);
let mut streams = Vec::new();
let collect_result = if schema.0.env.registry.introspection_mode
== IntrospectionMode::IntrospectionOnly
|| env.introspection_mode == IntrospectionMode::IntrospectionOnly
{
collect_subscription_streams(&ctx, &EmptySubscription, &mut streams)
} else {
collect_subscription_streams(&ctx, &schema.0.subscription, &mut streams)
};
if let Err(err) = collect_result {
yield Response::from_errors(vec![err]);
}
let mut stream = stream::select_all(streams);
while let Some(resp) = stream.next().await {
yield resp;
}
}
});
extensions.subscribe(stream)
}
/// Execute a GraphQL subscription.
pub fn execute_stream(
&self,
request: impl Into<Request>,
) -> impl Stream<Item = Response> + Send + Unpin {
self.execute_stream_with_session_data(request, Default::default())
}
}
#[async_trait::async_trait]
impl<Query, Mutation, Subscription> Executor for Schema<Query, Mutation, Subscription>
where
Query: ObjectType + 'static,
Mutation: ObjectType + 'static,
Subscription: SubscriptionType + 'static,
{
async fn execute(&self, request: Request) -> Response {
Schema::execute(self, request).await
}
sourcepub async fn execute_batch(&self, batch_request: BatchRequest) -> BatchResponse
pub async fn execute_batch(&self, batch_request: BatchRequest) -> BatchResponse
Execute a GraphQL batch query.
sourcepub fn execute_stream_with_session_data(
&self,
request: impl Into<Request>,
session_data: Arc<Data>
) -> impl Stream<Item = Response> + Send + Unpin
pub fn execute_stream_with_session_data(
&self,
request: impl Into<Request>,
session_data: Arc<Data>
) -> impl Stream<Item = Response> + Send + Unpin
Execute a GraphQL subscription with session data.
Examples found in repository?
591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617
pub fn execute_stream(
&self,
request: impl Into<Request>,
) -> impl Stream<Item = Response> + Send + Unpin {
self.execute_stream_with_session_data(request, Default::default())
}
}
#[async_trait::async_trait]
impl<Query, Mutation, Subscription> Executor for Schema<Query, Mutation, Subscription>
where
Query: ObjectType + 'static,
Mutation: ObjectType + 'static,
Subscription: SubscriptionType + 'static,
{
async fn execute(&self, request: Request) -> Response {
Schema::execute(self, request).await
}
fn execute_stream(
&self,
request: Request,
session_data: Option<Arc<Data>>,
) -> BoxStream<'static, Response> {
Schema::execute_stream_with_session_data(&self, request, session_data.unwrap_or_default())
.boxed()
}