2020-03-06 15:58:43 +00:00
|
|
|
use crate::args;
|
|
|
|
use crate::args::{InterfaceField, InterfaceFieldArgument};
|
|
|
|
use crate::output_type::OutputType;
|
2020-09-28 09:44:00 +00:00
|
|
|
use crate::utils::{generate_default, get_crate_name, get_rustdoc, GeneratorResult};
|
|
|
|
use darling::ast::{Data, Style};
|
2020-03-09 12:00:57 +00:00
|
|
|
use inflector::Inflector;
|
2020-03-06 15:58:43 +00:00
|
|
|
use proc_macro::TokenStream;
|
|
|
|
use proc_macro2::{Ident, Span};
|
|
|
|
use quote::quote;
|
2020-05-11 03:03:13 +00:00
|
|
|
use std::collections::HashSet;
|
2020-09-28 09:44:00 +00:00
|
|
|
use syn::{Error, Type};
|
2020-03-06 15:58:43 +00:00
|
|
|
|
2020-09-28 09:44:00 +00:00
|
|
|
pub fn generate(interface_args: &args::Interface) -> GeneratorResult<TokenStream> {
|
2020-03-06 15:58:43 +00:00
|
|
|
let crate_name = get_crate_name(interface_args.internal);
|
2020-09-28 09:44:00 +00:00
|
|
|
let ident = &interface_args.ident;
|
|
|
|
let generics = &interface_args.generics;
|
|
|
|
let s = match &interface_args.data {
|
2020-05-11 03:03:13 +00:00
|
|
|
Data::Enum(s) => s,
|
|
|
|
_ => {
|
2020-09-28 09:44:00 +00:00
|
|
|
return Err(
|
|
|
|
Error::new_spanned(ident, "Interface can only be applied to an enum.").into(),
|
|
|
|
)
|
2020-05-11 03:03:13 +00:00
|
|
|
}
|
2020-03-06 15:58:43 +00:00
|
|
|
};
|
2020-04-09 14:03:09 +00:00
|
|
|
let extends = interface_args.extends;
|
2020-03-06 15:58:43 +00:00
|
|
|
let mut enum_names = Vec::new();
|
2020-05-11 03:03:13 +00:00
|
|
|
let mut enum_items = HashSet::new();
|
2020-03-06 15:58:43 +00:00
|
|
|
let mut type_into_impls = Vec::new();
|
|
|
|
let gql_typename = interface_args
|
|
|
|
.name
|
|
|
|
.clone()
|
|
|
|
.unwrap_or_else(|| ident.to_string());
|
2020-05-10 04:41:05 +00:00
|
|
|
|
2020-09-28 09:44:00 +00:00
|
|
|
let desc = get_rustdoc(&interface_args.attrs)?
|
2020-05-10 04:41:05 +00:00
|
|
|
.map(|s| quote! { Some(#s) })
|
2020-03-06 15:58:43 +00:00
|
|
|
.unwrap_or_else(|| quote! {None});
|
2020-05-10 04:41:05 +00:00
|
|
|
|
2020-03-06 15:58:43 +00:00
|
|
|
let mut registry_types = Vec::new();
|
|
|
|
let mut possible_types = Vec::new();
|
2020-04-02 12:37:01 +00:00
|
|
|
let mut get_introspection_typename = Vec::new();
|
2020-09-12 09:29:52 +00:00
|
|
|
let mut collect_all_fields = Vec::new();
|
2020-03-06 15:58:43 +00:00
|
|
|
|
2020-09-28 09:44:00 +00:00
|
|
|
for variant in s {
|
2020-05-11 03:03:13 +00:00
|
|
|
let enum_name = &variant.ident;
|
2020-09-28 09:44:00 +00:00
|
|
|
let ty = match variant.fields.style {
|
|
|
|
Style::Tuple if variant.fields.fields.len() == 1 => &variant.fields.fields[0],
|
|
|
|
Style::Tuple => {
|
2020-05-11 03:03:13 +00:00
|
|
|
return Err(Error::new_spanned(
|
2020-09-28 09:44:00 +00:00
|
|
|
enum_name,
|
2020-05-11 03:03:13 +00:00
|
|
|
"Only single value variants are supported",
|
2020-09-28 09:44:00 +00:00
|
|
|
)
|
|
|
|
.into())
|
2020-05-11 03:03:13 +00:00
|
|
|
}
|
2020-09-28 09:44:00 +00:00
|
|
|
Style::Unit => {
|
|
|
|
return Err(
|
|
|
|
Error::new_spanned(enum_name, "Empty variants are not supported").into(),
|
|
|
|
)
|
2020-05-11 03:03:13 +00:00
|
|
|
}
|
2020-09-28 09:44:00 +00:00
|
|
|
Style::Struct => {
|
2020-05-11 03:03:13 +00:00
|
|
|
return Err(Error::new_spanned(
|
2020-09-28 09:44:00 +00:00
|
|
|
enum_name,
|
2020-05-11 03:03:13 +00:00
|
|
|
"Variants with named fields are not supported",
|
2020-09-28 09:44:00 +00:00
|
|
|
)
|
|
|
|
.into())
|
2020-05-11 03:03:13 +00:00
|
|
|
}
|
|
|
|
};
|
2020-09-28 09:44:00 +00:00
|
|
|
|
|
|
|
if let Type::Path(p) = ty {
|
2020-05-11 03:03:13 +00:00
|
|
|
// This validates that the field type wasn't already used
|
2020-05-11 13:47:24 +00:00
|
|
|
if !enum_items.insert(p) {
|
2020-09-28 09:44:00 +00:00
|
|
|
return Err(
|
|
|
|
Error::new_spanned(ty, "This type already used in another variant").into(),
|
|
|
|
);
|
2020-05-11 03:03:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type_into_impls.push(quote! {
|
2020-06-28 06:28:53 +00:00
|
|
|
#[allow(clippy::all, clippy::pedantic)]
|
2020-09-17 05:43:08 +00:00
|
|
|
impl #generics ::std::convert::From<#p> for #ident #generics {
|
2020-05-11 03:03:13 +00:00
|
|
|
fn from(obj: #p) -> Self {
|
|
|
|
#ident::#enum_name(obj)
|
2020-03-06 15:58:43 +00:00
|
|
|
}
|
2020-05-11 03:03:13 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
enum_names.push(enum_name);
|
2020-04-20 06:37:28 +00:00
|
|
|
|
2020-05-11 03:03:13 +00:00
|
|
|
registry_types.push(quote! {
|
|
|
|
<#p as #crate_name::Type>::create_type_info(registry);
|
|
|
|
registry.add_implements(&<#p as #crate_name::Type>::type_name(), #gql_typename);
|
|
|
|
});
|
2020-04-20 06:37:28 +00:00
|
|
|
|
2020-05-11 03:03:13 +00:00
|
|
|
possible_types.push(quote! {
|
|
|
|
possible_types.insert(<#p as #crate_name::Type>::type_name().to_string());
|
|
|
|
});
|
2020-04-20 06:37:28 +00:00
|
|
|
|
2020-05-11 03:03:13 +00:00
|
|
|
get_introspection_typename.push(quote! {
|
|
|
|
#ident::#enum_name(obj) => <#p as #crate_name::Type>::type_name()
|
2020-09-12 09:29:52 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
collect_all_fields.push(quote! {
|
|
|
|
#ident::#enum_name(obj) => obj.collect_all_fields(ctx, fields)
|
|
|
|
});
|
2020-05-11 03:03:13 +00:00
|
|
|
} else {
|
2020-09-28 09:44:00 +00:00
|
|
|
return Err(Error::new_spanned(ty, "Invalid type").into());
|
2020-03-06 15:58:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut methods = Vec::new();
|
|
|
|
let mut schema_fields = Vec::new();
|
|
|
|
let mut resolvers = Vec::new();
|
|
|
|
|
|
|
|
for InterfaceField {
|
|
|
|
name,
|
2020-05-27 02:25:23 +00:00
|
|
|
method,
|
2020-03-06 15:58:43 +00:00
|
|
|
desc,
|
|
|
|
ty,
|
|
|
|
args,
|
|
|
|
deprecation,
|
2020-04-09 14:03:09 +00:00
|
|
|
external,
|
|
|
|
provides,
|
|
|
|
requires,
|
2020-03-06 15:58:43 +00:00
|
|
|
} in &interface_args.fields
|
|
|
|
{
|
2020-05-27 02:25:23 +00:00
|
|
|
let (name, method_name) = if let Some(method) = method {
|
|
|
|
(name.to_string(), Ident::new(method, Span::call_site()))
|
|
|
|
} else {
|
|
|
|
let method_name = Ident::new(&name, Span::call_site());
|
|
|
|
(name.to_camel_case(), method_name)
|
|
|
|
};
|
2020-09-28 09:44:00 +00:00
|
|
|
let ty = match syn::parse_str::<syn::Type>(&ty.value()) {
|
|
|
|
Ok(ty) => ty,
|
|
|
|
Err(_) => return Err(Error::new_spanned(&ty, "Expect type").into()),
|
|
|
|
};
|
2020-03-06 15:58:43 +00:00
|
|
|
let mut calls = Vec::new();
|
|
|
|
let mut use_params = Vec::new();
|
|
|
|
let mut decl_params = Vec::new();
|
|
|
|
let mut get_params = Vec::new();
|
|
|
|
let mut schema_args = Vec::new();
|
2020-04-09 14:03:09 +00:00
|
|
|
let requires = match &requires {
|
|
|
|
Some(requires) => quote! { Some(#requires) },
|
|
|
|
None => quote! { None },
|
|
|
|
};
|
|
|
|
let provides = match &provides {
|
|
|
|
Some(provides) => quote! { Some(#provides) },
|
|
|
|
None => quote! { None },
|
|
|
|
};
|
2020-03-06 15:58:43 +00:00
|
|
|
|
2020-05-01 23:57:34 +00:00
|
|
|
decl_params.push(quote! { ctx: &'ctx #crate_name::Context<'ctx> });
|
|
|
|
use_params.push(quote! { ctx });
|
2020-03-07 03:21:56 +00:00
|
|
|
|
2020-03-06 15:58:43 +00:00
|
|
|
for InterfaceFieldArgument {
|
|
|
|
name,
|
|
|
|
desc,
|
|
|
|
ty,
|
|
|
|
default,
|
2020-09-28 09:44:00 +00:00
|
|
|
default_with,
|
2020-03-06 15:58:43 +00:00
|
|
|
} in args
|
|
|
|
{
|
|
|
|
let ident = Ident::new(name, Span::call_site());
|
2020-03-09 12:00:57 +00:00
|
|
|
let name = name.to_camel_case();
|
2020-09-28 09:44:00 +00:00
|
|
|
let ty = match syn::parse_str::<syn::Type>(&ty.value()) {
|
|
|
|
Ok(ty) => ty,
|
|
|
|
Err(_) => return Err(Error::new_spanned(&ty, "Expect type").into()),
|
|
|
|
};
|
2020-03-06 15:58:43 +00:00
|
|
|
decl_params.push(quote! { #ident: #ty });
|
2020-03-07 03:21:56 +00:00
|
|
|
use_params.push(quote! { #ident });
|
2020-03-06 15:58:43 +00:00
|
|
|
|
2020-09-28 09:44:00 +00:00
|
|
|
let default = generate_default(&default, &default_with)?;
|
|
|
|
let get_default = match &default {
|
2020-05-26 10:34:43 +00:00
|
|
|
Some(default) => quote! { Some(|| -> #ty { #default }) },
|
|
|
|
None => quote! { None },
|
2020-03-06 15:58:43 +00:00
|
|
|
};
|
|
|
|
get_params.push(quote! {
|
2020-05-26 10:34:43 +00:00
|
|
|
let #ident: #ty = ctx.param_value(#name, #get_default)?;
|
2020-03-06 15:58:43 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
let desc = desc
|
|
|
|
.as_ref()
|
|
|
|
.map(|s| quote! {Some(#s)})
|
|
|
|
.unwrap_or_else(|| quote! {None});
|
|
|
|
let schema_default = default
|
|
|
|
.as_ref()
|
2020-05-26 10:34:43 +00:00
|
|
|
.map(|value| {
|
|
|
|
quote! {Some( <#ty as #crate_name::InputValueType>::to_value(&#value).to_string() )}
|
2020-03-06 15:58:43 +00:00
|
|
|
})
|
|
|
|
.unwrap_or_else(|| quote! {None});
|
|
|
|
schema_args.push(quote! {
|
2020-05-15 02:08:37 +00:00
|
|
|
args.insert(#name, #crate_name::registry::MetaInputValue {
|
2020-03-06 15:58:43 +00:00
|
|
|
name: #name,
|
|
|
|
description: #desc,
|
2020-03-19 09:20:12 +00:00
|
|
|
ty: <#ty as #crate_name::Type>::create_type_info(registry),
|
2020-03-06 15:58:43 +00:00
|
|
|
default_value: #schema_default,
|
2020-03-22 01:34:32 +00:00
|
|
|
validator: None,
|
2020-03-08 12:35:36 +00:00
|
|
|
});
|
2020-03-06 15:58:43 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
for enum_name in &enum_names {
|
|
|
|
calls.push(quote! {
|
|
|
|
#ident::#enum_name(obj) => obj.#method_name(#(#use_params),*).await
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
let desc = desc
|
|
|
|
.as_ref()
|
|
|
|
.map(|s| quote! {Some(#s)})
|
|
|
|
.unwrap_or_else(|| quote! {None});
|
|
|
|
let deprecation = deprecation
|
|
|
|
.as_ref()
|
|
|
|
.map(|s| quote! {Some(#s)})
|
|
|
|
.unwrap_or_else(|| quote! {None});
|
|
|
|
|
2020-09-28 09:44:00 +00:00
|
|
|
let oty = OutputType::parse(&ty)?;
|
2020-05-01 23:57:34 +00:00
|
|
|
let ty = match oty {
|
|
|
|
OutputType::Value(ty) => ty,
|
|
|
|
OutputType::Result(_, ty) => ty,
|
|
|
|
};
|
|
|
|
let schema_ty = oty.value_type();
|
|
|
|
|
|
|
|
methods.push(quote! {
|
|
|
|
#[inline]
|
Rework errors
This completely overhauls the error system used in async-graphql.
- `Error` has been renamed to `ServerError` and `FieldError` has been
renamed to just `Error`. This is because `FieldError` is by far the most
common error that users will have to use so it makes sense to use the
most obvious error name. Also, the current name didn't make sense as it
was used for things other than field errors, such as the data callback
for websockets.
- `ServerError` has been made completely opaque. Before it was an enum
of all the possible errors, but now it just contains an error message,
the locations, the path and extensions. It is a shame that we lose
information, it makes more sense as _conceptually_ GraphQL does not
provide that information. It also frees us to change the internals of
async-graphql a lot more.
- The path of errors is no longer an opaque JSON value but a regular
type, `Vec<PathSegment>`. The type duplication of `PathSegment` and
`QueryPathSegment` is unfortunate, I plan to work on this in the future.
- Now that `ServerError` is opaque, `RuleError` has been removed from
the public API, making it simpler.
- Additionally `QueryError` has been completely removed. Instead the
error messages are constructed ad-hoc; I took care to never repeat an
error message.
- Instead of constructing field-not-found errors inside the
implementations of field resolvers they now return `Option`s, where a
`None` value is representative of the field not being found.
- As an unfortunate consequence of the last change, self-referential
types based on the output of a subscription resolver can no longer be
created. This does not mean anything for users, but causes lifetime
issues in the implementation of merged objects. I fixed it with a bit of
a hack, but this'll have to be looked into further.
- `InputValueError` now has a generic parameter - it's kind of weird but
it's necessary for ergonomics. It also improves error messages.
- The `ErrorExtensions` trait has been removed. I didn't think the
`extend` method was necessary since `From` impls exist. But the
ergonomics are still there with a new trait `ExtendError`, which
is implemented for both errors and results.
- `Response` now supports serializing multiple errors. This allows for
nice things like having multiple validation errors not be awkwardly
shoved into a single error.
- When an error occurs in execution, data is sent as `null`. This is
slightly more compliant with the spec but the algorithm described in
<https://spec.graphql.org/June2018/#sec-Errors-and-Non-Nullability> has
yet to be implemented.
2020-09-29 19:06:44 +00:00
|
|
|
async fn #method_name <'ctx>(&self, #(#decl_params),*) -> #crate_name::Result<#ty> {
|
2020-05-01 23:57:34 +00:00
|
|
|
match self {
|
|
|
|
#(#calls,)*
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2020-03-06 15:58:43 +00:00
|
|
|
|
|
|
|
schema_fields.push(quote! {
|
2020-05-15 02:08:37 +00:00
|
|
|
fields.insert(#name.to_string(), #crate_name::registry::MetaField {
|
2020-03-19 09:20:12 +00:00
|
|
|
name: #name.to_string(),
|
2020-03-06 15:58:43 +00:00
|
|
|
description: #desc,
|
2020-03-08 12:35:36 +00:00
|
|
|
args: {
|
2020-05-16 02:05:48 +00:00
|
|
|
let mut args = #crate_name::indexmap::IndexMap::new();
|
2020-03-08 12:35:36 +00:00
|
|
|
#(#schema_args)*
|
|
|
|
args
|
|
|
|
},
|
2020-03-19 09:20:12 +00:00
|
|
|
ty: <#schema_ty as #crate_name::Type>::create_type_info(registry),
|
2020-03-06 15:58:43 +00:00
|
|
|
deprecation: #deprecation,
|
2020-03-22 08:45:59 +00:00
|
|
|
cache_control: Default::default(),
|
2020-04-09 14:03:09 +00:00
|
|
|
external: #external,
|
|
|
|
provides: #provides,
|
|
|
|
requires: #requires,
|
2020-03-08 12:35:36 +00:00
|
|
|
});
|
2020-03-06 15:58:43 +00:00
|
|
|
});
|
|
|
|
|
2020-05-01 23:57:34 +00:00
|
|
|
let resolve_obj = quote! {
|
|
|
|
self.#method_name(#(#use_params),*).await.
|
Rework errors
This completely overhauls the error system used in async-graphql.
- `Error` has been renamed to `ServerError` and `FieldError` has been
renamed to just `Error`. This is because `FieldError` is by far the most
common error that users will have to use so it makes sense to use the
most obvious error name. Also, the current name didn't make sense as it
was used for things other than field errors, such as the data callback
for websockets.
- `ServerError` has been made completely opaque. Before it was an enum
of all the possible errors, but now it just contains an error message,
the locations, the path and extensions. It is a shame that we lose
information, it makes more sense as _conceptually_ GraphQL does not
provide that information. It also frees us to change the internals of
async-graphql a lot more.
- The path of errors is no longer an opaque JSON value but a regular
type, `Vec<PathSegment>`. The type duplication of `PathSegment` and
`QueryPathSegment` is unfortunate, I plan to work on this in the future.
- Now that `ServerError` is opaque, `RuleError` has been removed from
the public API, making it simpler.
- Additionally `QueryError` has been completely removed. Instead the
error messages are constructed ad-hoc; I took care to never repeat an
error message.
- Instead of constructing field-not-found errors inside the
implementations of field resolvers they now return `Option`s, where a
`None` value is representative of the field not being found.
- As an unfortunate consequence of the last change, self-referential
types based on the output of a subscription resolver can no longer be
created. This does not mean anything for users, but causes lifetime
issues in the implementation of merged objects. I fixed it with a bit of
a hack, but this'll have to be looked into further.
- `InputValueError` now has a generic parameter - it's kind of weird but
it's necessary for ergonomics. It also improves error messages.
- The `ErrorExtensions` trait has been removed. I didn't think the
`extend` method was necessary since `From` impls exist. But the
ergonomics are still there with a new trait `ExtendError`, which
is implemented for both errors and results.
- `Response` now supports serializing multiple errors. This allows for
nice things like having multiple validation errors not be awkwardly
shoved into a single error.
- When an error occurs in execution, data is sent as `null`. This is
slightly more compliant with the spec but the algorithm described in
<https://spec.graphql.org/June2018/#sec-Errors-and-Non-Nullability> has
yet to be implemented.
2020-09-29 19:06:44 +00:00
|
|
|
map_err(|err| err.into_server_error().at(ctx.item.pos))?
|
2020-03-06 15:58:43 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
resolvers.push(quote! {
|
2020-09-12 16:42:15 +00:00
|
|
|
if ctx.item.node.name.node == #name {
|
2020-03-06 15:58:43 +00:00
|
|
|
#(#get_params)*
|
2020-09-12 16:42:15 +00:00
|
|
|
let ctx_obj = ctx.with_selection_set(&ctx.item.node.selection_set);
|
Rework errors
This completely overhauls the error system used in async-graphql.
- `Error` has been renamed to `ServerError` and `FieldError` has been
renamed to just `Error`. This is because `FieldError` is by far the most
common error that users will have to use so it makes sense to use the
most obvious error name. Also, the current name didn't make sense as it
was used for things other than field errors, such as the data callback
for websockets.
- `ServerError` has been made completely opaque. Before it was an enum
of all the possible errors, but now it just contains an error message,
the locations, the path and extensions. It is a shame that we lose
information, it makes more sense as _conceptually_ GraphQL does not
provide that information. It also frees us to change the internals of
async-graphql a lot more.
- The path of errors is no longer an opaque JSON value but a regular
type, `Vec<PathSegment>`. The type duplication of `PathSegment` and
`QueryPathSegment` is unfortunate, I plan to work on this in the future.
- Now that `ServerError` is opaque, `RuleError` has been removed from
the public API, making it simpler.
- Additionally `QueryError` has been completely removed. Instead the
error messages are constructed ad-hoc; I took care to never repeat an
error message.
- Instead of constructing field-not-found errors inside the
implementations of field resolvers they now return `Option`s, where a
`None` value is representative of the field not being found.
- As an unfortunate consequence of the last change, self-referential
types based on the output of a subscription resolver can no longer be
created. This does not mean anything for users, but causes lifetime
issues in the implementation of merged objects. I fixed it with a bit of
a hack, but this'll have to be looked into further.
- `InputValueError` now has a generic parameter - it's kind of weird but
it's necessary for ergonomics. It also improves error messages.
- The `ErrorExtensions` trait has been removed. I didn't think the
`extend` method was necessary since `From` impls exist. But the
ergonomics are still there with a new trait `ExtendError`, which
is implemented for both errors and results.
- `Response` now supports serializing multiple errors. This allows for
nice things like having multiple validation errors not be awkwardly
shoved into a single error.
- When an error occurs in execution, data is sent as `null`. This is
slightly more compliant with the spec but the algorithm described in
<https://spec.graphql.org/June2018/#sec-Errors-and-Non-Nullability> has
yet to be implemented.
2020-09-29 19:06:44 +00:00
|
|
|
return #crate_name::OutputValueType::resolve(&#resolve_obj, &ctx_obj, ctx.item).await.map(::std::option::Option::Some);
|
2020-03-06 15:58:43 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-04-20 06:37:28 +00:00
|
|
|
let introspection_type_name = if get_introspection_typename.is_empty() {
|
|
|
|
quote! { unreachable!() }
|
|
|
|
} else {
|
|
|
|
quote! {
|
|
|
|
match self {
|
|
|
|
#(#get_introspection_typename),*
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-03-06 15:58:43 +00:00
|
|
|
let expanded = quote! {
|
|
|
|
#(#type_into_impls)*
|
|
|
|
|
2020-06-28 06:28:53 +00:00
|
|
|
#[allow(clippy::all, clippy::pedantic)]
|
2020-03-06 15:58:43 +00:00
|
|
|
impl #generics #ident #generics {
|
|
|
|
#(#methods)*
|
|
|
|
}
|
|
|
|
|
2020-06-28 06:28:53 +00:00
|
|
|
#[allow(clippy::all, clippy::pedantic)]
|
2020-03-19 09:20:12 +00:00
|
|
|
impl #generics #crate_name::Type for #ident #generics {
|
2020-06-24 06:04:48 +00:00
|
|
|
fn type_name() -> ::std::borrow::Cow<'static, str> {
|
|
|
|
::std::borrow::Cow::Borrowed(#gql_typename)
|
2020-03-06 15:58:43 +00:00
|
|
|
}
|
|
|
|
|
2020-06-24 06:04:48 +00:00
|
|
|
fn introspection_type_name(&self) -> ::std::borrow::Cow<'static, str> {
|
2020-04-20 06:37:28 +00:00
|
|
|
#introspection_type_name
|
2020-04-02 12:37:01 +00:00
|
|
|
}
|
|
|
|
|
2020-03-06 15:58:43 +00:00
|
|
|
fn create_type_info(registry: &mut #crate_name::registry::Registry) -> String {
|
|
|
|
registry.create_type::<Self, _>(|registry| {
|
|
|
|
#(#registry_types)*
|
|
|
|
|
2020-05-15 02:08:37 +00:00
|
|
|
#crate_name::registry::MetaType::Interface {
|
2020-03-19 09:20:12 +00:00
|
|
|
name: #gql_typename.to_string(),
|
2020-03-06 15:58:43 +00:00
|
|
|
description: #desc,
|
2020-03-08 12:35:36 +00:00
|
|
|
fields: {
|
2020-05-16 02:05:48 +00:00
|
|
|
let mut fields = #crate_name::indexmap::IndexMap::new();
|
2020-03-08 12:35:36 +00:00
|
|
|
#(#schema_fields)*
|
|
|
|
fields
|
|
|
|
},
|
2020-03-10 10:07:47 +00:00
|
|
|
possible_types: {
|
2020-05-16 02:05:48 +00:00
|
|
|
let mut possible_types = #crate_name::indexmap::IndexSet::new();
|
2020-03-10 10:07:47 +00:00
|
|
|
#(#possible_types)*
|
|
|
|
possible_types
|
|
|
|
},
|
2020-04-09 14:03:09 +00:00
|
|
|
extends: #extends,
|
|
|
|
keys: None,
|
2020-03-06 15:58:43 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-28 06:28:53 +00:00
|
|
|
#[allow(clippy::all, clippy::pedantic)]
|
2020-03-06 15:58:43 +00:00
|
|
|
#[#crate_name::async_trait::async_trait]
|
2020-09-29 23:45:48 +00:00
|
|
|
impl #generics #crate_name::resolver_utils::ContainerType for #ident #generics {
|
Rework errors
This completely overhauls the error system used in async-graphql.
- `Error` has been renamed to `ServerError` and `FieldError` has been
renamed to just `Error`. This is because `FieldError` is by far the most
common error that users will have to use so it makes sense to use the
most obvious error name. Also, the current name didn't make sense as it
was used for things other than field errors, such as the data callback
for websockets.
- `ServerError` has been made completely opaque. Before it was an enum
of all the possible errors, but now it just contains an error message,
the locations, the path and extensions. It is a shame that we lose
information, it makes more sense as _conceptually_ GraphQL does not
provide that information. It also frees us to change the internals of
async-graphql a lot more.
- The path of errors is no longer an opaque JSON value but a regular
type, `Vec<PathSegment>`. The type duplication of `PathSegment` and
`QueryPathSegment` is unfortunate, I plan to work on this in the future.
- Now that `ServerError` is opaque, `RuleError` has been removed from
the public API, making it simpler.
- Additionally `QueryError` has been completely removed. Instead the
error messages are constructed ad-hoc; I took care to never repeat an
error message.
- Instead of constructing field-not-found errors inside the
implementations of field resolvers they now return `Option`s, where a
`None` value is representative of the field not being found.
- As an unfortunate consequence of the last change, self-referential
types based on the output of a subscription resolver can no longer be
created. This does not mean anything for users, but causes lifetime
issues in the implementation of merged objects. I fixed it with a bit of
a hack, but this'll have to be looked into further.
- `InputValueError` now has a generic parameter - it's kind of weird but
it's necessary for ergonomics. It also improves error messages.
- The `ErrorExtensions` trait has been removed. I didn't think the
`extend` method was necessary since `From` impls exist. But the
ergonomics are still there with a new trait `ExtendError`, which
is implemented for both errors and results.
- `Response` now supports serializing multiple errors. This allows for
nice things like having multiple validation errors not be awkwardly
shoved into a single error.
- When an error occurs in execution, data is sent as `null`. This is
slightly more compliant with the spec but the algorithm described in
<https://spec.graphql.org/June2018/#sec-Errors-and-Non-Nullability> has
yet to be implemented.
2020-09-29 19:06:44 +00:00
|
|
|
async fn resolve_field(&self, ctx: &#crate_name::Context<'_>) -> #crate_name::ServerResult<Option<#crate_name::serde_json::Value>> {
|
2020-03-06 15:58:43 +00:00
|
|
|
#(#resolvers)*
|
Rework errors
This completely overhauls the error system used in async-graphql.
- `Error` has been renamed to `ServerError` and `FieldError` has been
renamed to just `Error`. This is because `FieldError` is by far the most
common error that users will have to use so it makes sense to use the
most obvious error name. Also, the current name didn't make sense as it
was used for things other than field errors, such as the data callback
for websockets.
- `ServerError` has been made completely opaque. Before it was an enum
of all the possible errors, but now it just contains an error message,
the locations, the path and extensions. It is a shame that we lose
information, it makes more sense as _conceptually_ GraphQL does not
provide that information. It also frees us to change the internals of
async-graphql a lot more.
- The path of errors is no longer an opaque JSON value but a regular
type, `Vec<PathSegment>`. The type duplication of `PathSegment` and
`QueryPathSegment` is unfortunate, I plan to work on this in the future.
- Now that `ServerError` is opaque, `RuleError` has been removed from
the public API, making it simpler.
- Additionally `QueryError` has been completely removed. Instead the
error messages are constructed ad-hoc; I took care to never repeat an
error message.
- Instead of constructing field-not-found errors inside the
implementations of field resolvers they now return `Option`s, where a
`None` value is representative of the field not being found.
- As an unfortunate consequence of the last change, self-referential
types based on the output of a subscription resolver can no longer be
created. This does not mean anything for users, but causes lifetime
issues in the implementation of merged objects. I fixed it with a bit of
a hack, but this'll have to be looked into further.
- `InputValueError` now has a generic parameter - it's kind of weird but
it's necessary for ergonomics. It also improves error messages.
- The `ErrorExtensions` trait has been removed. I didn't think the
`extend` method was necessary since `From` impls exist. But the
ergonomics are still there with a new trait `ExtendError`, which
is implemented for both errors and results.
- `Response` now supports serializing multiple errors. This allows for
nice things like having multiple validation errors not be awkwardly
shoved into a single error.
- When an error occurs in execution, data is sent as `null`. This is
slightly more compliant with the spec but the algorithm described in
<https://spec.graphql.org/June2018/#sec-Errors-and-Non-Nullability> has
yet to be implemented.
2020-09-29 19:06:44 +00:00
|
|
|
Ok(None)
|
2020-03-06 15:58:43 +00:00
|
|
|
}
|
2020-03-07 02:39:55 +00:00
|
|
|
|
Rework errors
This completely overhauls the error system used in async-graphql.
- `Error` has been renamed to `ServerError` and `FieldError` has been
renamed to just `Error`. This is because `FieldError` is by far the most
common error that users will have to use so it makes sense to use the
most obvious error name. Also, the current name didn't make sense as it
was used for things other than field errors, such as the data callback
for websockets.
- `ServerError` has been made completely opaque. Before it was an enum
of all the possible errors, but now it just contains an error message,
the locations, the path and extensions. It is a shame that we lose
information, it makes more sense as _conceptually_ GraphQL does not
provide that information. It also frees us to change the internals of
async-graphql a lot more.
- The path of errors is no longer an opaque JSON value but a regular
type, `Vec<PathSegment>`. The type duplication of `PathSegment` and
`QueryPathSegment` is unfortunate, I plan to work on this in the future.
- Now that `ServerError` is opaque, `RuleError` has been removed from
the public API, making it simpler.
- Additionally `QueryError` has been completely removed. Instead the
error messages are constructed ad-hoc; I took care to never repeat an
error message.
- Instead of constructing field-not-found errors inside the
implementations of field resolvers they now return `Option`s, where a
`None` value is representative of the field not being found.
- As an unfortunate consequence of the last change, self-referential
types based on the output of a subscription resolver can no longer be
created. This does not mean anything for users, but causes lifetime
issues in the implementation of merged objects. I fixed it with a bit of
a hack, but this'll have to be looked into further.
- `InputValueError` now has a generic parameter - it's kind of weird but
it's necessary for ergonomics. It also improves error messages.
- The `ErrorExtensions` trait has been removed. I didn't think the
`extend` method was necessary since `From` impls exist. But the
ergonomics are still there with a new trait `ExtendError`, which
is implemented for both errors and results.
- `Response` now supports serializing multiple errors. This allows for
nice things like having multiple validation errors not be awkwardly
shoved into a single error.
- When an error occurs in execution, data is sent as `null`. This is
slightly more compliant with the spec but the algorithm described in
<https://spec.graphql.org/June2018/#sec-Errors-and-Non-Nullability> has
yet to be implemented.
2020-09-29 19:06:44 +00:00
|
|
|
fn collect_all_fields<'a>(&'a self, ctx: &#crate_name::ContextSelectionSet<'a>, fields: &mut #crate_name::resolver_utils::Fields<'a>) -> #crate_name::ServerResult<()> {
|
2020-09-12 09:29:52 +00:00
|
|
|
match self {
|
|
|
|
#(#collect_all_fields),*
|
|
|
|
}
|
2020-03-07 02:39:55 +00:00
|
|
|
}
|
2020-03-06 15:58:43 +00:00
|
|
|
}
|
2020-03-19 09:20:12 +00:00
|
|
|
|
2020-06-28 06:28:53 +00:00
|
|
|
#[allow(clippy::all, clippy::pedantic)]
|
2020-03-19 09:20:12 +00:00
|
|
|
#[#crate_name::async_trait::async_trait]
|
|
|
|
impl #generics #crate_name::OutputValueType for #ident #generics {
|
Rework errors
This completely overhauls the error system used in async-graphql.
- `Error` has been renamed to `ServerError` and `FieldError` has been
renamed to just `Error`. This is because `FieldError` is by far the most
common error that users will have to use so it makes sense to use the
most obvious error name. Also, the current name didn't make sense as it
was used for things other than field errors, such as the data callback
for websockets.
- `ServerError` has been made completely opaque. Before it was an enum
of all the possible errors, but now it just contains an error message,
the locations, the path and extensions. It is a shame that we lose
information, it makes more sense as _conceptually_ GraphQL does not
provide that information. It also frees us to change the internals of
async-graphql a lot more.
- The path of errors is no longer an opaque JSON value but a regular
type, `Vec<PathSegment>`. The type duplication of `PathSegment` and
`QueryPathSegment` is unfortunate, I plan to work on this in the future.
- Now that `ServerError` is opaque, `RuleError` has been removed from
the public API, making it simpler.
- Additionally `QueryError` has been completely removed. Instead the
error messages are constructed ad-hoc; I took care to never repeat an
error message.
- Instead of constructing field-not-found errors inside the
implementations of field resolvers they now return `Option`s, where a
`None` value is representative of the field not being found.
- As an unfortunate consequence of the last change, self-referential
types based on the output of a subscription resolver can no longer be
created. This does not mean anything for users, but causes lifetime
issues in the implementation of merged objects. I fixed it with a bit of
a hack, but this'll have to be looked into further.
- `InputValueError` now has a generic parameter - it's kind of weird but
it's necessary for ergonomics. It also improves error messages.
- The `ErrorExtensions` trait has been removed. I didn't think the
`extend` method was necessary since `From` impls exist. But the
ergonomics are still there with a new trait `ExtendError`, which
is implemented for both errors and results.
- `Response` now supports serializing multiple errors. This allows for
nice things like having multiple validation errors not be awkwardly
shoved into a single error.
- When an error occurs in execution, data is sent as `null`. This is
slightly more compliant with the spec but the algorithm described in
<https://spec.graphql.org/June2018/#sec-Errors-and-Non-Nullability> has
yet to be implemented.
2020-09-29 19:06:44 +00:00
|
|
|
async fn resolve(&self, ctx: &#crate_name::ContextSelectionSet<'_>, _field: &#crate_name::Positioned<#crate_name::parser::types::Field>) -> #crate_name::ServerResult<#crate_name::serde_json::Value> {
|
2020-09-29 23:45:48 +00:00
|
|
|
#crate_name::resolver_utils::resolve_container(ctx, self).await
|
2020-03-19 09:20:12 +00:00
|
|
|
}
|
|
|
|
}
|
2020-09-27 07:32:55 +00:00
|
|
|
|
2020-09-29 23:45:48 +00:00
|
|
|
impl #generics #crate_name::InterfaceType for #ident #generics {}
|
2020-03-06 15:58:43 +00:00
|
|
|
};
|
|
|
|
Ok(expanded.into())
|
|
|
|
}
|