Add `SchemaBuilder::enable_suggestions` method to enable or disable field suggestions. #1101
This commit is contained in:
parent
13a66a5013
commit
e50808888a
|
@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
||||||
# [4.0.15] 2022-10-01
|
# [4.0.15] 2022-10-01
|
||||||
|
|
||||||
- Skip tracing for introspection queries. [#841](https://github.com/async-graphql/async-graphql/issues/841)
|
- Skip tracing for introspection queries. [#841](https://github.com/async-graphql/async-graphql/issues/841)
|
||||||
|
- Add `SchemaBuilder::enable_suggestions` method to enable or disable field suggestions. [#1101](https://github.com/async-graphql/async-graphql/issues/1101)
|
||||||
|
|
||||||
# [4.0.14] 2022-09-25
|
# [4.0.14] 2022-09-25
|
||||||
|
|
||||||
|
|
|
@ -601,6 +601,7 @@ pub struct Registry {
|
||||||
pub enable_federation: bool,
|
pub enable_federation: bool,
|
||||||
pub federation_subscription: bool,
|
pub federation_subscription: bool,
|
||||||
pub ignore_name_conflicts: HashSet<String>,
|
pub ignore_name_conflicts: HashSet<String>,
|
||||||
|
pub enable_suggestions: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Registry {
|
impl Registry {
|
||||||
|
|
|
@ -219,6 +219,13 @@ impl<Query, Mutation, Subscription> SchemaBuilder<Query, Mutation, Subscription>
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Enable field suggestions, default is `true`.
|
||||||
|
#[must_use]
|
||||||
|
pub fn enable_suggestions(mut self, enable: bool) -> Self {
|
||||||
|
self.registry.enable_suggestions = enable;
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
/// Build schema.
|
/// Build schema.
|
||||||
pub fn finish(mut self) -> Schema<Query, Mutation, Subscription> {
|
pub fn finish(mut self) -> Schema<Query, Mutation, Subscription> {
|
||||||
// federation
|
// federation
|
||||||
|
@ -381,6 +388,7 @@ where
|
||||||
enable_federation: false,
|
enable_federation: false,
|
||||||
federation_subscription: false,
|
federation_subscription: false,
|
||||||
ignore_name_conflicts,
|
ignore_name_conflicts,
|
||||||
|
enable_suggestions: true,
|
||||||
};
|
};
|
||||||
|
|
||||||
registry.add_directive(MetaDirective {
|
registry.add_directive(MetaDirective {
|
||||||
|
|
|
@ -38,17 +38,21 @@ impl<'a> Visitor<'a> for FieldsOnCorrectType {
|
||||||
"Unknown field \"{}\" on type \"{}\".{}",
|
"Unknown field \"{}\" on type \"{}\".{}",
|
||||||
field.node.name,
|
field.node.name,
|
||||||
parent_type.name(),
|
parent_type.name(),
|
||||||
make_suggestion(
|
if ctx.registry.enable_suggestions {
|
||||||
" Did you mean",
|
make_suggestion(
|
||||||
parent_type
|
" Did you mean",
|
||||||
.fields()
|
parent_type
|
||||||
.iter()
|
.fields()
|
||||||
.map(|fields| fields.keys())
|
.iter()
|
||||||
.flatten()
|
.map(|fields| fields.keys())
|
||||||
.map(String::as_str),
|
.flatten()
|
||||||
&field.node.name.node,
|
.map(String::as_str),
|
||||||
)
|
&field.node.name.node,
|
||||||
.unwrap_or_default()
|
)
|
||||||
|
.unwrap_or_default()
|
||||||
|
} else {
|
||||||
|
String::new()
|
||||||
|
}
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
|
@ -79,7 +79,11 @@ impl<'a> Visitor<'a> for KnownArgumentNames<'a> {
|
||||||
name,
|
name,
|
||||||
field_name,
|
field_name,
|
||||||
type_name,
|
type_name,
|
||||||
self.get_suggestion(name.node.as_str())
|
if ctx.registry.enable_suggestions {
|
||||||
|
self.get_suggestion(name.node.as_str())
|
||||||
|
} else {
|
||||||
|
String::new()
|
||||||
|
}
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue