diff --git a/src/bucklescript/output_bucklescript_decoder.re b/src/bucklescript/output_bucklescript_decoder.re index 662529c6..4950d635 100644 --- a/src/bucklescript/output_bucklescript_decoder.re +++ b/src/bucklescript/output_bucklescript_decoder.re @@ -149,7 +149,7 @@ let generate_error = (loc, message) => { Ast_helper.Exp.extension(~loc, ext); }; -let rec generate_decoder = config => { +let rec generate_decoder = config => fun | Res_nullable(loc, inner) => generate_nullable_decoder(config, conv_loc(loc), inner) @@ -189,28 +189,26 @@ let rec generate_decoder = config => { ) | Res_solo_fragment_spread(loc, name) => generate_solo_fragment_spread(conv_loc(loc), name) - | Res_error(loc, message) => generate_error(conv_loc(loc), message); -} + | Res_error(loc, message) => generate_error(conv_loc(loc), message) and generate_nullable_decoder = (config, loc, inner) => [@metaloc loc] - { + ( switch%expr (Js.Json.decodeNull(value)) { | None => Some([%e generate_decoder(config, inner)]) | Some(_) => None - }; - } + } + ) and generate_array_decoder = (config, loc, inner) => [@metaloc loc] - { - %expr + [%expr value |> Js.Json.decodeArray |> Js.Option.getExn |> Js.Array.map(value => { %e generate_decoder(config, inner) - }); - } + }) + ] and generate_custom_decoder = (config, loc, ident, inner) => { let fn_expr = Ast_helper.( diff --git a/src/bucklescript/output_bucklescript_module.re b/src/bucklescript/output_bucklescript_module.re index 6eb74284..7b8a5054 100644 --- a/src/bucklescript/output_bucklescript_module.re +++ b/src/bucklescript/output_bucklescript_module.re @@ -178,7 +178,7 @@ let generate_default_operation = Result_structure.res_loc(res_structure), variable_defs, ); - let (make_fn, make_with_variables_fn) = + let (make_fn, make_with_variables_fn, make_variables_fn) = Output_bucklescript_unifier.make_make_fun(config, variable_defs); List.concat([ make_printed_query(config, [Graphql_ast.Operation(operation)]), @@ -204,6 +204,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]], ], ]), ret_type_magic, diff --git a/src/bucklescript/output_bucklescript_unifier.re b/src/bucklescript/output_bucklescript_unifier.re index b5034457..40e72419 100644 --- a/src/bucklescript/output_bucklescript_unifier.re +++ b/src/bucklescript/output_bucklescript_unifier.re @@ -26,6 +26,7 @@ let make_make_fun = (config, variable_defs) => { ]), ), ); + switch (variable_defs) { | Some({item, span}) => let rec make_labelled_function = (defs, body) => @@ -128,6 +129,7 @@ let make_make_fun = (config, variable_defs) => { ( 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), ); | None => ( [%expr @@ -140,6 +142,7 @@ let make_make_fun = (config, variable_defs) => { ] ) ], + [%expr (() => [%e [%expr Js.Json.null]])], ) }; }; diff --git a/src/native/output_native_module.re b/src/native/output_native_module.re index 4817ea72..a6b3049a 100644 --- a/src/native/output_native_module.re +++ b/src/native/output_native_module.re @@ -195,7 +195,7 @@ let generate_default_operation = Result_structure.res_loc(res_structure), variable_defs, ); - let (make_fn, make_with_variables_fn) = + let (make_fn, make_with_variables_fn, make_variables_fn) = Output_native_unifier.make_make_fun(config, variable_defs); List.concat([ make_printed_query(config, [Graphql_ast.Operation(operation)]), @@ -224,6 +224,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]], ], ]), /* ret_type_magic */ diff --git a/src/native/output_native_unifier.re b/src/native/output_native_unifier.re index 43473e72..83a4b38e 100644 --- a/src/native/output_native_unifier.re +++ b/src/native/output_native_unifier.re @@ -114,12 +114,14 @@ let make_make_fun = (config, variable_defs) => { ( 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), ); | None => ( [%expr (() => [%e make_make_triple(Location.none, [%expr `Null])])], [%expr ((_: {.}) => [%e make_make_triple(Location.none, [%expr `Null])]) ], + [%expr (() => [%e [%expr `Null]])], ) }; }; diff --git a/tests_bucklescript/__tests__/apolloMode.re b/tests_bucklescript/__tests__/apolloMode.re index 011224c6..6f1a5b95 100644 --- a/tests_bucklescript/__tests__/apolloMode.re +++ b/tests_bucklescript/__tests__/apolloMode.re @@ -55,7 +55,7 @@ describe("Apollo mode", () => { |> Js.String.match(typenameRegex) |> Belt.Option.map(_, Array.length) |> expect - // 3 because on each union case and in simpleSubscription + /* 3 because on each union case and in simpleSubscription */ |> toEqual(Some(3)) ); }); diff --git a/tests_bucklescript/__tests__/apolloMode.rei b/tests_bucklescript/__tests__/apolloMode.rei index 99b3194d..ea81c2eb 100644 --- a/tests_bucklescript/__tests__/apolloMode.rei +++ b/tests_bucklescript/__tests__/apolloMode.rei @@ -36,6 +36,7 @@ module BasciQuery: { "query": string, "variables": Js.Json.t, }; + let makeVariables: unit => Js.Json.t; }; module Subscription: { let query: string; @@ -65,4 +66,5 @@ module Subscription: { "query": string, "variables": Js.Json.t, }; + let makeVariables: unit => Js.Json.t; }; diff --git a/tests_bucklescript/__tests__/argNamedQuery.re b/tests_bucklescript/__tests__/argNamedQuery.re index aa8975df..4c9d7b3e 100644 --- a/tests_bucklescript/__tests__/argNamedQuery.re +++ b/tests_bucklescript/__tests__/argNamedQuery.re @@ -16,6 +16,11 @@ Jest.( == Js.Json.parseExn({|{"query": "a query"}|}) ); + test("Can create variables", () => + expect(MyQuery.makeVariables(~query="a query", ())) + == Js.Json.parseExn({|{"query": "a query"}|}) + ); + test("No name clash with the query field", () => expect(MyQuery.make(~query="a query", ())##query) != "a query" ); diff --git a/tests_bucklescript/__tests__/customDecoder.rei b/tests_bucklescript/__tests__/customDecoder.rei index 318887ac..3e1cc91b 100644 --- a/tests_bucklescript/__tests__/customDecoder.rei +++ b/tests_bucklescript/__tests__/customDecoder.rei @@ -25,4 +25,5 @@ module MyQuery: { "variables": Js.Json.t, }; let query: string; + let makeVariables: unit => Js.Json.t; }; diff --git a/tests_bucklescript/__tests__/enumInput.rei b/tests_bucklescript/__tests__/enumInput.rei index 4e0bbfc1..70eabe40 100644 --- a/tests_bucklescript/__tests__/enumInput.rei +++ b/tests_bucklescript/__tests__/enumInput.rei @@ -19,4 +19,5 @@ module MyQuery: { }; let query: string; + let makeVariables: (~arg: [ | `FIRST | `SECOND | `THIRD], unit) => Js.Json.t; }; diff --git a/tests_bucklescript/__tests__/fragmentDefinition.rei b/tests_bucklescript/__tests__/fragmentDefinition.rei index 1d0794e9..312bab8f 100644 --- a/tests_bucklescript/__tests__/fragmentDefinition.rei +++ b/tests_bucklescript/__tests__/fragmentDefinition.rei @@ -51,4 +51,5 @@ module MyQuery: { "query": string, "variables": Js.Json.t, }; + let makeVariables: unit => Js.Json.t; }; diff --git a/tests_bucklescript/__tests__/interface.rei b/tests_bucklescript/__tests__/interface.rei index 9533797c..3290dbdb 100644 --- a/tests_bucklescript/__tests__/interface.rei +++ b/tests_bucklescript/__tests__/interface.rei @@ -40,6 +40,7 @@ module QueryWithFragments: { "variables": Js.Json.t, }; let query: string; + let makeVariables: unit => Js.Json.t; }; module QueryWithoutFragments: { @@ -62,4 +63,5 @@ module QueryWithoutFragments: { "variables": Js.Json.t, }; let query: string; + let makeVariables: unit => Js.Json.t; }; diff --git a/tests_bucklescript/__tests__/lists.rei b/tests_bucklescript/__tests__/lists.rei index a0fddee3..9c2a86af 100644 --- a/tests_bucklescript/__tests__/lists.rei +++ b/tests_bucklescript/__tests__/lists.rei @@ -27,4 +27,5 @@ module MyQuery: { "variables": Js.Json.t, }; let query: string; + let makeVariables: unit => Js.Json.t; }; diff --git a/tests_bucklescript/__tests__/listsArgs.rei b/tests_bucklescript/__tests__/listsArgs.rei index 0c5a6c17..45e3d194 100644 --- a/tests_bucklescript/__tests__/listsArgs.rei +++ b/tests_bucklescript/__tests__/listsArgs.rei @@ -30,4 +30,13 @@ module MyQuery: { "variables": Js.Json.t, }; let query: string; + let makeVariables: + ( + ~nullableOfNullable: array(option(string))=?, + ~nullableOfNonNullable: array(string)=?, + ~nonNullableOfNullable: array(option(string)), + ~nonNullableOfNonNullable: array(string), + unit + ) => + Js.Json.t; }; diff --git a/tests_bucklescript/__tests__/listsInput.rei b/tests_bucklescript/__tests__/listsInput.rei index 0777422c..7847bc25 100644 --- a/tests_bucklescript/__tests__/listsInput.rei +++ b/tests_bucklescript/__tests__/listsInput.rei @@ -37,5 +37,18 @@ module MyQuery: { "variables": Js.Json.t, }; + let makeVariables: + ( + ~arg: { + .. + "nullableOfNullable": option(array(option(string))), + "nullableOfNonNullable": option(array(string)), + "nonNullableOfNullable": array(option(string)), + "nonNullableOfNonNullable": array(string), + }, + unit + ) => + Js.Json.t; + let query: string; }; diff --git a/tests_bucklescript/__tests__/mutation.rei b/tests_bucklescript/__tests__/mutation.rei index a2ef89a4..23fd8d31 100644 --- a/tests_bucklescript/__tests__/mutation.rei +++ b/tests_bucklescript/__tests__/mutation.rei @@ -33,4 +33,5 @@ module MyQuery: { }; let query: string; + let makeVariables: unit => Js.Json.t; }; diff --git a/tests_bucklescript/__tests__/nested.rei b/tests_bucklescript/__tests__/nested.rei index ab8c7e18..55734942 100644 --- a/tests_bucklescript/__tests__/nested.rei +++ b/tests_bucklescript/__tests__/nested.rei @@ -28,4 +28,5 @@ module MyQuery: { }; let query: string; + let makeVariables: unit => Js.Json.t; }; diff --git a/tests_bucklescript/__tests__/nonrecursiveInput.rei b/tests_bucklescript/__tests__/nonrecursiveInput.rei index 39d4b256..a3f915c0 100644 --- a/tests_bucklescript/__tests__/nonrecursiveInput.rei +++ b/tests_bucklescript/__tests__/nonrecursiveInput.rei @@ -34,4 +34,14 @@ module MyQuery: { }; let query: string; + let makeVariables: + ( + ~arg: { + . + "enum": option([ | `FIRST | `SECOND | `THIRD]), + "field": option(string), + }, + unit + ) => + Js.Json.t; }; diff --git a/tests_bucklescript/__tests__/record.rei b/tests_bucklescript/__tests__/record.rei index f8023e31..66e65b61 100644 --- a/tests_bucklescript/__tests__/record.rei +++ b/tests_bucklescript/__tests__/record.rei @@ -23,5 +23,7 @@ module MyQuery: { "variables": Js.Json.t, }; + let makeVariables: unit => Js.Json.t; + let query: string; }; diff --git a/tests_bucklescript/__tests__/recursiveInput.rei b/tests_bucklescript/__tests__/recursiveInput.rei index 11ec5601..17397b33 100644 --- a/tests_bucklescript/__tests__/recursiveInput.rei +++ b/tests_bucklescript/__tests__/recursiveInput.rei @@ -36,4 +36,15 @@ module MyQuery: { }; let query: string; + let makeVariables: + ( + ~arg: { + . + "inner": option('a), + "enum": option([ | `FIRST | `SECOND | `THIRD]), + "otherField": option(string), + } as 'a, + unit + ) => + Js.Json.t; }; diff --git a/tests_bucklescript/__tests__/scalars.rei b/tests_bucklescript/__tests__/scalars.rei index 66685ad1..88595846 100644 --- a/tests_bucklescript/__tests__/scalars.rei +++ b/tests_bucklescript/__tests__/scalars.rei @@ -34,4 +34,5 @@ module MyQuery: { }; let query: string; + let makeVariables: unit => Js.Json.t; }; diff --git a/tests_bucklescript/__tests__/scalarsArgs.rei b/tests_bucklescript/__tests__/scalarsArgs.rei index b52d0911..95484443 100644 --- a/tests_bucklescript/__tests__/scalarsArgs.rei +++ b/tests_bucklescript/__tests__/scalarsArgs.rei @@ -44,4 +44,19 @@ module MyQuery: { }; let query: string; + let makeVariables: + ( + ~nullableString: string=?, + ~string: string, + ~nullableInt: int=?, + ~int: int, + ~nullableFloat: float=?, + ~float: float, + ~nullableBoolean: bool=?, + ~boolean: bool, + ~nullableID: string=?, + ~id: string, + unit + ) => + Js.Json.t; }; diff --git a/tests_bucklescript/__tests__/scalarsInput.rei b/tests_bucklescript/__tests__/scalarsInput.rei index 4ff63df4..f2f741e9 100644 --- a/tests_bucklescript/__tests__/scalarsInput.rei +++ b/tests_bucklescript/__tests__/scalarsInput.rei @@ -50,4 +50,22 @@ module MyQuery: { }; let query: string; + let makeVariables: + ( + ~arg: { + .. + "nullableString": option(string), + "string": string, + "nullableInt": option(int), + "int": int, + "nullableFloat": option(float), + "float": float, + "nullableBoolean": option(bool), + "boolean": bool, + "nullableID": option(string), + "id": string, + }, + unit + ) => + Js.Json.t; }; diff --git a/tests_bucklescript/__tests__/skipDirectives.rei b/tests_bucklescript/__tests__/skipDirectives.rei index 9e984457..23e38a32 100644 --- a/tests_bucklescript/__tests__/skipDirectives.rei +++ b/tests_bucklescript/__tests__/skipDirectives.rei @@ -32,4 +32,5 @@ module MyQuery: { }; let query: string; + let makeVariables: (~var: bool, unit) => Js.Json.t; }; diff --git a/tests_bucklescript/__tests__/subscription.rei b/tests_bucklescript/__tests__/subscription.rei index 7a1e32bc..28473dda 100644 --- a/tests_bucklescript/__tests__/subscription.rei +++ b/tests_bucklescript/__tests__/subscription.rei @@ -24,4 +24,5 @@ module MyQuery: { "variables": Js.Json.t, }; let query: string; + let makeVariables: unit => Js.Json.t; }; diff --git a/tests_bucklescript/__tests__/typename.rei b/tests_bucklescript/__tests__/typename.rei index 8bc8ced4..3faa5a5b 100644 --- a/tests_bucklescript/__tests__/typename.rei +++ b/tests_bucklescript/__tests__/typename.rei @@ -35,4 +35,5 @@ module MyQuery: { "variables": Js.Json.t, }; let query: string; + let makeVariables: unit => Js.Json.t; }; diff --git a/tests_bucklescript/__tests__/union.rei b/tests_bucklescript/__tests__/union.rei index ae7b4707..8cb95e88 100644 --- a/tests_bucklescript/__tests__/union.rei +++ b/tests_bucklescript/__tests__/union.rei @@ -30,4 +30,6 @@ module MyQuery: { "variables": Js.Json.t, }; let query: string; + + let makeVariables: unit => Js.Json.t; }; diff --git a/tests_bucklescript/__tests__/unionPartial.rei b/tests_bucklescript/__tests__/unionPartial.rei index 8a49c818..032c784b 100644 --- a/tests_bucklescript/__tests__/unionPartial.rei +++ b/tests_bucklescript/__tests__/unionPartial.rei @@ -29,4 +29,5 @@ module MyQuery: { "variables": Js.Json.t, }; let query: string; + let makeVariables: unit => Js.Json.t; }; diff --git a/tests_bucklescript/__tests__/variant.rei b/tests_bucklescript/__tests__/variant.rei index 05528c1b..0b6fa0b5 100644 --- a/tests_bucklescript/__tests__/variant.rei +++ b/tests_bucklescript/__tests__/variant.rei @@ -30,4 +30,5 @@ module MyQuery: { "variables": Js.Json.t, }; let query: string; + let makeVariables: unit => Js.Json.t; };