Skip to content

Commit 5d92a6b

Browse files
committed
Add graphqlIsFunction configuration
1 parent 248f629 commit 5d92a6b

17 files changed

+1410
-3
lines changed

src/base/generator_utils.re

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ type output_config = {
2323
full_document: Graphql_ast.document,
2424
template_tag: (option(string), option(string), option(string)),
2525
template_tag_return_type: option(string),
26+
template_tag_is_function: option(bool),
2627
records: bool,
2728
inline: bool,
2829
future_added_value: bool,

src/base/ppx_config.re

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ type config = {
1919
template_tag_location: option(string),
2020
template_tag_import: option(string),
2121
template_tag_return_type: option(string),
22+
template_tag_is_function: option(bool),
2223
custom_fields: Hashtbl.t(string, string),
2324
future_added_value: bool,
2425
extend_query: option(string),
@@ -62,6 +63,9 @@ let template_tag_location = () =>
6263
let template_tag_return_type = () =>
6364
(config_ref^ |> Option.unsafe_unwrap).template_tag_return_type;
6465

66+
let template_tag_is_function = () =>
67+
(config_ref^ |> Option.unsafe_unwrap).template_tag_is_function;
68+
6569
let verbose_error_handling = () =>
6670
(config_ref^ |> Option.unsafe_unwrap).verbose_error_handling;
6771

src/base/result_decoder.re

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -817,6 +817,7 @@ type query_config = {
817817
template_tag_import: option(string),
818818
template_tag_return_type: option(string),
819819
tagged_template: option(bool),
820+
template_tag_is_function: option(bool),
820821
future_added_value: option(bool),
821822
extend: option(string),
822823
fragment_in_query: option(Ppx_config.fragment_in_query),
@@ -930,6 +931,10 @@ let config_arguments_to_config =
930931
...config,
931932
tagged_template: Some(value),
932933
}
934+
| ({item: "templateTagIsFunction"}, {item: Iv_boolean(value)}) => {
935+
...config,
936+
template_tag_is_function: Some(value),
937+
}
933938
| ({item: "futureAddedValue"}, {item: Iv_boolean(value)}) => {
934939
...config,
935940
future_added_value: Some(value),
@@ -1013,6 +1018,11 @@ let to_output_config =
10131018
query_config.template_tag_return_type,
10141019
Ppx_config.template_tag_return_type(),
10151020
),
1021+
template_tag_is_function:
1022+
get_with_default(
1023+
query_config.template_tag_is_function,
1024+
Ppx_config.template_tag_is_function(),
1025+
),
10161026
extend: query_config.extend,
10171027
fragment_in_query:
10181028
switch (query_config.fragment_in_query) {

src/base/result_decoder.rei

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ type query_config = {
88
template_tag_import: option(string),
99
template_tag_return_type: option(string),
1010
tagged_template: option(bool),
11+
template_tag_is_function: option(bool),
1112
future_added_value: option(bool),
1213
extend: option(string),
1314
fragment_in_query: option(Ppx_config.fragment_in_query),

src/bucklescript/bucklescript_config.re

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ let defaultConfig =
6363
template_tag_location: None,
6464
template_tag_import: None,
6565
template_tag_return_type: None,
66+
template_tag_is_function: None,
6667
custom_fields: Hashtbl.create(0),
6768
future_added_value: true,
6869
extend_query: None,
@@ -282,6 +283,14 @@ let read_config = () => {
282283
}
283284
);
284285
};
286+
let handleTemplateTagIsFunction = template_tag_is_function => {
287+
Ppx_config.update_config(current =>
288+
{
289+
...current,
290+
template_tag_is_function: Some(template_tag_is_function),
291+
}
292+
);
293+
};
285294

286295
let configBool = (key, value) =>
287296
ppxConfig |> JsonHelper.mapBool(key, value);
@@ -342,6 +351,7 @@ let read_config = () => {
342351
configString("templateTagLocation", handleTemplateTagLocation);
343352
configString("template-tag-return-type", handleTemplateTagReturnType);
344353
configString("templateTagReturnType", handleTemplateTagReturnType);
354+
configBool("template-tag-is-function", handleTemplateTagIsFunction);
345355

346356
ppxConfig |> read_custom_fields;
347357
};

src/bucklescript/graphql_ppx.re

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,8 @@ let get_query_config_from_trailing_record = fields =>
289289
extend: extract_extend_from_config(fields),
290290
fragment_in_query: extract_fragment_in_query_from_config(fields),
291291
apollo_mode: extract_apollo_mode_from_config(fields),
292+
// do not support these new options in the trailing record
293+
template_tag_is_function: None,
292294
};
293295
}
294296
);
@@ -304,6 +306,7 @@ let empty_query_config =
304306
template_tag_location: None,
305307
template_tag_import: None,
306308
template_tag_return_type: None,
309+
template_tag_is_function: None,
307310
future_added_value: None,
308311
extend: None,
309312
fragment_in_query: None,

