Skip to content

Commit d4c070a

Browse files
authored
Merge pull request #214 from upsuper/default-root-type
Use default root operation type names when not specified
2 parents 4a09154 + b312a49 commit d4c070a

File tree

2 files changed

+8
-15
lines changed

2 files changed

+8
-15
lines changed

graphql_client_codegen/src/codegen.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -69,20 +69,16 @@ pub(crate) fn response_for_query(
6969
}
7070

7171
let response_data_fields = {
72-
let opt_root_name = operation.root_name(&context.schema);
73-
let root_name: &str = if let Some(root_name) = opt_root_name {
74-
root_name
72+
let root_name = operation.root_name(&context.schema);
73+
let opt_definition = context.schema.objects.get(&root_name);
74+
let definition = if let Some(definition) = opt_definition {
75+
definition
7576
} else {
7677
panic!(
7778
"operation type '{:?}' not in schema",
7879
operation.operation_type
7980
);
8081
};
81-
let definition = context
82-
.schema
83-
.objects
84-
.get(&root_name)
85-
.expect("schema declaration is invalid");
8682
let prefix = &operation.name;
8783
let selection = &operation.selection;
8884

graphql_client_codegen/src/operations.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,11 @@ pub struct Operation<'query> {
2323
}
2424

2525
impl<'query> Operation<'query> {
26-
pub(crate) fn root_name<'schema>(
27-
&self,
28-
schema: &'schema ::schema::Schema,
29-
) -> Option<&'schema str> {
26+
pub(crate) fn root_name<'schema>(&self, schema: &'schema ::schema::Schema) -> &'schema str {
3027
match self.operation_type {
31-
OperationType::Query => schema.query_type,
32-
OperationType::Mutation => schema.mutation_type,
33-
OperationType::Subscription => schema.subscription_type,
28+
OperationType::Query => schema.query_type.unwrap_or("Query"),
29+
OperationType::Mutation => schema.mutation_type.unwrap_or("Mutation"),
30+
OperationType::Subscription => schema.subscription_type.unwrap_or("Subscription"),
3431
}
3532
}
3633

0 commit comments

Comments
 (0)