Rewrite the object generation code

This commit is contained in:
sunli 2020-03-05 14:23:55 +08:00
parent cc96dba8d8
commit e9cc5969f2
26 changed files with 614 additions and 599 deletions

View File

@ -1,6 +1,6 @@
[package]
name = "async-graphql"
version = "0.6.0"
version = "0.7.0"
authors = ["sunli <scott_s829@163.com>"]
edition = "2018"
description = "The GraphQL server library implemented by rust"
@ -17,7 +17,7 @@ readme = "README.md"
default = ["chrono", "uuid"]
[dependencies]
async-graphql-derive = { path = "async-graphql-derive", version = "0.6.0" }
async-graphql-derive = { path = "async-graphql-derive", version = "0.7.0" }
graphql-parser = "0.2.3"
anyhow = "1.0.26"
thiserror = "1.0.11"

View File

@ -1,6 +1,6 @@
[package]
name = "async-graphql-derive"
version = "0.6.0"
version = "0.7.0"
authors = ["sunli <scott_s829@163.com>"]
edition = "2018"
description = "The GraphQL server library implemented by rust"

View File

@ -1,13 +1,12 @@
use crate::utils::parse_value;
use graphql_parser::query::Value;
use syn::{Attribute, AttributeArgs, Error, Meta, MetaList, NestedMeta, Result, Type};
use syn::{Attribute, AttributeArgs, Error, Meta, NestedMeta, Result};
#[derive(Debug)]
pub struct Object {
pub internal: bool,
pub name: Option<String>,
pub desc: Option<String>,
pub fields: Vec<Field>,
}
impl Object {
@ -15,7 +14,6 @@ impl Object {
let mut internal = false;
let mut name = None;
let mut desc = None;
let mut fields = Vec::new();
for arg in args {
match arg {
@ -43,9 +41,6 @@ impl Object {
}
}
}
NestedMeta::Meta(Meta::List(ls)) if ls.path.is_ident("field") => {
fields.push(Field::parse(&ls)?);
}
_ => {}
}
}
@ -54,107 +49,29 @@ impl Object {
internal,
name,
desc,
fields,
})
}
}
#[derive(Debug)]
pub struct Argument {
pub name: String,
pub name: Option<String>,
pub desc: Option<String>,
pub ty: Type,
pub default: Option<Value>,
}
#[derive(Debug)]
pub struct Field {
pub name: String,
pub resolver: Option<String>,
pub desc: Option<String>,
pub ty: Type,
pub is_owned: bool,
pub arguments: Vec<Argument>,
pub deprecation: Option<String>,
}
impl Field {
fn parse(ls: &MetaList) -> Result<Self> {
impl Argument {
pub fn parse(attrs: &[Attribute]) -> Result<Self> {
let mut name = None;
let mut resolver = None;
let mut desc = None;
let mut ty = None;
let mut is_owned = false;
let mut arguments = Vec::new();
let mut deprecation = None;
let mut default = None;
for meta in &ls.nested {
match meta {
NestedMeta::Meta(Meta::Path(p)) if p.is_ident("owned") => {
is_owned = true;
}
NestedMeta::Meta(Meta::NameValue(nv)) => {
if nv.path.is_ident("name") {
if let syn::Lit::Str(lit) = &nv.lit {
name = Some(lit.value());
} else {
return Err(Error::new_spanned(
&nv.lit,
"Attribute 'name' should be a string.",
));
}
} else if nv.path.is_ident("desc") {
if let syn::Lit::Str(lit) = &nv.lit {
desc = Some(lit.value());
} else {
return Err(Error::new_spanned(
&nv.lit,
"Attribute 'desc' should be a string.",
));
}
} else if nv.path.is_ident("resolver") {
if let syn::Lit::Str(lit) = &nv.lit {
resolver = Some(lit.value());
} else {
return Err(Error::new_spanned(
&nv.lit,
"Attribute 'resolver' should be a string.",
));
}
} else if nv.path.is_ident("type") {
if let syn::Lit::Str(lit) = &nv.lit {
if let Ok(ty2) = syn::parse_str::<syn::Type>(&lit.value()) {
ty = Some(ty2);
} else {
return Err(Error::new_spanned(&lit, "Expect type"));
}
desc = Some(lit.value());
} else {
return Err(Error::new_spanned(
&nv.lit,
"Attribute 'type' should be a string.",
));
}
} else if nv.path.is_ident("deprecation") {
if let syn::Lit::Str(lit) = &nv.lit {
deprecation = Some(lit.value());
} else {
return Err(Error::new_spanned(
&nv.lit,
"Attribute 'deprecation' should be a string.",
));
}
}
}
NestedMeta::Meta(Meta::List(ls)) => {
if ls.path.is_ident("arg") {
let mut name = None;
let mut desc = None;
let mut ty = None;
let mut default = None;
for meta in &ls.nested {
if let NestedMeta::Meta(Meta::NameValue(nv)) = meta {
for attr in attrs {
match attr.parse_meta() {
Ok(Meta::List(ls)) if ls.path.is_ident("arg") => {
for meta in &ls.nested {
match meta {
NestedMeta::Meta(Meta::NameValue(nv)) => {
if nv.path.is_ident("name") {
if let syn::Lit::Str(lit) = &nv.lit {
name = Some(lit.value());
@ -193,66 +110,100 @@ impl Field {
} else {
return Err(Error::new_spanned(
&nv.lit,
"Attribute 'default' should be a string.",
));
}
} else if nv.path.is_ident("type") {
if let syn::Lit::Str(lit) = &nv.lit {
if let Ok(ty2) = syn::parse_str::<syn::Type>(&lit.value()) {
ty = Some(ty2);
} else {
return Err(Error::new_spanned(&lit, "expect type"));
}
} else {
return Err(Error::new_spanned(
&nv.lit,
"Attribute 'type' should be a string.",
"Attribute 'deprecation' should be a string.",
));
}
}
}
_ => {}
}
if name.is_none() {
return Err(Error::new_spanned(ls, "missing name."));
}
if ty.is_none() {
return Err(Error::new_spanned(ls, "missing type."));
}
arguments.push(Argument {
name: name.unwrap(),
desc,
ty: ty.unwrap(),
default,
});
}
}
_ => {}
}
}
if name.is_none() {
return Err(Error::new_spanned(ls, "missing name."));
}
if ty.is_none() {
return Err(Error::new_spanned(ls, "missing type."));
}
Ok(Self {
name: name.unwrap(),
resolver,
name,
desc,
ty: ty.unwrap(),
is_owned,
arguments,
deprecation,
default,
})
}
}
#[derive(Debug)]
pub struct Field {
pub name: Option<String>,
pub desc: Option<String>,
pub deprecation: Option<String>,
}
impl Field {
pub fn parse(attrs: &[Attribute]) -> Result<Option<Self>> {
let mut is_field = false;
let mut name = None;
let mut desc = None;
let mut deprecation = None;
for attr in attrs {
match attr.parse_meta() {
Ok(Meta::Path(p)) if p.is_ident("field") => {
is_field = true;
}
Ok(Meta::List(ls)) if ls.path.is_ident("field") => {
is_field = true;
for meta in &ls.nested {
match meta {
NestedMeta::Meta(Meta::NameValue(nv)) => {
if nv.path.is_ident("name") {
if let syn::Lit::Str(lit) = &nv.lit {
name = Some(lit.value());
} else {
return Err(Error::new_spanned(
&nv.lit,
"Attribute 'name' should be a string.",
));
}
} else if nv.path.is_ident("desc") {
if let syn::Lit::Str(lit) = &nv.lit {
desc = Some(lit.value());
} else {
return Err(Error::new_spanned(
&nv.lit,
"Attribute 'desc' should be a string.",
));
}
} else if nv.path.is_ident("deprecation") {
if let syn::Lit::Str(lit) = &nv.lit {
deprecation = Some(lit.value());
} else {
return Err(Error::new_spanned(
&nv.lit,
"Attribute 'deprecation' should be a string.",
));
}
}
}
_ => {}
}
}
}
_ => {}
}
}
if is_field {
Ok(Some(Self {
name,
desc,
deprecation,
}))
} else {
Ok(None)
}
}
}
#[derive(Debug)]
pub struct Enum {
pub internal: bool,

View File

@ -8,7 +8,7 @@ mod utils;
use proc_macro::TokenStream;
use syn::parse_macro_input;
use syn::{AttributeArgs, DeriveInput};
use syn::{AttributeArgs, DeriveInput, ItemImpl};
#[proc_macro_attribute]
#[allow(non_snake_case)]
@ -17,8 +17,8 @@ pub fn Object(args: TokenStream, input: TokenStream) -> TokenStream {
Ok(object_args) => object_args,
Err(err) => return err.to_compile_error().into(),
};
let input = parse_macro_input!(input as DeriveInput);
match object::generate(&object_args, &input) {
let mut item_impl = parse_macro_input!(input as ItemImpl);
match object::generate(&object_args, &mut item_impl) {
Ok(expanded) => expanded,
Err(err) => err.to_compile_error().into(),
}

View File

@ -1,149 +1,259 @@
use crate::args;
use crate::utils::{build_value_repr, get_crate_name};
use inflector::Inflector;
use proc_macro::TokenStream;
use proc_macro2::Span;
use quote::quote;
use syn::{Data, DeriveInput, Error, Ident, Result};
use syn::{
Error, FnArg, GenericArgument, ImplItem, ItemImpl, Pat, PathArguments, Result, ReturnType,
Type, TypeReference,
};
pub fn generate(object_args: &args::Object, input: &DeriveInput) -> Result<TokenStream> {
enum OutputType<'a> {
Value(&'a Type),
ValueRef(&'a TypeReference),
Result(&'a Type, &'a Type),
}
pub fn generate(object_args: &args::Object, item_impl: &mut ItemImpl) -> Result<TokenStream> {
let crate_name = get_crate_name(object_args.internal);
let vis = &input.vis;
let ident = &input.ident;
let generics = &input.generics;
match &input.data {
Data::Struct(_) => {}
_ => return Err(Error::new_spanned(input, "It should be a struct.")),
let (self_ty, self_name) = match item_impl.self_ty.as_ref() {
Type::Path(path) => (
path,
path.path
.segments
.last()
.map(|s| s.ident.to_string())
.unwrap(),
),
_ => return Err(Error::new_spanned(&item_impl.self_ty, "Invalid type")),
};
let generics = &item_impl.generics;
let gql_typename = object_args
.name
.clone()
.unwrap_or_else(|| ident.to_string());
.unwrap_or_else(|| self_name.clone());
let desc = object_args
.desc
.as_ref()
.map(|s| quote! {Some(#s)})
.unwrap_or_else(|| quote! {None});
let trait_ident = Ident::new(&format!("{}Fields", ident.to_string()), Span::call_site());
let mut trait_fns = Vec::new();
let mut resolvers = Vec::new();
let mut schema_fields = Vec::new();
for field in &object_args.fields {
let ty = &field.ty;
let field_name = &field.name;
let desc = field
.desc
.as_ref()
.map(|s| quote! {Some(#s)})
.unwrap_or_else(|| quote! {None});
let deprecation = field
.deprecation
.as_ref()
.map(|s| quote! { Some(#s) })
.unwrap_or_else(|| quote! {None});
for item in &mut item_impl.items {
if let ImplItem::Method(method) = item {
if let Some(field) = args::Field::parse(&method.attrs)? {
let field_name = field
.name
.clone()
.unwrap_or_else(|| method.sig.ident.to_string());
let field_desc = field
.desc
.as_ref()
.map(|s| quote! {Some(#s)})
.unwrap_or_else(|| quote! {None});
let field_deprecation = field
.deprecation
.as_ref()
.map(|s| quote! {Some(#s)})
.unwrap_or_else(|| quote! {None});
let ty = match &method.sig.output {
ReturnType::Type(_, ty) => {
if let Type::Path(p) = ty.as_ref() {
if p.path.is_ident("Result") {
if let PathArguments::AngleBracketed(args) =
&p.path.segments[0].arguments
{
if args.args.len() == 0 {
return Err(Error::new_spanned(
&method.sig.output,
"Invalid type",
));
}
let mut res = None;
for arg in &args.args {
if let GenericArgument::Type(value_ty) = arg {
res = Some(OutputType::Result(ty, value_ty));
break;
}
}
if res.is_none() {
return Err(Error::new_spanned(
&method.sig.output,
"Invalid type",
));
}
res.unwrap()
} else {
return Err(Error::new_spanned(
&method.sig.output,
"Invalid type",
));
}
} else {
OutputType::Value(ty)
}
} else if let Type::Reference(ty) = ty.as_ref() {
OutputType::ValueRef(ty)
} else {
return Err(Error::new_spanned(&method.sig.output, "Invalid type"));
}
}
ReturnType::Default => {
return Err(Error::new_spanned(&method.sig.output, "Missing type"));
}
};
let mut decl_params = Vec::new();
let mut get_params = Vec::new();
let mut use_params = Vec::new();
let mut schema_args = Vec::new();
let mut arg_ctx = false;
let mut args = Vec::new();
for arg in &field.arguments {
let name = Ident::new(&arg.name, Span::call_site());
let ty = &arg.ty;
let name_str = name.to_string();
let snake_case_name = Ident::new(&name.to_string().to_snake_case(), ident.span());
let desc = arg
.desc
.as_ref()
.map(|s| quote! { Some(#s) })
.unwrap_or_else(|| quote! {None});
let schema_default = arg
.default
.as_ref()
.map(|v| {
let s = v.to_string();
quote! {Some(#s)}
})
.unwrap_or_else(|| quote! {None});
for (idx, arg) in method.sig.inputs.iter_mut().enumerate() {
if let FnArg::Receiver(receiver) = arg {
if idx != 0 {
return Err(Error::new_spanned(
receiver,
"The self receiver must be the first parameter.",
));
}
} else if let FnArg::Typed(pat) = arg {
if idx == 0 {
// 第一个参数必须是self
return Err(Error::new_spanned(
pat,
"The self receiver must be the first parameter.",
));
}
decl_params.push(quote! { #snake_case_name: #ty });
let default = match &arg.default {
Some(default) => {
let repr = build_value_repr(&crate_name, &default);
quote! {Some(|| #repr)}
match (&*pat.pat, &*pat.ty) {
(Pat::Ident(arg_ident), Type::Path(arg_ty)) => {
args.push((arg_ident, arg_ty, args::Argument::parse(&pat.attrs)?));
pat.attrs.clear();
}
(_, Type::Reference(TypeReference { elem, .. })) => {
if let Type::Path(path) = elem.as_ref() {
if idx != 1
|| path.path.segments.last().unwrap().ident.to_string()
!= "Context"
{
return Err(Error::new_spanned(
arg,
"The Context must be the second argument.",
));
}
arg_ctx = true;
}
}
_ => return Err(Error::new_spanned(arg, "Invalid argument type.")),
}
}
}
None => quote! { None },
};
get_params.push(quote! {
let #snake_case_name: #ty = ctx_field.param_value(#name_str, #default)?;
});
use_params.push(quote! { #snake_case_name });
let mut schema_args = Vec::new();
let mut use_params = Vec::new();
let mut get_params = Vec::new();
schema_args.push(quote! {
#crate_name::registry::InputValue {
name: #name_str,
description: #desc,
ty: <#ty as #crate_name::GQLType>::create_type_info(registry),
default_value: #schema_default,
for (
ident,
ty,
args::Argument {
name,
desc,
default,
},
) in args
{
let name = name.clone().unwrap_or_else(|| ident.ident.to_string());
let desc = desc
.as_ref()
.map(|s| quote! {Some(#s)})
.unwrap_or_else(|| quote! {None});
let schema_default = default
.as_ref()
.map(|v| {
let s = v.to_string();
quote! {Some(#s)}
})
.unwrap_or_else(|| quote! {None});
schema_args.push(quote! {
#crate_name::registry::InputValue {
name: #name,
description: #desc,
ty: <#ty as #crate_name::GQLType>::create_type_info(registry),
default_value: #schema_default,
}
});
use_params.push(quote! { #ident });
let default = match &default {
Some(default) => {
let repr = build_value_repr(&crate_name, &default);
quote! {Some(|| #repr) }
}
None => quote! { None },
};
get_params.push(quote! {
let #ident: #ty = ctx_field.param_value(#name, #default)?;
});
}
});
}
let resolver = Ident::new(
&field
.resolver
.as_ref()
.unwrap_or(&field.name.to_snake_case()),
Span::call_site(),
);
if field.is_owned {
trait_fns.push(quote! {
async fn #resolver(&self, ctx: &#crate_name::Context<'_>, #(#decl_params),*) -> #crate_name::Result<#ty>;
let schema_ty = match &ty {
OutputType::Result(_, value_ty) => value_ty,
OutputType::Value(value_ty) => value_ty,
OutputType::ValueRef(r) => r.elem.as_ref(),
};
schema_fields.push(quote! {
#crate_name::registry::Field {
name: #field_name,
description: #field_desc,
args: vec![#(#schema_args),*],
ty: <#schema_ty as #crate_name::GQLType>::create_type_info(registry),
deprecation: #field_deprecation,
}
});
} else {
trait_fns.push(quote! {
async fn #resolver<'a>(&'a self, ctx: &#crate_name::Context<'_>, #(#decl_params),*) -> #crate_name::Result<&'a #ty>;
let ctx_field = match arg_ctx {
true => quote! { &ctx_field, },
false => quote! {},
};
let field_ident = &method.sig.ident;
let resolve_obj = match &ty {
OutputType::Value(_) | OutputType::ValueRef(_) => quote! {
self.#field_ident(#ctx_field #(#use_params),*).await
},
OutputType::Result(_, _) => {
quote! {
self.#field_ident(#ctx_field #(#use_params),*).await.
map_err(|err| err.with_position(field.position))?
}
}
};
resolvers.push(quote! {
if field.name.as_str() == #field_name {
#(#get_params)*
let obj = #resolve_obj;
let ctx_obj = ctx_field.with_item(&field.selection_set);
let value = obj.resolve(&ctx_obj).await.
map_err(|err| err.with_position(field.position))?;
let name = field.alias.clone().unwrap_or_else(|| field.name.clone());
result.insert(name, value.into());
continue;
}
});
method.attrs.clear();
}
}
resolvers.push(quote! {
if field.name.as_str() == #field_name {
#(#get_params)*
let obj = #trait_ident::#resolver(self, &ctx_field, #(#use_params),*).await.
map_err(|err| err.with_position(field.position))?;
let ctx_obj = ctx_field.with_item(&field.selection_set);
let value = obj.resolve(&ctx_obj).await.
map_err(|err| err.with_position(field.position))?;
let name = field.alias.clone().unwrap_or_else(|| field.name.clone());
result.insert(name, value.into());
continue;
}
});
schema_fields.push(quote! {
#crate_name::registry::Field {
name: #field_name,
description: #desc,
args: vec![#(#schema_args),*],
ty: <#ty as #crate_name::GQLType>::create_type_info(registry),
deprecation: #deprecation,
}
});
}
let expanded = quote! {
#input
#item_impl
#[#crate_name::async_trait::async_trait]
#vis trait #trait_ident {
#(#trait_fns)*
}
impl#generics #crate_name::GQLType for #ident#generics {
impl #generics #crate_name::GQLType for #self_ty {
fn type_name() -> std::borrow::Cow<'static, str> {
std::borrow::Cow::Borrowed(#gql_typename)
}
@ -158,7 +268,7 @@ pub fn generate(object_args: &args::Object, input: &DeriveInput) -> Result<Token
}
#[#crate_name::async_trait::async_trait]
impl#generics #crate_name::GQLOutputValue for #ident#generics {
impl #generics #crate_name::GQLOutputValue for #self_ty {
async fn resolve(&self, ctx: &#crate_name::ContextSelectionSet<'_>) -> #crate_name::Result<#crate_name::serde_json::Value> {
use #crate_name::ErrorWithPosition;
@ -198,7 +308,7 @@ pub fn generate(object_args: &args::Object, input: &DeriveInput) -> Result<Token
}
}
impl#generics #crate_name::GQLObject for #ident#generics {}
impl#generics #crate_name::GQLObject for #self_ty {}
};
Ok(expanded.into())
}

View File

@ -30,7 +30,11 @@ async fn main() -> std::io::Result<()> {
.data(Schema::new(MyObj { value: 10 }, GQLEmptyMutation))
.service(web::resource("/").guard(guard::Post()).to(index))
.service(web::resource("/").guard(guard::Get()).to(gql_playgound))
.service(web::resource("/graphiql").guard(guard::Get()).to(gql_graphiql))
.service(
web::resource("/graphiql")
.guard(guard::Get())
.to(gql_graphiql),
)
})
.bind("127.0.0.1:8000")?
.run()

View File

@ -1,79 +0,0 @@
#[async_graphql::Enum]
enum MyEnum {
A,
B,
}
#[async_graphql::InputObject]
struct MyInputObj {
#[field(default = "\"hehe\"")]
a: i32,
b: i32,
}
#[async_graphql::Object(
field(name = "a", type = "i32"),
field(
owned,
name = "b",
type = "i32",
arg(name = "v", type = "i32", default = "123")
),
field(owned, name = "c", type = "Option<String>")
)]
struct MyObj {
value: i32,
}
#[async_trait::async_trait]
impl MyObjFields for MyObj {
async fn a<'a>(&'a self, ctx: &async_graphql::Context<'_>) -> async_graphql::Result<&'a i32> {
Ok(&self.value)
}
async fn b(&self, ctx: &async_graphql::Context<'_>, v: i32) -> async_graphql::Result<i32> {
Ok(v)
}
async fn c(&self, ctx: &async_graphql::Context<'_>) -> async_graphql::Result<Option<String>> {
Ok(Some(format!("**{}**", self.value)))
}
}
#[async_std::main]
async fn main() {
// let schema = async_graphql::Schema::<MyObj, async_graphql::GQLEmptyMutation>::new();
// let vars = async_graphql::Variables::parse_from_json(b"{\"myvar1\": 999}").unwrap();
// let res = schema
// .query(
// MyObj { value: 100 },
// async_graphql::GQLEmptyMutation,
// r#"
// {
// __schema {
// directives @skip(if: true) {
// name
// description
// locations
// args {
// name description type {
// name
// description
// kind
// ofType {
// name
// description
// }
// } defaultValue
// }
// }
// }
// }
// "#,
// )
// .variables(&vars)
// .execute()
// .await
// .unwrap();
// serde_json::to_writer_pretty(std::io::stdout(), &res).unwrap();
}

View File

@ -11,31 +11,33 @@ pub struct MyInputObj {
b: i32,
}
#[async_graphql::Object(
field(name = "a", type = "i32"),
field(
owned,
name = "b",
type = "i32",
arg(name = "v", type = "i32", default = "123")
),
field(owned, name = "c", type = "Option<String>")
)]
pub struct MyObj {
pub value: i32,
}
#[async_trait::async_trait]
impl MyObjFields for MyObj {
async fn a<'a>(&'a self, ctx: &async_graphql::Context<'_>) -> async_graphql::Result<&'a i32> {
Ok(&self.value)
#[async_graphql::Object(
field(name = "a", type = "i32"),
field(
owned,
name = "b",
type = "i32",
arg(name = "v", type = "i32", default = "123")
),
field(owned, name = "c", type = "Option<String>")
)]
impl MyObj {
#[field]
async fn a(&self) -> &i32 {
&self.value
}
async fn b(&self, ctx: &async_graphql::Context<'_>, v: i32) -> async_graphql::Result<i32> {
Ok(v)
#[field]
async fn b(&self, #[arg(default = "123")] v: i32) -> i32 {
v
}
async fn c(&self, ctx: &async_graphql::Context<'_>) -> async_graphql::Result<Option<String>> {
Ok(Some(format!("**{}**", self.value)))
#[field]
async fn c(&self) -> Option<String> {
Some(format!("**{}**", self.value))
}
}
}

View File

@ -30,7 +30,7 @@ pub trait GQLObject: GQLOutputValue {}
#[doc(hidden)]
pub trait GQLInputObject: GQLInputValue {}
pub trait Scalar: Sized + Send {
pub trait GQLScalar: Sized + Send {
fn type_name() -> &'static str;
fn description() -> Option<&'static str> {
None
@ -39,7 +39,7 @@ pub trait Scalar: Sized + Send {
fn to_json(&self) -> Result<serde_json::Value>;
}
impl<T: Scalar> GQLType for T {
impl<T: GQLScalar> GQLType for T {
fn type_name() -> Cow<'static, str> {
Cow::Borrowed(T::type_name())
}
@ -52,14 +52,14 @@ impl<T: Scalar> GQLType for T {
}
}
impl<T: Scalar> GQLInputValue for T {
impl<T: GQLScalar> GQLInputValue for T {
fn parse(value: &Value) -> Option<Self> {
T::parse(value)
}
}
#[async_trait::async_trait]
impl<T: Scalar + Sync> GQLOutputValue for T {
impl<T: GQLScalar + Sync> GQLOutputValue for T {
async fn resolve(&self, _: &ContextSelectionSet<'_>) -> Result<serde_json::Value> {
T::to_json(self)
}

View File

@ -73,8 +73,7 @@ pub use serde_json;
pub mod http;
pub use async_graphql_derive::{Enum, InputObject, Object};
pub use base::Scalar;
pub use base::{GQLInputObject, GQLInputValue, GQLObject, GQLOutputValue, GQLType};
pub use base::{GQLInputObject, GQLInputValue, GQLObject, GQLOutputValue, GQLScalar, GQLType};
pub use context::{Context, ContextBase, Variables};
pub use error::{ErrorWithPosition, PositionError, QueryError, QueryParseError};
pub use graphql_parser::query::Value;

View File

@ -1,5 +1,5 @@
use crate::model::__InputValue;
use crate::{registry, Context, Result};
use crate::registry;
use async_graphql_derive::{Enum, Object};
#[Enum(
@ -66,44 +66,42 @@ pub enum __DirectiveLocation {
INPUT_FIELD_DEFINITION,
}
#[Object(
internal,
desc = r#"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."#,
field(name = "name", type = "String", owned),
field(name = "description", type = "Option<String>", owned),
field(name = "locations", type = "Vec<__DirectiveLocation>"),
field(name = "args", type = "Vec<__InputValue>", owned)
)]
pub struct __Directive<'a> {
pub registry: &'a registry::Registry,
pub directive: &'a registry::Directive,
}
#[async_trait::async_trait]
impl<'a> __DirectiveFields for __Directive<'a> {
async fn name(&self, _: &Context<'_>) -> Result<String> {
Ok(self.directive.name.to_string())
#[Object(
internal,
desc = r#"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."#
)]
impl<'a> __Directive<'a> {
#[field]
async fn name(&self) -> String {
self.directive.name.to_string()
}
async fn description(&self, _: &Context<'_>) -> Result<Option<String>> {
Ok(self.directive.description.map(|s| s.to_string()))
#[field]
async fn description(&self) -> Option<String> {
self.directive.description.map(|s| s.to_string())
}
async fn locations<'b>(&'b self, _: &Context<'_>) -> Result<&'b Vec<__DirectiveLocation>> {
Ok(&self.directive.locations)
#[field]
async fn locations(&self) -> &Vec<__DirectiveLocation> {
&self.directive.locations
}
async fn args<'b>(&'b self, _: &Context<'_>) -> Result<Vec<__InputValue<'b>>> {
Ok(self
.directive
#[field]
async fn args(&self) -> Vec<__InputValue<'a>> {
self.directive
.args
.iter()
.map(|input_value| __InputValue {
registry: self.registry,
input_value,
})
.collect())
.collect()
}
}

View File

@ -1,34 +1,33 @@
use crate::{registry, Context, Result};
use crate::{registry, Context};
use async_graphql_derive::Object;
#[Object(
internal,
desc = "One possible value for a given Enum. Enum values are unique values, not a placeholder for a string or numeric value. However an Enum value is returned in a JSON response as a string.",
field(name = "name", type = "String", owned),
field(name = "description", type = "Option<String>", owned),
field(name = "isDeprecated", type = "bool", owned),
field(name = "deprecationReason", type = "Option<String>", owned)
)]
pub struct __EnumValue<'a> {
pub registry: &'a registry::Registry,
pub value: &'a registry::EnumValue,
}
#[async_trait::async_trait]
impl<'a> __EnumValueFields for __EnumValue<'a> {
async fn name(&self, _: &Context<'_>) -> Result<String> {
Ok(self.value.name.to_string())
#[Object(
internal,
desc = "One possible value for a given Enum. Enum values are unique values, not a placeholder for a string or numeric value. However an Enum value is returned in a JSON response as a string."
)]
impl<'a> __EnumValue<'a> {
#[field]
async fn name(&self, _: &Context<'_>) -> String {
self.value.name.to_string()
}
async fn description(&self, _: &Context<'_>) -> Result<Option<String>> {
Ok(self.value.description.map(|s| s.to_string()))
#[field]
async fn description(&self, _: &Context<'_>) -> Option<String> {
self.value.description.map(|s| s.to_string())
}
async fn is_deprecated(&self, _: &Context<'_>) -> Result<bool> {
Ok(self.value.deprecation.is_some())
#[field(name = "isDeprecated")]
async fn is_deprecated(&self, _: &Context<'_>) -> bool {
self.value.deprecation.is_some()
}
async fn deprecation_reason(&self, _: &Context<'_>) -> Result<Option<String>> {
Ok(self.value.deprecation.map(|s| s.to_string()))
#[field(name = "deprecationReason")]
async fn deprecation_reason(&self, _: &Context<'_>) -> Option<String> {
self.value.deprecation.map(|s| s.to_string())
}
}

View File

@ -1,54 +1,51 @@
use crate::model::{__InputValue, __Type};
use crate::{registry, Context, Result};
use crate::registry;
use async_graphql_derive::Object;
#[Object(
internal,
desc = "Object and Interface types are described by a list of Fields, each of which has a name, potentially a list of arguments, and a return type.",
field(name = "name", type = "String", owned),
field(name = "description", type = "Option<String>", owned),
field(name = "args", type = "Vec<__InputValue>", owned),
field(name = "type", resolver = "ty", type = "__Type", owned),
field(name = "isDeprecated", type = "bool", owned),
field(name = "deprecationReason", type = "Option<String>", owned)
)]
pub struct __Field<'a> {
pub registry: &'a registry::Registry,
pub field: &'a registry::Field,
}
#[async_trait::async_trait]
#[allow()]
impl<'a> __FieldFields for __Field<'a> {
async fn name(&self, _: &Context<'_>) -> Result<String> {
Ok(self.field.name.to_string())
#[Object(
internal,
desc = "Object and Interface types are described by a list of Fields, each of which has a name, potentially a list of arguments, and a return type."
)]
impl<'a> __Field<'a> {
#[field]
async fn name(&self) -> String {
self.field.name.to_string()
}
async fn description(&self, _: &Context<'_>) -> Result<Option<String>> {
Ok(self.field.description.map(|s| s.to_string()))
#[field]
async fn description(&self) -> Option<String> {
self.field.description.map(|s| s.to_string())
}
async fn args<'b>(&'b self, _: &Context<'_>) -> Result<Vec<__InputValue<'b>>> {
Ok(self
.field
#[field]
async fn args(&self) -> Vec<__InputValue<'a>> {
self.field
.args
.iter()
.map(|input_value| __InputValue {
registry: self.registry,
input_value,
})
.collect())
.collect()
}
async fn ty<'b>(&'b self, _: &Context<'_>) -> Result<__Type<'b>> {
Ok(__Type::new(self.registry, &self.field.ty))
#[field(name = "type")]
async fn ty(&self) -> __Type<'a> {
__Type::new(self.registry, &self.field.ty)
}
async fn is_deprecated(&self, _: &Context<'_>) -> Result<bool> {
Ok(self.field.deprecation.is_some())
#[field(name = "isDeprecated")]
async fn is_deprecated(&self) -> bool {
self.field.deprecation.is_some()
}
async fn deprecation_reason(&self, _: &Context<'_>) -> Result<Option<String>> {
Ok(self.field.deprecation.map(|s| s.to_string()))
#[field(name = "deprecationReason")]
async fn deprecation_reason(&self) -> Option<String> {
self.field.deprecation.map(|s| s.to_string())
}
}

View File

@ -1,35 +1,34 @@
use crate::model::__Type;
use crate::{registry, Context, Result};
use crate::registry;
use async_graphql_derive::Object;
#[Object(
internal,
desc = "Arguments provided to Fields or Directives and the input fields of an InputObject are represented as Input Values which describe their type and optionally a default value.",
field(name = "name", type = "String", owned),
field(name = "description", type = "Option<String>", owned),
field(name = "type", resolver = "ty", type = "__Type", owned),
field(name = "defaultValue", type = "Option<String>", owned)
)]
pub struct __InputValue<'a> {
pub registry: &'a registry::Registry,
pub input_value: &'a registry::InputValue,
}
#[async_trait::async_trait]
impl<'a> __InputValueFields for __InputValue<'a> {
async fn name(&self, _: &Context<'_>) -> Result<String> {
Ok(self.input_value.name.to_string())
#[Object(
internal,
desc = "Arguments provided to Fields or Directives and the input fields of an InputObject are represented as Input Values which describe their type and optionally a default value."
)]
impl<'a> __InputValue<'a> {
#[field]
async fn name(&self) -> String {
self.input_value.name.to_string()
}
async fn description(&self, _: &Context<'_>) -> Result<Option<String>> {
Ok(self.input_value.description.map(|s| s.to_string()))
#[field]
async fn description(&self) -> Option<String> {
self.input_value.description.map(|s| s.to_string())
}
async fn ty<'b>(&'b self, _: &Context<'_>) -> Result<__Type<'b>> {
Ok(__Type::new(self.registry, &self.input_value.ty))
#[field(name = "type")]
async fn ty(&self) -> __Type<'a> {
__Type::new(self.registry, &self.input_value.ty)
}
async fn default_value(&self, _: &Context<'_>) -> Result<Option<String>> {
Ok(self.input_value.default_value.map(|s| s.to_string()))
#[field(name = "defaultValue")]
async fn default_value(&self) -> Option<String> {
self.input_value.default_value.map(|s| s.to_string())
}
}

View File

@ -1,86 +1,63 @@
use crate::model::{__Directive, __Type};
use crate::registry;
use crate::{Context, Result};
use async_graphql_derive::Object;
#[Object(
internal,
desc = "A GraphQL Schema defines the capabilities of a GraphQL server. It exposes all available types and directives on the server, as well as the entry points for query, mutation, and subscription operations.",
field(
name = "types",
desc = "A list of all types supported by this server.",
type = "Vec<__Type>",
owned
),
field(
name = "queryType",
desc = "The type that query operations will be rooted at.",
type = "__Type",
owned
),
field(
name = "mutationType",
desc = "If this server supports mutation, the type that mutation operations will be rooted at.",
type = "Option<__Type>",
owned
),
field(
name = "subscriptionType",
desc = "If this server support subscription, the type that subscription operations will be rooted at.",
type = "Option<__Type>",
owned
),
field(
name = "directives",
desc = "A list of all directives supported by this server.",
type = "Vec<__Directive>",
owned
)
)]
pub struct __Schema<'a> {
pub registry: &'a registry::Registry,
pub query_type: &'a str,
pub mutation_type: &'a str,
}
#[async_trait::async_trait]
impl<'a> __SchemaFields for __Schema<'a> {
async fn types<'b>(&'b self, _: &Context<'_>) -> Result<Vec<__Type<'b>>> {
Ok(self
.registry
#[Object(
internal,
desc = "A GraphQL Schema defines the capabilities of a GraphQL server. It exposes all available types and directives on the server, as well as the entry points for query, mutation, and subscription operations."
)]
impl<'a> __Schema<'a> {
#[field(desc = "A list of all types supported by this server.")]
async fn types(&self) -> Vec<__Type<'a>> {
self.registry
.types
.values()
.map(|ty| __Type::new_simple(self.registry, ty))
.collect())
.collect()
}
async fn query_type<'b>(&'b self, _: &Context<'_>) -> Result<__Type<'b>> {
Ok(__Type::new_simple(
#[field(
name = "queryType",
desc = "The type that query operations will be rooted at."
)]
async fn query_type(&self) -> __Type<'a> {
__Type::new_simple(self.registry, &self.registry.types[self.query_type])
}
#[field(
name = "mutationType",
desc = "If this server supports mutation, the type that mutation operations will be rooted at."
)]
async fn mutation_type(&self) -> Option<__Type<'a>> {
Some(__Type::new_simple(
self.registry,
&self.registry.types[self.query_type],
&self.registry.types[self.mutation_type],
))
}
async fn mutation_type<'b>(&'b self, _: &Context<'_>) -> Result<Option<__Type<'b>>> {
Ok(Some(__Type::new_simple(
self.registry,
&self.registry.types[self.mutation_type],
)))
#[field(
name = "subscriptionType",
desc = "If this server support subscription, the type that subscription operations will be rooted at."
)]
async fn subscription_type(&self) -> Option<__Type<'a>> {
None
}
async fn subscription_type<'b>(&'b self, _: &Context<'_>) -> Result<Option<__Type<'b>>> {
Ok(None)
}
async fn directives<'b>(&'b self, _: &Context<'_>) -> Result<Vec<__Directive<'b>>> {
Ok(self
.registry
#[field(desc = "A list of all directives supported by this server.")]
async fn directives(&self) -> Vec<__Directive<'a>> {
self.registry
.directives
.iter()
.map(|directive| __Directive {
registry: &self.registry,
directive,
})
.collect())
.collect()
}
}

View File

@ -1,6 +1,6 @@
use crate::model::{__EnumValue, __Field, __InputValue, __TypeKind};
use crate::registry;
use crate::registry::Type;
use crate::{registry, Context, Result};
use async_graphql_derive::Object;
enum TypeDetail<'a> {
@ -9,33 +9,6 @@ enum TypeDetail<'a> {
List(&'a registry::Type),
}
#[Object(
internal,
desc = r#"
The fundamental unit of any GraphQL Schema is the type. There are many kinds of types in GraphQL as represented by the `__TypeKind` enum.
Depending on the kind of a type, certain fields describe information about that type. Scalar types provide no information beyond a name and description, while Enum types provide their values. Object and Interface types provide the fields they describe. Abstract types, Union and Interface, provide the Object types possible at runtime. List and NonNull types compose other types.
"#,
field(name = "kind", type = "__TypeKind", owned),
field(name = "name", type = "Option<String>", owned),
field(name = "description", type = "Option<String>", owned),
field(
name = "fields",
type = "Option<Vec<__Field>>",
owned,
arg(name = "includeDeprecated", type = "bool", default = "false")
),
field(name = "interfaces", type = "Option<Vec<__Type>>", owned),
field(name = "possibleTypes", type = "Option<Vec<__Type>>", owned),
field(
name = "enumValues",
type = "Option<Vec<__EnumValue>>",
owned,
arg(name = "includeDeprecated", type = "bool", default = "false")
),
field(name = "inputFields", type = "Option<Vec<__InputValue>>", owned),
field(name = "ofType", type = "Option<__Type>", owned)
)]
pub struct __Type<'a> {
registry: &'a registry::Registry,
detail: TypeDetail<'a>,
@ -69,41 +42,51 @@ impl<'a> __Type<'a> {
}
}
#[async_trait::async_trait]
impl<'a> __TypeFields for __Type<'a> {
async fn kind(&self, _: &Context<'_>) -> Result<__TypeKind> {
#[Object(
internal,
desc = r#"
The fundamental unit of any GraphQL Schema is the type. There are many kinds of types in GraphQL as represented by the `__TypeKind` enum.
Depending on the kind of a type, certain fields describe information about that type. Scalar types provide no information beyond a name and description, while Enum types provide their values. Object and Interface types provide the fields they describe. Abstract types, Union and Interface, provide the Object types possible at runtime. List and NonNull types compose other types.
"#
)]
impl<'a> __Type<'a> {
#[field]
async fn kind(&self) -> __TypeKind {
match &self.detail {
TypeDetail::Simple(ty) => Ok(match ty {
TypeDetail::Simple(ty) => match ty {
registry::Type::Scalar { .. } => __TypeKind::SCALAR,
registry::Type::Object { .. } => __TypeKind::OBJECT,
registry::Type::Interface { .. } => __TypeKind::INTERFACE,
registry::Type::Union { .. } => __TypeKind::UNION,
registry::Type::Enum { .. } => __TypeKind::ENUM,
registry::Type::InputObject { .. } => __TypeKind::INPUT_OBJECT,
}),
TypeDetail::NonNull(_) => Ok(__TypeKind::NON_NULL),
TypeDetail::List(_) => Ok(__TypeKind::LIST),
},
TypeDetail::NonNull(_) => __TypeKind::NON_NULL,
TypeDetail::List(_) => __TypeKind::LIST,
}
}
async fn name(&self, _: &Context<'_>) -> Result<Option<String>> {
#[field]
async fn name(&self) -> Option<String> {
match &self.detail {
TypeDetail::Simple(ty) => Ok(match ty {
TypeDetail::Simple(ty) => match ty {
registry::Type::Scalar { name, .. } => Some(name.clone()),
registry::Type::Object { name, .. } => Some(name.to_string()),
registry::Type::Interface { name, .. } => Some(name.to_string()),
registry::Type::Union { name, .. } => Some(name.to_string()),
registry::Type::Enum { name, .. } => Some(name.to_string()),
registry::Type::InputObject { name, .. } => Some(name.to_string()),
}),
TypeDetail::NonNull(_) => Ok(None),
TypeDetail::List(_) => Ok(None),
},
TypeDetail::NonNull(_) => None,
TypeDetail::List(_) => None,
}
}
async fn description(&self, _: &Context<'_>) -> Result<Option<String>> {
#[field]
async fn description(&self) -> Option<String> {
match &self.detail {
TypeDetail::Simple(ty) => Ok(match ty {
TypeDetail::Simple(ty) => match ty {
registry::Type::Scalar { description, .. } => description.map(|s| s.to_string()),
registry::Type::Object { description, .. } => description.map(|s| s.to_string()),
registry::Type::Interface { description, .. } => description.map(|s| s.to_string()),
@ -112,19 +95,19 @@ impl<'a> __TypeFields for __Type<'a> {
registry::Type::InputObject { description, .. } => {
description.map(|s| s.to_string())
}
}),
TypeDetail::NonNull(_) => Ok(None),
TypeDetail::List(_) => Ok(None),
},
TypeDetail::NonNull(_) => None,
TypeDetail::List(_) => None,
}
}
async fn fields<'b>(
&'b self,
_: &Context<'_>,
include_deprecated: bool,
) -> Result<Option<Vec<__Field<'b>>>> {
#[field]
async fn fields(
&self,
#[arg(name = "includeDeprecated", default = "false")] include_deprecated: bool,
) -> Option<Vec<__Field<'a>>> {
if let TypeDetail::Simple(Type::Object { fields, .. }) = &self.detail {
Ok(Some(
Some(
fields
.iter()
.filter(|field| {
@ -139,27 +122,29 @@ impl<'a> __TypeFields for __Type<'a> {
field,
})
.collect(),
))
)
} else {
Ok(None)
None
}
}
async fn interfaces<'b>(&'b self, _: &Context<'_>) -> Result<Option<Vec<__Type<'b>>>> {
Ok(None)
#[field]
async fn interfaces(&self) -> Option<Vec<__Type<'a>>> {
None
}
async fn possible_types<'b>(&'b self, _: &Context<'_>) -> Result<Option<Vec<__Type<'b>>>> {
Ok(None)
#[field]
async fn possible_types(&self) -> Option<Vec<__Type<'a>>> {
None
}
async fn enum_values<'b>(
&'b self,
_: &Context<'_>,
include_deprecated: bool,
) -> Result<Option<Vec<__EnumValue<'b>>>> {
#[field(name = "enumValues")]
async fn enum_values(
&self,
#[arg(name = "includeDeprecated", default = "false")] include_deprecated: bool,
) -> Option<Vec<__EnumValue<'a>>> {
if let TypeDetail::Simple(Type::Enum { enum_values, .. }) = &self.detail {
Ok(Some(
Some(
enum_values
.iter()
.filter(|field| {
@ -174,15 +159,16 @@ impl<'a> __TypeFields for __Type<'a> {
value,
})
.collect(),
))
)
} else {
Ok(None)
None
}
}
async fn input_fields<'b>(&'b self, _: &Context<'_>) -> Result<Option<Vec<__InputValue<'b>>>> {
#[field(name = "inputFields")]
async fn input_fields(&self) -> Option<Vec<__InputValue<'a>>> {
if let TypeDetail::Simple(Type::InputObject { input_fields, .. }) = &self.detail {
Ok(Some(
Some(
input_fields
.iter()
.map(|input_value| __InputValue {
@ -190,25 +176,26 @@ impl<'a> __TypeFields for __Type<'a> {
input_value,
})
.collect(),
))
)
} else {
Ok(None)
None
}
}
async fn of_type<'b>(&'b self, _: &Context<'_>) -> Result<Option<__Type<'b>>> {
#[field(name = "ofType")]
async fn of_type(&self) -> Option<__Type<'a>> {
if let TypeDetail::List(ty) = &self.detail {
Ok(Some(__Type {
Some(__Type {
registry: self.registry,
detail: TypeDetail::Simple(ty),
}))
})
} else if let TypeDetail::NonNull(ty) = &self.detail {
Ok(Some(__Type {
Some(__Type {
registry: self.registry,
detail: TypeDetail::Simple(ty),
}))
})
} else {
Ok(None)
None
}
}
}

View File

@ -1,6 +1,6 @@
use crate::{Result, Scalar, Value};
use crate::{Result, GQLScalar, Value};
impl Scalar for bool {
impl GQLScalar for bool {
fn type_name() -> &'static str {
"Boolean"
}

View File

@ -1,7 +1,7 @@
use crate::{Result, Scalar, Value};
use crate::{Result, GQLScalar, Value};
use chrono::{DateTime, TimeZone, Utc};
impl Scalar for DateTime<Utc> {
impl GQLScalar for DateTime<Utc> {
fn type_name() -> &'static str {
"DateTime"
}

View File

@ -1,9 +1,9 @@
use crate::{Result, Scalar, Value};
use crate::{Result, GQLScalar, Value};
macro_rules! impl_float_scalars {
($($ty:ty),*) => {
$(
impl Scalar for $ty {
impl GQLScalar for $ty {
fn type_name() -> &'static str {
"Float"
}

View File

@ -1,4 +1,4 @@
use crate::{Result, Scalar, Value};
use crate::{Result, GQLScalar, Value};
use std::ops::{Deref, DerefMut};
#[derive(Clone, Ord, PartialOrd, Eq, PartialEq, Hash, Debug)]
@ -18,7 +18,7 @@ impl DerefMut for ID {
}
}
impl Scalar for ID {
impl GQLScalar for ID {
fn type_name() -> &'static str {
"ID"
}

View File

@ -1,9 +1,9 @@
use crate::{Result, Scalar, Value};
use crate::{Result, GQLScalar, Value};
macro_rules! impl_integer_scalars {
($($ty:ty),*) => {
$(
impl Scalar for $ty {
impl GQLScalar for $ty {
fn type_name() -> &'static str {
"Int"
}

View File

@ -10,3 +10,47 @@ mod datetime;
mod uuid;
pub use id::ID;
#[cfg(test)]
mod tests {
use super::ID;
use crate::GQLType;
use chrono::{DateTime, Utc};
use uuid::Uuid;
#[test]
fn test_scalar_type() {
assert_eq!(<bool as GQLType>::type_name(), "Boolean");
assert_eq!(<bool as GQLType>::qualified_type_name(), "Boolean!");
assert_eq!(<i32 as GQLType>::type_name(), "Int");
assert_eq!(<i32 as GQLType>::qualified_type_name(), "Int!");
assert_eq!(<f32 as GQLType>::type_name(), "Float");
assert_eq!(<f32 as GQLType>::qualified_type_name(), "Float!");
assert_eq!(<&str as GQLType>::type_name(), "String");
assert_eq!(<&str as GQLType>::qualified_type_name(), "String!");
assert_eq!(<String as GQLType>::type_name(), "String");
assert_eq!(<String as GQLType>::qualified_type_name(), "String!");
assert_eq!(<ID as GQLType>::type_name(), "ID");
assert_eq!(<ID as GQLType>::qualified_type_name(), "ID!");
#[cfg(feature = "chrono")]
{
assert_eq!(<DateTime::<Utc> as GQLType>::type_name(), "DateTime");
assert_eq!(
<DateTime::<Utc> as GQLType>::qualified_type_name(),
"DateTime!"
);
}
#[cfg(feature = "uuid")]
{
assert_eq!(<Uuid as GQLType>::type_name(), "UUID");
assert_eq!(<Uuid as GQLType>::qualified_type_name(), "UUID!");
}
}
}

View File

@ -1,10 +1,10 @@
use crate::registry;
use crate::{ContextSelectionSet, GQLOutputValue, GQLType, Result, Scalar, Value};
use crate::{ContextSelectionSet, GQLOutputValue, GQLType, Result, GQLScalar, Value};
use std::borrow::Cow;
const STRING_DESC:&'static str = "The `String` scalar type represents textual data, represented as UTF-8 character sequences. The String type is most often used by GraphQL to represent free-form human-readable text.";
impl Scalar for String {
impl GQLScalar for String {
fn type_name() -> &'static str {
"String"
}

View File

@ -1,7 +1,7 @@
use crate::{Result, Scalar, Value};
use crate::{Result, GQLScalar, Value};
use uuid::Uuid;
impl Scalar for Uuid {
impl GQLScalar for Uuid {
fn type_name() -> &'static str {
"UUID"
}

View File

@ -3,11 +3,11 @@ use std::borrow::Cow;
impl<T: GQLType> GQLType for Vec<T> {
fn type_name() -> Cow<'static, str> {
T::type_name()
Cow::Owned(format!("[{}]", T::qualified_type_name()))
}
fn qualified_type_name() -> String {
format!("[{}]", T::qualified_type_name())
format!("[{}]!", T::qualified_type_name())
}
fn create_type_info(registry: &mut registry::Registry) -> String {
@ -61,3 +61,19 @@ impl<T: GQLOutputValue + Send + Sync> GQLOutputValue for &[T] {
Ok(res.into())
}
}
#[cfg(test)]
mod tests {
use crate::GQLType;
#[test]
fn test_list_type() {
assert_eq!(Vec::<i32>::type_name(), "[Int!]");
assert_eq!(Vec::<Option<i32>>::type_name(), "[Int]");
assert_eq!(Option::<Vec::<Option<i32>>>::type_name(), "[Int]");
assert_eq!(Vec::<i32>::qualified_type_name(), "[Int!]!");
assert_eq!(Vec::<Option<i32>>::qualified_type_name(), "[Int]!");
assert_eq!(Option::<Vec::<Option<i32>>>::qualified_type_name(), "[Int]");
}
}

View File

@ -34,3 +34,14 @@ impl<T: GQLOutputValue + Sync> GQLOutputValue for Option<T> {
}
}
}
#[cfg(test)]
mod tests {
use crate::GQLType;
#[test]
fn test_optional_type() {
assert_eq!(Option::<i32>::type_name(), "Int");
assert_eq!(Option::<i32>::qualified_type_name(), "Int");
}
}