Expose GraphQLRequest fields (#750)

This commit is contained in:
tyranron 2021-07-21 20:45:01 +03:00
parent d211f4a8ef
commit 8a90f867d4
No known key found for this signature in database
GPG key ID: 762E144FB230A4F0
4 changed files with 19 additions and 24 deletions

View file

@ -1,12 +0,0 @@
#[test]
fn operation_name_is_public() {
use juniper::{http::GraphQLRequest, DefaultScalarValue};
let request = GraphQLRequest::<DefaultScalarValue>::new(
"query".to_string(),
Some("name".to_string()),
None,
);
assert_eq!(request.operation_name(), Some("name"));
}

View file

@ -1,6 +1,4 @@
#[cfg(test)] #[cfg(test)]
mod api;
#[cfg(test)]
mod arc_fields; mod arc_fields;
#[cfg(test)] #[cfg(test)]
mod codegen; mod codegen;

View file

@ -1,6 +1,7 @@
# master # master
- Allow spreading interface fragments on unions and other interfaces ([#965](https://github.com/graphql-rust/juniper/pull/965), [#798](https://github.com/graphql-rust/juniper/issues/798)) - Allow spreading interface fragments on unions and other interfaces ([#965](https://github.com/graphql-rust/juniper/pull/965), [#798](https://github.com/graphql-rust/juniper/issues/798))
- Expose `GraphQLRequest` fields ([#750](https://github.com/graphql-rust/juniper/issues/750))
# [[0.15.7] 2021-07-08](https://github.com/graphql-rust/juniper/releases/tag/juniper-v0.15.7) # [[0.15.7] 2021-07-08](https://github.com/graphql-rust/juniper/releases/tag/juniper-v0.15.7)

View file

@ -29,23 +29,31 @@ pub struct GraphQLRequest<S = DefaultScalarValue>
where where
S: ScalarValue, S: ScalarValue,
{ {
query: String, /// GraphQL query representing this request.
pub query: String,
/// Optional name of the operation associated with this request.
#[serde(rename = "operationName")] #[serde(rename = "operationName")]
operation_name: Option<String>, pub operation_name: Option<String>,
/// Optional variables to execute the GraphQL operation with.
#[serde(bound(deserialize = "InputValue<S>: Deserialize<'de> + Serialize"))] #[serde(bound(deserialize = "InputValue<S>: Deserialize<'de> + Serialize"))]
variables: Option<InputValue<S>>, pub variables: Option<InputValue<S>>,
} }
impl<S> GraphQLRequest<S> impl<S> GraphQLRequest<S>
where where
S: ScalarValue, S: ScalarValue,
{ {
// TODO: Remove in 0.17 `juniper` version.
/// Returns the `operation_name` associated with this request. /// Returns the `operation_name` associated with this request.
#[deprecated(since = "0.16", note = "Use the direct field access instead.")]
pub fn operation_name(&self) -> Option<&str> { pub fn operation_name(&self) -> Option<&str> {
self.operation_name.as_deref() self.operation_name.as_deref()
} }
fn variables(&self) -> Variables<S> { /// Returns operation [`Variables`] defined withing this request.
pub fn variables(&self) -> Variables<S> {
self.variables self.variables
.as_ref() .as_ref()
.and_then(|iv| { .and_then(|iv| {
@ -64,7 +72,7 @@ where
operation_name: Option<String>, operation_name: Option<String>,
variables: Option<InputValue<S>>, variables: Option<InputValue<S>>,
) -> Self { ) -> Self {
GraphQLRequest { Self {
query, query,
operation_name, operation_name,
variables, variables,
@ -88,7 +96,7 @@ where
{ {
GraphQLResponse(crate::execute_sync( GraphQLResponse(crate::execute_sync(
&self.query, &self.query,
self.operation_name(), self.operation_name.as_deref(),
root_node, root_node,
&self.variables(), &self.variables(),
context, context,
@ -114,7 +122,7 @@ where
SubscriptionT::TypeInfo: Sync, SubscriptionT::TypeInfo: Sync,
S: ScalarValue + Send + Sync, S: ScalarValue + Send + Sync,
{ {
let op = self.operation_name(); let op = self.operation_name.as_deref();
let vars = &self.variables(); let vars = &self.variables();
let res = crate::execute(&self.query, op, root_node, vars, context).await; let res = crate::execute(&self.query, op, root_node, vars, context).await;
GraphQLResponse(res) GraphQLResponse(res)
@ -143,7 +151,7 @@ where
SubscriptionT::TypeInfo: Sync, SubscriptionT::TypeInfo: Sync,
S: ScalarValue + Send + Sync, S: ScalarValue + Send + Sync,
{ {
let op = req.operation_name(); let op = req.operation_name.as_deref();
let vars = req.variables(); let vars = req.variables();
crate::resolve_into_stream(&req.query, op, root_node, &vars, context).await crate::resolve_into_stream(&req.query, op, root_node, &vars, context).await
@ -316,8 +324,8 @@ where
/// The operation names of the request. /// The operation names of the request.
pub fn operation_names(&self) -> Vec<Option<&str>> { pub fn operation_names(&self) -> Vec<Option<&str>> {
match self { match self {
Self::Single(req) => vec![req.operation_name()], Self::Single(req) => vec![req.operation_name.as_deref()],
Self::Batch(reqs) => reqs.iter().map(|req| req.operation_name()).collect(), Self::Batch(reqs) => reqs.iter().map(|r| r.operation_name.as_deref()).collect(),
} }
} }
} }