Add `Request::disable_introspection` method. #456

This commit is contained in:
Sunli 2021-03-30 11:59:57 +08:00
parent 5d641a79bd
commit e899885aa8
6 changed files with 70 additions and 1 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.7.1]
- Add `Request::disable_introspection` method. [#456](https://github.com/async-graphql/async-graphql/issues/456)
## [2.7.0]
## Fixed

View File

@ -246,6 +246,7 @@ pub struct QueryEnvInner {
pub session_data: Arc<Data>,
pub ctx_data: Arc<Data>,
pub http_headers: spin::Mutex<HeaderMap<String>>,
pub disable_introspection: bool,
}
#[doc(hidden)]

View File

@ -39,6 +39,10 @@ pub struct Request {
/// The extensions config of the request.
#[serde(default)]
pub extensions: HashMap<String, Value>,
/// Disable introspection queries for this request.
#[serde(skip)]
pub disable_introspection: bool,
}
impl Request {
@ -51,6 +55,7 @@ impl Request {
uploads: Vec::default(),
data: Data::default(),
extensions: Default::default(),
disable_introspection: false,
}
}
@ -73,6 +78,12 @@ impl Request {
self
}
/// Disable introspection queries for this request.
pub fn disable_introspection(mut self) -> Self {
self.disable_introspection = true;
self
}
/// Set a variable to an upload value.
///
/// `var_path` is a dot-separated path to the item that begins with `variables`, for example

View File

@ -453,6 +453,7 @@ where
session_data,
ctx_data: query_data,
http_headers: Default::default(),
disable_introspection: request.disable_introspection,
};
Ok((QueryEnv::new(env), validation_result.cache_control))
}

View File

@ -91,7 +91,7 @@ impl<T: Type> Type for QueryRoot<T> {
#[async_trait::async_trait]
impl<T: ObjectType> ContainerType for QueryRoot<T> {
async fn resolve_field(&self, ctx: &Context<'_>) -> ServerResult<Option<Value>> {
if !ctx.schema_env.registry.disable_introspection {
if !ctx.schema_env.registry.disable_introspection && !ctx.query_env.disable_introspection {
if ctx.item.node.name.node == "__schema" {
let ctx_obj = ctx.with_selection_set(&ctx.item.node.selection_set);
return OutputType::resolve(

View File

@ -1220,3 +1220,55 @@ pub async fn test_introspection_subscription() {
//
// assert_eq!(res, res_json)
// }
#[tokio::test]
pub async fn test_disable_introspection() {
#[derive(SimpleObject)]
struct Query {
value: i32,
}
let schema = Schema::build(Query, EmptyMutation, EmptySubscription)
.disable_introspection()
.finish();
assert_eq!(
schema
.execute("{ __type(name: \"Query\") { name } }")
.await
.into_result()
.unwrap()
.data,
value!({ "__type": null })
);
assert_eq!(
schema
.execute(Request::new("{ __type(name: \"Query\") { name } }").disable_introspection())
.await
.into_result()
.unwrap()
.data,
value!({ "__type": null })
);
let schema = Schema::build(Query, EmptyMutation, EmptySubscription).finish();
assert_eq!(
schema
.execute("{ __type(name: \"Query\") { name } }")
.await
.into_result()
.unwrap()
.data,
value!({ "__type": { "name": "Query" } })
);
assert_eq!(
schema
.execute(Request::new("{ __type(name: \"Query\") { name } }").disable_introspection())
.await
.into_result()
.unwrap()
.data,
value!({ "__type": null })
);
}