Generate `@deprecated` to SDL. #874

Expose `Connection::edges` #871
This commit is contained in:
Sunli 2022-03-22 11:09:31 +08:00
parent b2ac69b102
commit e4d04faaa0
3 changed files with 60 additions and 3 deletions

View File

@ -4,6 +4,11 @@ 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).
# [3.0.36] 2022-3-22
- Generate `@deprecated` to SDL. [#874](https://github.com/async-graphql/async-graphql/issues/874)
- Expose `Connection::edges`. [#871](https://github.com/async-graphql/async-graphql/issues/871)
# [3.0.35] 2022-3-14
- Make `HashMap` more generics for `InputOutput` and `OutputType`.

View File

@ -1,6 +1,6 @@
use std::fmt::Write;
use crate::registry::{MetaField, MetaInputValue, MetaType, Registry};
use crate::registry::{Deprecation, MetaField, MetaInputValue, MetaType, Registry};
impl Registry {
pub fn export_sdl(&self, federation: bool) -> String {
@ -84,6 +84,8 @@ impl Registry {
write!(sdl, " @oneof").ok();
}
write_deprecated(sdl, &field.deprecation);
if federation {
if field.external {
write!(sdl, " @external").ok();
@ -209,7 +211,9 @@ impl Registry {
write!(sdl, "enum {} ", name).ok();
writeln!(sdl, "{{").ok();
for value in enum_values.values() {
writeln!(sdl, "\t{}", value.name).ok();
write!(sdl, "\t{}", value.name).ok();
write_deprecated(sdl, &value.deprecation);
writeln!(sdl).ok();
}
writeln!(sdl, "}}").ok();
}
@ -282,3 +286,51 @@ fn export_input_value(input_value: &MetaInputValue) -> String {
format!("{}: {}", input_value.name, input_value.ty)
}
}
fn write_deprecated(sdl: &mut String, deprecation: &Deprecation) {
if let Deprecation::Deprecated { reason } = deprecation {
let _ = match reason {
Some(reason) => write!(sdl, " @deprecated(reason: \"{}\")", escape_string(reason)).ok(),
None => write!(sdl, " @deprecated").ok(),
};
}
}
fn escape_string(s: &str) -> String {
let mut res = String::new();
for c in s.chars() {
let ec = match c {
'\\' => Some("\\\\"),
'\x08' => Some("\\b"),
'\x0c' => Some("\\f"),
'\n' => Some("\\n"),
'\r' => Some("\\r"),
'\t' => Some("\\t"),
_ => None,
};
match ec {
Some(ec) => {
res.write_str(ec).ok();
}
None => {
res.write_char(c).ok();
}
}
}
res
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_escape_string() {
assert_eq!(
escape_string("1\\\x08d\x0c3\n4\r5\t6"),
"1\\\\\\bd\\f3\\n4\\r5\\t6"
);
}
}

View File

@ -18,7 +18,7 @@ use crate::{
/// Connection is the result of a query for `connection::query`.
pub struct Connection<C, T, EC = EmptyFields, EE = EmptyFields> {
/// All edges of the current page.
edges: Vec<Edge<C, T, EE>>,
pub edges: Vec<Edge<C, T, EE>>,
additional_fields: EC,
has_previous_page: bool,
has_next_page: bool,