Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit aa5d192

Browse files
fubhyleoyvens
authored andcommitted
fix: remove the _Schema_ type from the generated schema
1 parent 7a12542 commit aa5d192

File tree

1 file changed

+31
-45
lines changed

1 file changed

+31
-45
lines changed

graphql/src/schema/api.rs

Lines changed: 31 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use crate::schema::ast;
88

99
use graph::data::{
1010
graphql::ext::{DirectiveExt, DocumentExt, ValueExt},
11-
schema::{META_FIELD_NAME, META_FIELD_TYPE},
11+
schema::{META_FIELD_NAME, META_FIELD_TYPE, SCHEMA_TYPE_NAME},
1212
subgraph::SubgraphFeature,
1313
};
1414
use graph::prelude::s::{Value, *};
@@ -85,6 +85,16 @@ pub fn api_schema(
8585
add_field_arguments(&mut schema, &input_schema)?;
8686
add_query_type(&mut schema, &object_types, &interface_types, features)?;
8787
add_subscription_type(&mut schema, &object_types, &interface_types, features)?;
88+
89+
// Remove the `_Schema_` type from the generated schema.
90+
schema.definitions.retain(|d| match d {
91+
Definition::TypeDefinition(def @ TypeDefinition::Object(_)) => match def {
92+
TypeDefinition::Object(t) if t.name.eq(SCHEMA_TYPE_NAME) => false,
93+
_ => true,
94+
},
95+
_ => true,
96+
});
97+
8898
Ok(schema)
8999
}
90100

