2020-05-02 04:24:01 +02:00
|
|
|
|
//!
|
|
|
|
|
|
|
|
|
|
use crate::util::duplicate::Duplicate;
|
|
|
|
|
use proc_macro2::Span;
|
|
|
|
|
use proc_macro_error::{Diagnostic, Level};
|
|
|
|
|
use std::fmt;
|
|
|
|
|
|
2020-06-04 11:19:01 +03:00
|
|
|
|
/// URL of the GraphQL specification (June 2018 Edition).
|
|
|
|
|
pub const SPEC_URL: &'static str = "https://spec.graphql.org/June2018/";
|
2020-05-02 04:24:01 +02:00
|
|
|
|
|
|
|
|
|
#[allow(unused_variables)]
|
|
|
|
|
pub enum GraphQLScope {
|
2020-06-04 11:19:01 +03:00
|
|
|
|
UnionAttr,
|
2020-05-02 04:24:01 +02:00
|
|
|
|
DeriveObject,
|
|
|
|
|
DeriveInputObject,
|
2020-06-04 11:19:01 +03:00
|
|
|
|
UnionDerive,
|
2020-05-02 04:24:01 +02:00
|
|
|
|
DeriveEnum,
|
|
|
|
|
DeriveScalar,
|
|
|
|
|
ImplScalar,
|
|
|
|
|
ImplObject,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl GraphQLScope {
|
2020-06-04 11:19:01 +03:00
|
|
|
|
pub fn spec_section(&self) -> &str {
|
2020-05-02 04:24:01 +02:00
|
|
|
|
match self {
|
2020-06-04 11:19:01 +03:00
|
|
|
|
Self::DeriveObject | Self::ImplObject => "#sec-Objects",
|
|
|
|
|
Self::DeriveInputObject => "#sec-Input-Objects",
|
|
|
|
|
Self::UnionAttr | Self::UnionDerive => "#sec-Unions",
|
|
|
|
|
Self::DeriveEnum => "#sec-Enums",
|
|
|
|
|
Self::DeriveScalar | Self::ImplScalar => "#sec-Scalars",
|
2020-05-02 04:24:01 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl fmt::Display for GraphQLScope {
|
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
|
let name = match self {
|
2020-06-04 11:19:01 +03:00
|
|
|
|
Self::DeriveObject | Self::ImplObject => "object",
|
|
|
|
|
Self::DeriveInputObject => "input object",
|
|
|
|
|
Self::UnionAttr | Self::UnionDerive => "union",
|
|
|
|
|
Self::DeriveEnum => "enum",
|
|
|
|
|
Self::DeriveScalar | Self::ImplScalar => "scalar",
|
2020-05-02 04:24:01 +02:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
write!(f, "GraphQL {}", name)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[allow(unused_variables)]
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
|
pub enum UnsupportedAttribute {
|
|
|
|
|
Skip,
|
|
|
|
|
Interface,
|
|
|
|
|
Scalar,
|
|
|
|
|
Deprecation,
|
|
|
|
|
Default,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl GraphQLScope {
|
2020-06-04 11:19:01 +03:00
|
|
|
|
fn spec_link(&self) -> String {
|
|
|
|
|
format!("{}{}", SPEC_URL, self.spec_section())
|
2020-05-02 04:24:01 +02:00
|
|
|
|
}
|
|
|
|
|
|
2020-06-04 11:19:01 +03:00
|
|
|
|
pub fn custom<S: AsRef<str>>(&self, span: Span, msg: S) -> Diagnostic {
|
2020-05-02 04:24:01 +02:00
|
|
|
|
Diagnostic::spanned(span, Level::Error, format!("{} {}", self, msg.as_ref()))
|
2020-06-04 11:19:01 +03:00
|
|
|
|
.note(self.spec_link())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn emit_custom<S: AsRef<str>>(&self, span: Span, msg: S) {
|
|
|
|
|
self.custom(span, msg).emit()
|
2020-05-02 04:24:01 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn custom_error<S: AsRef<str>>(&self, span: Span, msg: S) -> syn::Error {
|
|
|
|
|
syn::Error::new(span, format!("{} {}", self, msg.as_ref()))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn unsupported_attribute(&self, attribute: Span, kind: UnsupportedAttribute) {
|
|
|
|
|
Diagnostic::spanned(
|
|
|
|
|
attribute,
|
|
|
|
|
Level::Error,
|
|
|
|
|
format!("attribute `{:?}` can not be used at the top level of {}", kind, self),
|
|
|
|
|
)
|
|
|
|
|
.note("The macro is known to Juniper. However, not all valid #[graphql] attributes are available for each macro".to_string())
|
|
|
|
|
.emit();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn unsupported_attribute_within(&self, attribute: Span, kind: UnsupportedAttribute) {
|
|
|
|
|
Diagnostic::spanned(
|
|
|
|
|
attribute,
|
|
|
|
|
Level::Error,
|
|
|
|
|
format!("attribute `{:?}` can not be used inside of {}", kind, self),
|
|
|
|
|
)
|
|
|
|
|
.note("The macro is known to Juniper. However, not all valid #[graphql] attributes are available for each macro".to_string())
|
|
|
|
|
.emit();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn not_empty(&self, container: Span) {
|
|
|
|
|
Diagnostic::spanned(
|
|
|
|
|
container,
|
|
|
|
|
Level::Error,
|
|
|
|
|
format!("{} expects at least one field", self),
|
|
|
|
|
)
|
2020-06-04 11:19:01 +03:00
|
|
|
|
.note(self.spec_link())
|
2020-05-02 04:24:01 +02:00
|
|
|
|
.emit();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn duplicate<'a, T: syn::spanned::Spanned + 'a>(
|
|
|
|
|
&self,
|
|
|
|
|
duplicates: impl IntoIterator<Item = &'a Duplicate<T>>,
|
|
|
|
|
) {
|
|
|
|
|
duplicates
|
|
|
|
|
.into_iter()
|
|
|
|
|
.for_each(|dup| {
|
|
|
|
|
(&dup.spanned[1..])
|
|
|
|
|
.iter()
|
|
|
|
|
.for_each(|spanned| {
|
|
|
|
|
Diagnostic::spanned(
|
|
|
|
|
spanned.span(),
|
|
|
|
|
Level::Error,
|
|
|
|
|
format!(
|
|
|
|
|
"{} does not allow fields with the same name",
|
|
|
|
|
self
|
|
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
.help(format!("There is at least one other field with the same name `{}`, possibly renamed via the #[graphql] attribute", dup.name))
|
2020-06-04 11:19:01 +03:00
|
|
|
|
.note(self.spec_link())
|
2020-05-02 04:24:01 +02:00
|
|
|
|
.emit();
|
|
|
|
|
});
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn no_double_underscore(&self, field: Span) {
|
|
|
|
|
Diagnostic::spanned(
|
|
|
|
|
field,
|
|
|
|
|
Level::Error,
|
2020-06-04 11:19:01 +03:00
|
|
|
|
"All types and directives defined within a schema must not have a name which begins \
|
|
|
|
|
with `__` (two underscores), as this is used exclusively by GraphQL’s introspection \
|
|
|
|
|
system."
|
|
|
|
|
.into(),
|
2020-05-02 04:24:01 +02:00
|
|
|
|
)
|
2020-06-04 11:19:01 +03:00
|
|
|
|
.note(format!("{}#sec-Schema", SPEC_URL))
|
|
|
|
|
.emit();
|
2020-05-02 04:24:01 +02:00
|
|
|
|
}
|
|
|
|
|
}
|