Skip to content

Commit c682a42

Browse files
improve(selection): emit Option when using @include or @Skip
1 parent 46791c8 commit c682a42

File tree

9 files changed

+31
-12
lines changed

9 files changed

+31
-12
lines changed

examples/github/examples/github.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ fn main() -> Result<(), anyhow::Error> {
4545
let variables = repo_view::Variables {
4646
owner: owner.to_string(),
4747
name: name.to_string(),
48+
with_issues: true
4849
};
4950

5051
let client = Client::builder()
@@ -81,6 +82,7 @@ fn main() -> Result<(), anyhow::Error> {
8182
.repository
8283
.expect("missing repository")
8384
.issues
85+
.unwrap()
8486
.nodes
8587
.expect("issue nodes is null")
8688
.iter()

examples/github/examples/query_1.graphql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
query RepoView($owner: String!, $name: String!) {
1+
query RepoView($owner: String!, $name: String!, $withIssues: Boolean!) {
22
repository(owner: $owner, name: $name) {
33
homepageUrl
44
stargazers {
55
totalCount
66
}
7-
issues(first: 20, states: OPEN) {
7+
issues(first: 20, states: OPEN) @include(if: $withIssues) {
88
nodes {
99
title
1010
comments {

graphql_client_codegen/src/codegen.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -199,10 +199,10 @@ fn render_variable_field_type(
199199
let safe_name = shared::keyword_replace(normalized_name.clone());
200200
let full_name = Ident::new(safe_name.as_ref(), Span::call_site());
201201

202-
decorate_type(&full_name, &variable.r#type.qualifiers)
202+
decorate_type(&full_name, &variable.r#type.qualifiers, false)
203203
}
204204

205-
fn decorate_type(ident: &Ident, qualifiers: &[GraphqlTypeQualifier]) -> TokenStream {
205+
fn decorate_type(ident: &Ident, qualifiers: &[GraphqlTypeQualifier], skip_or_include: bool) -> TokenStream {
206206
let mut qualified = quote!(#ident);
207207

208208
let mut non_null = false;
@@ -233,7 +233,8 @@ fn decorate_type(ident: &Ident, qualifiers: &[GraphqlTypeQualifier]) -> TokenStr
233233

234234
// If we are in nullable context at the end of the iteration, we wrap the whole
235235
// type with an Option.
236-
if !non_null {
236+
// This can also happen if the field has a @skip or @include directive
237+
if !non_null || skip_or_include {
237238
qualified = quote!(Option<#qualified>);
238239
}
239240

graphql_client_codegen/src/codegen/inputs.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ fn generate_struct(
7474
None
7575
};
7676
let type_name = Ident::new(normalized_field_type_name.as_ref(), Span::call_site());
77-
let field_type_tokens = super::decorate_type(&type_name, &field_type.qualifiers);
77+
let field_type_tokens = super::decorate_type(&type_name, &field_type.qualifiers, false);
7878
let field_type = if field_type
7979
.id
8080
.as_input_id()
@@ -127,7 +127,7 @@ fn generate_enum(
127127
let mut qualifiers = vec![GraphqlTypeQualifier::Required];
128128
qualifiers.extend(field_type.qualifiers.iter().cloned());
129129

130-
let field_type_tokens = super::decorate_type(&type_name, &qualifiers);
130+
let field_type_tokens = super::decorate_type(&type_name, &qualifiers, false);
131131
let field_type = if field_type
132132
.id
133133
.as_input_id()

graphql_client_codegen/src/codegen/selection.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ fn calculate_custom_response_type_selection<'a>(
8282
flatten: false,
8383
boxed: false,
8484
deprecation: field.deprecation(),
85+
skip_or_include: false
8586
});
8687

8788
let struct_id = context.push_type(ExpandedType {
@@ -285,6 +286,8 @@ fn calculate_selection<'a>(
285286
struct_id,
286287
deprecation: None,
287288
boxed: fragment_is_recursive(*fragment_id, context.query.query),
289+
// TODO
290+
skip_or_include: false
288291
}),
289292
}
290293
}
@@ -331,6 +334,7 @@ fn calculate_selection<'a>(
331334
flatten: false,
332335
deprecation: schema_field.deprecation(),
333336
boxed: false,
337+
skip_or_include: field.skip_or_include
334338
});
335339
}
336340
TypeId::Scalar(scalar) => {
@@ -348,6 +352,7 @@ fn calculate_selection<'a>(
348352
flatten: false,
349353
deprecation: schema_field.deprecation(),
350354
boxed: false,
355+
skip_or_include: field.skip_or_include
351356
});
352357
}
353358
TypeId::Object(_) | TypeId::Interface(_) | TypeId::Union(_) => {
@@ -362,6 +367,7 @@ fn calculate_selection<'a>(
362367
flatten: false,
363368
boxed: false,
364369
deprecation: schema_field.deprecation(),
370+
skip_or_include: field.skip_or_include
365371
});
366372

367373
let type_id = context.push_type(ExpandedType {
@@ -407,6 +413,7 @@ fn calculate_selection<'a>(
407413
flatten: true,
408414
deprecation: None,
409415
boxed: fragment_is_recursive(*fragment_id, context.query.query),
416+
skip_or_include: false
410417
});
411418

412419
// We stop here, because the structs for the fragments are generated separately, to
@@ -434,6 +441,7 @@ struct ExpandedField<'a> {
434441
flatten: bool,
435442
deprecation: Option<Option<&'a str>>,
436443
boxed: bool,
444+
skip_or_include: bool
437445
}
438446

439447
impl ExpandedField<'_> {
@@ -442,6 +450,7 @@ impl ExpandedField<'_> {
442450
let qualified_type = decorate_type(
443451
&Ident::new(&self.field_type, Span::call_site()),
444452
self.field_type_qualifiers,
453+
self.skip_or_include
445454
);
446455

447456
let qualified_type = if self.boxed {

graphql_client_codegen/src/query.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,11 +318,17 @@ where
318318
))
319319
})?;
320320

321+
let has_skip_or_include = field
322+
.directives
323+
.iter()
324+
.any(|directive| ["skip", "include"].contains(&directive.name.as_ref()));
325+
321326
let id = query.push_selection(
322327
Selection::Field(SelectedField {
323328
alias: field.alias.as_ref().map(|alias| alias.as_ref().into()),
324329
field_id,
325330
selection_set: Vec::with_capacity(selection_set.items.len()),
331+
skip_or_include: has_skip_or_include
326332
}),
327333
parent,
328334
);

graphql_client_codegen/src/query/selection.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,7 @@ pub(crate) struct SelectedField {
257257
pub(crate) alias: Option<String>,
258258
pub(crate) field_id: StoredFieldId,
259259
pub(crate) selection_set: Vec<SelectionId>,
260+
pub(crate) skip_or_include: bool
260261
}
261262

262263
impl SelectedField {

graphql_client_codegen/src/schema/graphql_parser_conversion.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ fn ingest_object<'doc, T>(
184184
name: field.name.as_ref().into(),
185185
r#type: resolve_field_type(schema, &field.field_type),
186186
parent: super::StoredFieldParent::Object(object_id),
187-
deprecation: find_deprecation(&field.directives),
187+
deprecation: find_deprecation(&field.directives)
188188
};
189189

190190
field_ids.push(schema.push_field(field));
@@ -221,7 +221,7 @@ fn ingest_object_type_extension<'doc, T>(
221221
name: field.name.as_ref().into(),
222222
r#type: resolve_field_type(schema, &field.field_type),
223223
parent: super::StoredFieldParent::Object(object_id),
224-
deprecation: find_deprecation(&field.directives),
224+
deprecation: find_deprecation(&field.directives)
225225
};
226226

227227
field_ids.push(schema.push_field(field));
@@ -290,7 +290,7 @@ fn ingest_interface<'doc, T>(
290290
name: field.name.as_ref().into(),
291291
r#type: resolve_field_type(schema, &field.field_type),
292292
parent: super::StoredFieldParent::Interface(interface_id),
293-
deprecation: find_deprecation(&field.directives),
293+
deprecation: find_deprecation(&field.directives)
294294
};
295295

296296
field_ids.push(schema.push_field(field));

graphql_client_codegen/src/schema/json_conversion.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ fn ingest_interface(schema: &mut Schema, iface: &mut FullType) {
183183
Some(field.deprecation_reason.clone())
184184
} else {
185185
None
186-
},
186+
}
187187
};
188188

189189
field_ids.push(schema.push_field(field));
@@ -218,7 +218,7 @@ fn ingest_object(schema: &mut Schema, object: &mut FullType) {
218218
Some(field.deprecation_reason.clone())
219219
} else {
220220
None
221-
},
221+
}
222222
};
223223

224224
field_ids.push(schema.push_field(field));

0 commit comments

Comments
 (0)