Fix the problem that the borrowing lifetime returned by the `Context::data` function is too small.

This commit is contained in:
Sunli 2021-02-23 23:05:08 +08:00
parent 49d588b686
commit 035f6d0a51
4 changed files with 12 additions and 8 deletions

View File

@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
## [2.5.7] - 2021-02-23
- Fix the problem that the borrowing lifetime returned by the `Context::data` function is too small.
## [2.5.6] - 2021-02-23
- When introspection is disabled, introspection related types are no longer registered.

View File

@ -415,7 +415,7 @@ impl<'a, T> ContextBase<'a, T> {
/// # Errors
///
/// Returns a `Error` if the specified type data does not exist.
pub fn data<D: Any + Send + Sync>(&self) -> Result<&D> {
pub fn data<D: Any + Send + Sync>(&self) -> Result<&'a D> {
self.data_opt::<D>().ok_or_else(|| {
Error::new(format!(
"Data `{}` does not exist.",
@ -429,13 +429,13 @@ impl<'a, T> ContextBase<'a, T> {
/// # Panics
///
/// It will panic if the specified data type does not exist.
pub fn data_unchecked<D: Any + Send + Sync>(&self) -> &D {
pub fn data_unchecked<D: Any + Send + Sync>(&self) -> &'a D {
self.data_opt::<D>()
.unwrap_or_else(|| panic!("Data `{}` does not exist.", std::any::type_name::<D>()))
}
/// Gets the global data defined in the `Context` or `Schema` or `None` if the specified type data does not exist.
pub fn data_opt<D: Any + Send + Sync>(&self) -> Option<&D> {
pub fn data_opt<D: Any + Send + Sync>(&self) -> Option<&'a D> {
self.query_env
.ctx_data
.0

View File

@ -45,7 +45,7 @@ impl<'a> ExtensionContext<'a> {
/// # Errors
///
/// Returns a `Error` if the specified type data does not exist.
pub fn data<D: Any + Send + Sync>(&self) -> Result<&D> {
pub fn data<D: Any + Send + Sync>(&self) -> Result<&'a D> {
self.data_opt::<D>().ok_or_else(|| {
Error::new(format!(
"Data `{}` does not exist.",
@ -59,13 +59,13 @@ impl<'a> ExtensionContext<'a> {
/// # Panics
///
/// It will panic if the specified data type does not exist.
pub fn data_unchecked<D: Any + Send + Sync>(&self) -> &D {
pub fn data_unchecked<D: Any + Send + Sync>(&self) -> &'a D {
self.data_opt::<D>()
.unwrap_or_else(|| panic!("Data `{}` does not exist.", std::any::type_name::<D>()))
}
/// Gets the global data defined in the `Context` or `Schema` or `None` if the specified type data does not exist.
pub fn data_opt<D: Any + Send + Sync>(&self) -> Option<&D> {
pub fn data_opt<D: Any + Send + Sync>(&self) -> Option<&'a D> {
self.query_data
.get(&TypeId::of::<D>())
.or_else(|| self.schema_data.get(&TypeId::of::<D>()))

View File

@ -70,8 +70,8 @@ pub struct __Directive<'a> {
}
/// A Directive provides a way to describe alternate runtime execution and type validation behavior in a GraphQL document.
//
// In some cases, you need to provide options to alter GraphQL's execution behavior in ways field arguments will not suffice, such as conditionally including or skipping a field. Directives provide this by describing additional information to the executor.
///
/// In some cases, you need to provide options to alter GraphQL's execution behavior in ways field arguments will not suffice, such as conditionally including or skipping a field. Directives provide this by describing additional information to the executor.
#[Object(internal, name = "__Directive")]
impl<'a> __Directive<'a> {
async fn name(&self) -> String {