src/bucklescript/output_bucklescript_module.re

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -280,8 +280,11 @@ let make_printed_query = (config, document) => {
280280
|> emit_json(config)
281281

282282
| Ppx_config.String =>
283-
switch (config.template_tag) {
284-
| (template_tag, location, import)
283+
switch (config.template_tag_is_function, config.template_tag) {
284+
| (Some(true), (_, location, _)) when location != None =>
285+
%expr
286+
graphql([%e emit_printed_query(source, config)])
287+
| (_, (template_tag, location, import))
285288
when template_tag != None || location != None =>
286289
open Graphql_printer;
287290
// the only way to emit a template literal for now, using the bs.raw
@@ -331,7 +334,7 @@ let make_printed_query = (config, document) => {
331334
config.template_tag_return_type,
332335
);
333336

334-
| (_, _, _) => emit_printed_query(source, config)
337+
| _ => emit_printed_query(source, config)
335338
}
336339
};
337340
};
@@ -645,6 +648,44 @@ function back to the original JSON compatible data */
645648
|> List.concat;
646649
};
647650

651+
let graphql_external = (config: output_config) => {
652+
switch (config) {
653+
| {
654+
template_tag: (import, Some(location), _),
655+
template_tag_return_type,
656+
template_tag_is_function: Some(true),
657+
} =>
658+
let return_type =
659+
switch (template_tag_return_type) {
660+
| None => "string"
661+
| Some(return_type) => return_type
662+
};
663+
let import =
664+
switch (import) {
665+
| None => "default"
666+
| Some(import) => import
667+
};
668+
[
669+
Ast_helper.(
670+
Str.primitive(
671+
Val.mk(
672+
~attrs=[
673+
Ast_helper.Attr.mk(
674+
{txt: "bs.module", loc: Location.none},
675+
PStr([Str.eval(const_str_expr(location))]),
676+
),
677+
],
678+
~prim=[import],
679+
{txt: "graphql", loc: Location.none},
680+
[%type: string => [%t base_type_name(return_type)]],
681+
),
682+
)
683+
),
684+
];
685+
| _ => []
686+
};
687+
};
688+
648689
let generate_operation_implementation =
649690
(config, variable_defs, _has_error, operation, res_structure) => {
650691
let parse_fn =
@@ -719,6 +760,7 @@ let generate_operation_implementation =
719760
],
720761
types,
721762
arg_types,
763+
graphql_external(config),
722764
[
723765
[%stri let query = [%e printed_query]],
724766
[%stri let parse: Raw.t => t = value => [%e parse_fn]],
@@ -1040,6 +1082,7 @@ let generate_fragment_implementation =
10401082
[[%stri [@ocaml.warning "-32"]]],
10411083
[wrap_module(~loc=Location.none, "Raw", raw_types)],
10421084
types,
1085+
graphql_external(config),
10431086
[
10441087
[%stri let query = [%e printed_query]],
10451088
[%stri let parse: Raw.t => [%t type_name] = value => [%e parse_fn]],

src/native/graphql_ppx.re

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ let empty_query_config =
9999
template_tag_location: None,
100100
template_tag_import: None,
101101
template_tag_return_type: None,
102+
template_tag_is_function: None,
102103
future_added_value: None,
103104
extend: None,
104105
fragment_in_query: None,
@@ -249,6 +250,7 @@ let () =
249250
template_tag_location: None,
250251
template_tag_import: None,
251252
template_tag_return_type: None,
253+
template_tag_is_function: None,
252254
custom_fields: Hashtbl.create(0),
253255
future_added_value: true,
254256
extend_query: None,

tests_bucklescript/__snapshots__/Compile_Apollo_taggedtemplate_re.b2c72bab.0.snapshot

Lines changed: 88 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)