@@ -235,8 +245,10 @@ fn add_types_for_object_types(
235245
object_types: &Vec<&ObjectType>,
236246
) -> Result<(), APISchemaError> {
237247
for object_type in object_types {
238-
add_order_by_type(schema, &object_type.name, &object_type.fields)?;
239-
add_filter_type(schema, &object_type.name, &object_type.fields)?;
248+
if !object_type.name.eq(SCHEMA_TYPE_NAME) {
249+
add_order_by_type(schema, &object_type.name, &object_type.fields)?;
250+
add_filter_type(schema, &object_type.name, &object_type.fields)?;
251+
}
240252
}
241253
Ok(())
242254
}
@@ -296,15 +308,6 @@ fn add_filter_type(
296308
let filter_type_name = format!("{}_filter", type_name).to_string();
297309
match ast::get_named_type(schema, &filter_type_name) {
298310
None => {
299-
let input_values = field_input_values(schema, fields)?;
300-
301-
// Don't generate an input object with no fields, this makes the JS
302-
// graphql library, which graphiql uses, very confused and graphiql
303-
// is unable to load the schema. This happens for example with the
304-
// definition `interface Foo { x: OtherEntity }`.
305-
if input_values.is_empty() {
306-
return Ok(());
307-
}
308311
let typedef = TypeDefinition::InputObject(InputObjectType {
309312
position: Pos::default(),
310313
description: None,
@@ -516,8 +519,9 @@ fn add_query_type(
516519
let mut fields = object_types
517520
.iter()
518521
.map(|t| &t.name)
522+
.filter(|name| !name.eq(&SCHEMA_TYPE_NAME))
519523
.chain(interface_types.iter().map(|t| &t.name))
520-
.flat_map(|name| query_fields_for_type(schema, name, features))
524+
.flat_map(|name| query_fields_for_type(name, features))
521525
.collect::<Vec<Field>>();
522526
let mut fulltext_fields = schema
523527
.get_fulltext_directives()
@@ -617,8 +621,9 @@ fn add_subscription_type(
617621
let mut fields: Vec<Field> = object_types
618622
.iter()
619623
.map(|t| &t.name)
624+
.filter(|name| !name.eq(&SCHEMA_TYPE_NAME))
620625
.chain(interface_types.iter().map(|t| &t.name))
621-
.flat_map(|name| query_fields_for_type(schema, name, features))
626+
.flat_map(|name| query_fields_for_type(name, features))
622627
.collect();
623628
fields.push(meta_field());
624629

@@ -667,13 +672,8 @@ fn subgraph_error_argument() -> InputValue {
667672
}
668673

669674
/// Generates `Query` fields for the given type name (e.g. `users` and `user`).
670-
fn query_fields_for_type(
671-
schema: &Document,
672-
type_name: &str,
673-
features: &BTreeSet<SubgraphFeature>,
674-
) -> Vec<Field> {
675-
let input_objects = ast::get_input_object_definitions(schema);
676-
let mut collection_arguments = collection_arguments_for_named_type(&input_objects, type_name);
675+
fn query_fields_for_type(type_name: &str, features: &BTreeSet<SubgraphFeature>) -> Vec<Field> {
676+
let mut collection_arguments = collection_arguments_for_named_type(type_name);
677677
collection_arguments.push(block_argument());
678678

679679
let mut by_id_arguments = vec![
@@ -740,10 +740,7 @@ fn meta_field() -> Field {
740740
}
741741

742742
/// Generates arguments for collection queries of a named type (e.g. User).
743-
fn collection_arguments_for_named_type(
744-
input_objects: &[InputObjectType],
745-
type_name: &str,
746-
) -> Vec<InputValue> {
743+
fn collection_arguments_for_named_type(type_name: &str) -> Vec<InputValue> {
747744
// `first` and `skip` should be non-nullable, but the Apollo graphql client
748745
// exhibts non-conforming behaviour by erroing if no value is provided for a
749746
// non-nullable field, regardless of the presence of a default.
@@ -753,7 +750,7 @@ fn collection_arguments_for_named_type(
753750
let mut first = input_value(&"first".to_string(), "", Type::NamedType("Int".to_string()));
754751
first.default_value = Some(Value::Int(100.into()));
755752

756-
let mut args = vec![
753+
let args = vec![
757754
skip,
758755
first,
759756
input_value(
@@ -766,17 +763,12 @@ fn collection_arguments_for_named_type(
766763
"",
767764
Type::NamedType("OrderDirection".to_string()),
768765
),
769-
];
770-
771-
// Not all types have filter types, see comment in `add_filter_type`.
772-
let filter_name = format!("{}_filter", type_name);
773-
if input_objects.iter().any(|o| o.name == filter_name) {
774-
args.push(input_value(
766+
input_value(
775767
&"where".to_string(),
776768
"",
777-
Type::NamedType(filter_name),
778-
));
779-
}
769+
Type::NamedType(format!("{}_filter", type_name)),
770+
),
771+
];
780772

781773
args
782774
}
@@ -785,8 +777,6 @@ fn add_field_arguments(
785777
schema: &mut Document,
786778
input_schema: &Document,
787779
) -> Result<(), APISchemaError> {
788-
let input_objects = ast::get_input_object_definitions(schema);
789-
790780
// Refactor: Remove the `input_schema` argument and do a mutable iteration
791781
// over the definitions in `schema`. Also the duplication between this and
792782
// the loop for interfaces below.
@@ -807,12 +797,10 @@ fn add_field_arguments(
807797

808798
match input_reference_type {
809799
TypeDefinition::Object(ot) => {
810-
field.arguments =
811-
collection_arguments_for_named_type(&input_objects, &ot.name);
800+
field.arguments = collection_arguments_for_named_type(&ot.name);
812801
}
813802
TypeDefinition::Interface(it) => {
814-
field.arguments =
815-
collection_arguments_for_named_type(&input_objects, &it.name);
803+
field.arguments = collection_arguments_for_named_type(&it.name);
816804
}
817805
_ => unreachable!(
818806
"referenced entity types can only be object or interface types"
@@ -841,12 +829,10 @@ fn add_field_arguments(
841829

842830
match input_reference_type {
843831
TypeDefinition::Object(ot) => {
844-
field.arguments =
845-
collection_arguments_for_named_type(&input_objects, &ot.name);
832+
field.arguments = collection_arguments_for_named_type(&ot.name);
846833
}
847834
TypeDefinition::Interface(it) => {
848-
field.arguments =
849-
collection_arguments_for_named_type(&input_objects, &it.name);
835+
field.arguments = collection_arguments_for_named_type(&it.name);
850836
}
851837
_ => unreachable!(
852838
"referenced entity types can only be object or interface types"

0 commit comments

Comments
 (0)