From aba9453f2848f378346343fe53c381a0336005b0 Mon Sep 17 00:00:00 2001 From: Sean Grove Date: Sun, 24 Nov 2019 20:42:33 -0800 Subject: [PATCH 1/2] First pass at unified definition --- .../output_bucklescript_module.re | 5 + .../output_bucklescript_unifier_2.re | 101 ++++++++++++++++++ 2 files changed, 106 insertions(+) create mode 100644 src/bucklescript/output_bucklescript_unifier_2.re diff --git a/src/bucklescript/output_bucklescript_module.re b/src/bucklescript/output_bucklescript_module.re index 7b8a5054..695c3bdc 100644 --- a/src/bucklescript/output_bucklescript_module.re +++ b/src/bucklescript/output_bucklescript_module.re @@ -180,6 +180,10 @@ let generate_default_operation = ); let (make_fn, make_with_variables_fn, make_variables_fn) = Output_bucklescript_unifier.make_make_fun(config, variable_defs); + + let definition = + Output_bucklescript_unifier_2.make_definition(config, variable_defs); + List.concat([ make_printed_query(config, [Graphql_ast.Operation(operation)]), List.concat([ @@ -205,6 +209,7 @@ let generate_default_operation = [%stri let make = [%e make_fn]], [%stri let makeWithVariables = [%e make_with_variables_fn]], [%stri let makeVariables = [%e make_variables_fn]], + [%stri let definition = [%e definition]], ], ]), ret_type_magic, diff --git a/src/bucklescript/output_bucklescript_unifier_2.re b/src/bucklescript/output_bucklescript_unifier_2.re new file mode 100644 index 00000000..533ef68e --- /dev/null +++ b/src/bucklescript/output_bucklescript_unifier_2.re @@ -0,0 +1,101 @@ +open Graphql_ppx_base; +open Graphql_ast; +open Source_pos; +open Generator_utils; + +open Ast_406; +open Parsetree; +open Asttypes; + +open Type_utils; +open Output_bucklescript_utils; + +exception Unimplemented(string); + +let make_definition = (config, variable_defs) => { + let make_tuple = (loc, variables, compose) => [%expr + ( + parse, + ppx_printed_query, + graphql_ppx_use_json_variables_fn => [%e compose], + ) + ]; + + switch (variable_defs) { + | Some({item, span}) => + let rec make_labelled_function = (defs, body) => + switch (defs) { + | [] => + [@metaloc config.map_loc(span) |> conv_loc] [%expr (() => [%e body])] + | [(name, def), ...tl] => + let name_loc = config.map_loc(name.span) |> conv_loc; + Ast_helper.( + Exp.fun_( + ~loc=name_loc, + switch (def.vd_type.item) { + | Tr_non_null_list(_) + | Tr_non_null_named(_) => Labelled(name.item) + | Tr_list(_) + | Tr_named(_) => Optional(name.item) + }, + None, + Pat.var(~loc=name_loc, {txt: name.item, loc: name_loc}), + make_labelled_function(tl, body), + ) + ); + }; + + let make_var_ctor = defs => + defs + |> List.map(((name, def)) => { + let parser_ = + Output_bucklescript_encoder.parser_for_type( + config.schema, + config.map_loc(name.span), + to_native_type_ref(to_schema_type_ref(def.vd_type.item)), + ); + let loc = config.map_loc(name.span) |> conv_loc; + [@metaloc loc] + [%expr + ( + [%e + Ast_helper.Exp.constant( + ~loc, + Pconst_string(name.item, None), + ) + ], + [%e parser_]( + [%e + Ast_helper.Exp.ident( + ~loc, + {txt: Longident.parse(name.item), loc}, + ) + ], + ), + ) + ]; + }) + |> Ast_helper.Exp.array; + let loc = config.map_loc(span) |> conv_loc; + let variable_ctor_body = + [@metaloc loc] + [%expr + Js.Json.object_( + [%e make_var_ctor(item)] + |> [%e Output_bucklescript_encoder.filter_out_null_values] + |> Js.Dict.fromArray, + ) + ]; + + let user_function = + make_labelled_function( + item, + [%expr graphql_ppx_use_json_variables_fn([%e variable_ctor_body])], + ); + + make_tuple(loc, variable_ctor_body, user_function); + | None => + %expr + (() => [%e make_tuple(Location.none, [%expr Js.Json.null], [%expr 0])]) + }; +}; From 83c3d288122dadbaf5a8dbd2b089ab5d22687356 Mon Sep 17 00:00:00 2001 From: Sean Grove Date: Mon, 25 Nov 2019 10:24:20 -0800 Subject: [PATCH 2/2] Clean up implementation --- .../output_bucklescript_module.re | 7 +- .../output_bucklescript_unifier.re | 16 +++ .../output_bucklescript_unifier_2.re | 101 ------------------ 3 files changed, 18 insertions(+), 106 deletions(-) delete mode 100644 src/bucklescript/output_bucklescript_unifier_2.re diff --git a/src/bucklescript/output_bucklescript_module.re b/src/bucklescript/output_bucklescript_module.re index 695c3bdc..2e6dee2a 100644 --- a/src/bucklescript/output_bucklescript_module.re +++ b/src/bucklescript/output_bucklescript_module.re @@ -178,12 +178,9 @@ let generate_default_operation = Result_structure.res_loc(res_structure), variable_defs, ); - let (make_fn, make_with_variables_fn, make_variables_fn) = + let (make_fn, make_with_variables_fn, make_variables_fn, definition_tuple) = Output_bucklescript_unifier.make_make_fun(config, variable_defs); - let definition = - Output_bucklescript_unifier_2.make_definition(config, variable_defs); - List.concat([ make_printed_query(config, [Graphql_ast.Operation(operation)]), List.concat([ @@ -209,7 +206,7 @@ let generate_default_operation = [%stri let make = [%e make_fn]], [%stri let makeWithVariables = [%e make_with_variables_fn]], [%stri let makeVariables = [%e make_variables_fn]], - [%stri let definition = [%e definition]], + [%stri let definition = [%e definition_tuple]], ], ]), ret_type_magic, diff --git a/src/bucklescript/output_bucklescript_unifier.re b/src/bucklescript/output_bucklescript_unifier.re index 40e72419..30321f6a 100644 --- a/src/bucklescript/output_bucklescript_unifier.re +++ b/src/bucklescript/output_bucklescript_unifier.re @@ -13,6 +13,14 @@ open Output_bucklescript_utils; exception Unimplemented(string); let make_make_fun = (config, variable_defs) => { + let make_tuple = (loc, variables, compose) => [%expr + ( + parse, + ppx_printed_query, + graphql_ppx_use_json_variables_fn => [%e compose], + ) + ]; + let make_make_triple = (loc, variables) => Ast_helper.Exp.extension( ~loc, @@ -126,10 +134,17 @@ let make_make_fun = (config, variable_defs) => { ) ]; + let user_function = + make_labelled_function( + item, + [%expr graphql_ppx_use_json_variables_fn([%e variable_ctor_body])], + ); + ( make_labelled_function(item, make_make_triple(loc, variable_ctor_body)), make_object_function(item, make_make_triple(loc, variable_ctor_body)), make_labelled_function(item, variable_ctor_body), + make_tuple(loc, variable_ctor_body, user_function), ); | None => ( [%expr @@ -143,6 +158,7 @@ let make_make_fun = (config, variable_defs) => { ) ], [%expr (() => [%e [%expr Js.Json.null]])], + [%expr [%e make_tuple(Location.none, [%expr Js.Json.null], [%expr 0])]], ) }; }; diff --git a/src/bucklescript/output_bucklescript_unifier_2.re b/src/bucklescript/output_bucklescript_unifier_2.re deleted file mode 100644 index 533ef68e..00000000 --- a/src/bucklescript/output_bucklescript_unifier_2.re +++ /dev/null @@ -1,101 +0,0 @@ -open Graphql_ppx_base; -open Graphql_ast; -open Source_pos; -open Generator_utils; - -open Ast_406; -open Parsetree; -open Asttypes; - -open Type_utils; -open Output_bucklescript_utils; - -exception Unimplemented(string); - -let make_definition = (config, variable_defs) => { - let make_tuple = (loc, variables, compose) => [%expr - ( - parse, - ppx_printed_query, - graphql_ppx_use_json_variables_fn => [%e compose], - ) - ]; - - switch (variable_defs) { - | Some({item, span}) => - let rec make_labelled_function = (defs, body) => - switch (defs) { - | [] => - [@metaloc config.map_loc(span) |> conv_loc] [%expr (() => [%e body])] - | [(name, def), ...tl] => - let name_loc = config.map_loc(name.span) |> conv_loc; - Ast_helper.( - Exp.fun_( - ~loc=name_loc, - switch (def.vd_type.item) { - | Tr_non_null_list(_) - | Tr_non_null_named(_) => Labelled(name.item) - | Tr_list(_) - | Tr_named(_) => Optional(name.item) - }, - None, - Pat.var(~loc=name_loc, {txt: name.item, loc: name_loc}), - make_labelled_function(tl, body), - ) - ); - }; - - let make_var_ctor = defs => - defs - |> List.map(((name, def)) => { - let parser_ = - Output_bucklescript_encoder.parser_for_type( - config.schema, - config.map_loc(name.span), - to_native_type_ref(to_schema_type_ref(def.vd_type.item)), - ); - let loc = config.map_loc(name.span) |> conv_loc; - [@metaloc loc] - [%expr - ( - [%e - Ast_helper.Exp.constant( - ~loc, - Pconst_string(name.item, None), - ) - ], - [%e parser_]( - [%e - Ast_helper.Exp.ident( - ~loc, - {txt: Longident.parse(name.item), loc}, - ) - ], - ), - ) - ]; - }) - |> Ast_helper.Exp.array; - let loc = config.map_loc(span) |> conv_loc; - let variable_ctor_body = - [@metaloc loc] - [%expr - Js.Json.object_( - [%e make_var_ctor(item)] - |> [%e Output_bucklescript_encoder.filter_out_null_values] - |> Js.Dict.fromArray, - ) - ]; - - let user_function = - make_labelled_function( - item, - [%expr graphql_ppx_use_json_variables_fn([%e variable_ctor_body])], - ); - - make_tuple(loc, variable_ctor_body, user_function); - | None => - %expr - (() => [%e make_tuple(Location.none, [%expr Js.Json.null], [%expr 0])]) - }; -};