@@ -39,6 +39,115 @@ use super::content_section::GenericSection;
39
39
use crate :: config:: Config ;
40
40
use crate :: config:: ProjectConfig ;
41
41
42
+ pub fn generate_preloadable_query_parameters (
43
+ config : & Config ,
44
+ project_config : & ProjectConfig ,
45
+ printer : & mut Printer < ' _ > ,
46
+ schema : & SDLSchema ,
47
+ normalization_operation : & OperationDefinition ,
48
+ query_id : & QueryID ,
49
+ ) -> Result < Vec < u8 > , FmtError > {
50
+ let mut request_parameters = build_request_params ( normalization_operation) ;
51
+ let cloned_query_id = Some ( query_id. clone ( ) ) ;
52
+ request_parameters. id = & cloned_query_id;
53
+
54
+ let mut content_sections = ContentSections :: default ( ) ;
55
+
56
+ // -- Begin Docblock Section --
57
+ let extra_annotations = match query_id {
58
+ QueryID :: Persisted { text_hash, .. } => vec ! [ format!( "@relayHash {}" , text_hash) ] ,
59
+ _ => vec ! [ ] ,
60
+ } ;
61
+ content_sections. push ( ContentSection :: Docblock ( generate_docblock_section (
62
+ config,
63
+ project_config,
64
+ extra_annotations,
65
+ ) ?) ) ;
66
+ // -- End Docblock Section --
67
+
68
+ // -- Begin Disable Lint Section --
69
+ content_sections. push ( ContentSection :: Generic ( generate_disable_lint_section (
70
+ & project_config. typegen_config . language ,
71
+ ) ?) ) ;
72
+ // -- End Disable Lint Section --
73
+
74
+ // -- Begin Use Strict Section --
75
+ content_sections. push ( ContentSection :: Generic ( generate_use_strict_section (
76
+ & project_config. typegen_config . language ,
77
+ ) ?) ) ;
78
+ // -- End Use Strict Section --
79
+
80
+ // -- Begin Metadata Annotations Section --
81
+ let mut section = CommentAnnotationsSection :: default ( ) ;
82
+ if let Some ( QueryID :: Persisted { id, .. } ) = & request_parameters. id {
83
+ writeln ! ( section, "@relayRequestID {}" , id) ?;
84
+ }
85
+ content_sections. push ( ContentSection :: CommentAnnotations ( section) ) ;
86
+ // -- End Metadata Annotations Section --
87
+
88
+ // -- Begin Types Section --
89
+ let mut section = GenericSection :: default ( ) ;
90
+ if project_config. typegen_config . language == TypegenLanguage :: Flow {
91
+ writeln ! ( section, "/*::" ) ?;
92
+ }
93
+
94
+ write_import_type_from (
95
+ project_config,
96
+ & mut section,
97
+ "PreloadableConcreteRequest" ,
98
+ "relay-runtime" ,
99
+ ) ?;
100
+ write_import_type_from (
101
+ project_config,
102
+ & mut section,
103
+ & normalization_operation. name . item . 0 . to_string ( ) ,
104
+ & format ! ( "./{}.graphql" , normalization_operation. name. item. 0 ) ,
105
+ ) ?;
106
+
107
+ if project_config. typegen_config . language == TypegenLanguage :: Flow {
108
+ writeln ! ( section, "*/" ) ?;
109
+ }
110
+ content_sections. push ( ContentSection :: Generic ( section) ) ;
111
+ // -- End Types Section --
112
+
113
+ // -- Begin Query Node Section --
114
+ let preloadable_request = printer. print_preloadable_request (
115
+ schema,
116
+ request_parameters,
117
+ normalization_operation,
118
+ & mut Default :: default ( ) ,
119
+ ) ;
120
+ let mut section = GenericSection :: default ( ) ;
121
+
122
+ let node_type = format ! (
123
+ "PreloadableConcreteRequest<{}>" ,
124
+ normalization_operation. name. item. 0
125
+ ) ;
126
+
127
+ write_variable_value_with_type (
128
+ & project_config. typegen_config . language ,
129
+ & mut section,
130
+ "node" ,
131
+ & node_type,
132
+ & preloadable_request,
133
+ ) ?;
134
+ content_sections. push ( ContentSection :: Generic ( section) ) ;
135
+ // -- End Query Node Section --
136
+
137
+ // -- Begin Export Section --
138
+ let mut section = GenericSection :: default ( ) ;
139
+ write_export_generated_node (
140
+ & project_config. typegen_config ,
141
+ & mut section,
142
+ "node" ,
143
+ Some ( node_type) ,
144
+ ) ?;
145
+ content_sections. push ( ContentSection :: Generic ( section) ) ;
146
+ // -- End Export Section --
147
+
148
+ content_sections. into_signed_bytes ( )
149
+ }
150
+
42
151
#[ allow( clippy:: too_many_arguments) ]
43
152
pub fn generate_updatable_query (
44
153
config : & Config ,
@@ -200,14 +309,14 @@ pub fn generate_operation(
200
309
let mut content_sections = ContentSections :: default ( ) ;
201
310
202
311
// -- Begin Docblock Section --
203
- let v = match id_and_text_hash {
312
+ let extra_annotations = match id_and_text_hash {
204
313
Some ( QueryID :: Persisted { text_hash, .. } ) => vec ! [ format!( "@relayHash {}" , text_hash) ] ,
205
314
_ => vec ! [ ] ,
206
315
} ;
207
316
content_sections. push ( ContentSection :: Docblock ( generate_docblock_section (
208
317
config,
209
318
project_config,
210
- v ,
319
+ extra_annotations ,
211
320
) ?) ) ;
212
321
// -- End Docblock Section --
213
322
@@ -328,26 +437,38 @@ pub fn generate_operation(
328
437
if is_operation_preloadable ( normalization_operation) && id_and_text_hash. is_some ( ) {
329
438
match project_config. typegen_config . language {
330
439
TypegenLanguage :: Flow => {
331
- writeln ! (
332
- section,
333
- "require('relay-runtime').PreloadableQueryRegistry.set((node.params/*: any*/).id, node);" ,
334
- ) ?;
440
+ if project_config. typegen_config . eager_es_modules {
441
+ writeln ! (
442
+ section,
443
+ "import {{ PreloadableQueryRegistry }} from 'relay-runtime';" ,
444
+ ) ?;
445
+ writeln ! (
446
+ section,
447
+ "PreloadableQueryRegistry.set((node.params/*: any*/).id, node);" ,
448
+ ) ?;
449
+ } else {
450
+ writeln ! (
451
+ section,
452
+ "require('relay-runtime').PreloadableQueryRegistry.set((node.params/*: any*/).id, node);" ,
453
+ ) ?;
454
+ }
335
455
}
336
- TypegenLanguage :: JavaScript => {
337
- writeln ! (
338
- section,
339
- "require('relay-runtime').PreloadableQueryRegistry.set(node.params.id, node);" ,
340
- ) ?;
341
- }
342
- TypegenLanguage :: TypeScript => {
343
- writeln ! (
344
- section,
345
- "import {{ PreloadableQueryRegistry }} from 'relay-runtime';" ,
346
- ) ?;
347
- writeln ! (
348
- section,
349
- "PreloadableQueryRegistry.set(node.params.id, node);" ,
350
- ) ?;
456
+ TypegenLanguage :: JavaScript | TypegenLanguage :: TypeScript => {
457
+ if project_config. typegen_config . eager_es_modules {
458
+ writeln ! (
459
+ section,
460
+ "import {{ PreloadableQueryRegistry }} from 'relay-runtime';" ,
461
+ ) ?;
462
+ writeln ! (
463
+ section,
464
+ "PreloadableQueryRegistry.set(node.params.id, node);" ,
465
+ ) ?;
466
+ } else {
467
+ writeln ! (
468
+ section,
469
+ "require('relay-runtime').PreloadableQueryRegistry.set(node.params.id, node);" ,
470
+ ) ?;
471
+ }
351
472
}
352
473
}
353
474
}
0 commit comments