Skip to content

Commit

Permalink
Merge pull request #35 from jfrolich/makeVariables
Browse files Browse the repository at this point in the history
Add makeVariables function
  • Loading branch information
baransu authored Nov 23, 2019
2 parents 857cdf8 + 3dea997 commit 667673c
Show file tree
Hide file tree
Showing 29 changed files with 119 additions and 13 deletions.
18 changes: 8 additions & 10 deletions src/bucklescript/output_bucklescript_decoder.re
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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.(
Expand Down
3 changes: 2 additions & 1 deletion src/bucklescript/output_bucklescript_module.re
Original file line number Diff line number Diff line change
Expand Up @@ -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)]),
Expand All @@ -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,
Expand Down
3 changes: 3 additions & 0 deletions src/bucklescript/output_bucklescript_unifier.re
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ let make_make_fun = (config, variable_defs) => {
]),
),
);

switch (variable_defs) {
| Some({item, span}) =>
let rec make_labelled_function = (defs, body) =>
Expand Down Expand Up @@ -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
Expand All @@ -140,6 +142,7 @@ let make_make_fun = (config, variable_defs) => {
]
)
],
[%expr (() => [%e [%expr Js.Json.null]])],
)
};
};
3 changes: 2 additions & 1 deletion src/native/output_native_module.re
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,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)]),
Expand All @@ -203,6 +203,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,
Expand Down
2 changes: 2 additions & 0 deletions src/native/output_native_unifier.re
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,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
Expand All @@ -130,6 +131,7 @@ let make_make_fun = (config, variable_defs) => {
]
)
],
[%expr (() => [%e [%expr `Null]])],
)
};
};
2 changes: 1 addition & 1 deletion tests_bucklescript/__tests__/apolloMode.re
Original file line number Diff line number Diff line change
Expand Up @@ -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))
);
});
2 changes: 2 additions & 0 deletions tests_bucklescript/__tests__/apolloMode.rei
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ module BasciQuery: {
"query": string,
"variables": Js.Json.t,
};
let makeVariables: unit => Js.Json.t;
};
module Subscription: {
let query: string;
Expand Down Expand Up @@ -65,4 +66,5 @@ module Subscription: {
"query": string,
"variables": Js.Json.t,
};
let makeVariables: unit => Js.Json.t;
};
5 changes: 5 additions & 0 deletions tests_bucklescript/__tests__/argNamedQuery.re
Original file line number Diff line number Diff line change
Expand Up @@ -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"
);
Expand Down
1 change: 1 addition & 0 deletions tests_bucklescript/__tests__/customDecoder.rei
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,5 @@ module MyQuery: {
"variables": Js.Json.t,
};
let query: string;
let makeVariables: unit => Js.Json.t;
};
1 change: 1 addition & 0 deletions tests_bucklescript/__tests__/enumInput.rei
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@ module MyQuery: {
};

let query: string;
let makeVariables: (~arg: [ | `FIRST | `SECOND | `THIRD], unit) => Js.Json.t;
};
1 change: 1 addition & 0 deletions tests_bucklescript/__tests__/fragmentDefinition.rei
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,5 @@ module MyQuery: {
"query": string,
"variables": Js.Json.t,
};
let makeVariables: unit => Js.Json.t;
};
2 changes: 2 additions & 0 deletions tests_bucklescript/__tests__/interface.rei
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ module QueryWithFragments: {
"variables": Js.Json.t,
};
let query: string;
let makeVariables: unit => Js.Json.t;
};

module QueryWithoutFragments: {
Expand All @@ -62,4 +63,5 @@ module QueryWithoutFragments: {
"variables": Js.Json.t,
};
let query: string;
let makeVariables: unit => Js.Json.t;
};
1 change: 1 addition & 0 deletions tests_bucklescript/__tests__/lists.rei
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,5 @@ module MyQuery: {
"variables": Js.Json.t,
};
let query: string;
let makeVariables: unit => Js.Json.t;
};
9 changes: 9 additions & 0 deletions tests_bucklescript/__tests__/listsArgs.rei
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
13 changes: 13 additions & 0 deletions tests_bucklescript/__tests__/listsInput.rei
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
1 change: 1 addition & 0 deletions tests_bucklescript/__tests__/mutation.rei
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,5 @@ module MyQuery: {
};

let query: string;
let makeVariables: unit => Js.Json.t;
};
1 change: 1 addition & 0 deletions tests_bucklescript/__tests__/nested.rei
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,5 @@ module MyQuery: {
};

let query: string;
let makeVariables: unit => Js.Json.t;
};
10 changes: 10 additions & 0 deletions tests_bucklescript/__tests__/nonrecursiveInput.rei
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,14 @@ module MyQuery: {
};

let query: string;
let makeVariables:
(
~arg: {
.
"enum": option([ | `FIRST | `SECOND | `THIRD]),
"field": option(string),
},
unit
) =>
Js.Json.t;
};
2 changes: 2 additions & 0 deletions tests_bucklescript/__tests__/record.rei
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,7 @@ module MyQuery: {
"variables": Js.Json.t,
};

let makeVariables: unit => Js.Json.t;

let query: string;
};
11 changes: 11 additions & 0 deletions tests_bucklescript/__tests__/recursiveInput.rei
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
1 change: 1 addition & 0 deletions tests_bucklescript/__tests__/scalars.rei
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,5 @@ module MyQuery: {
};

let query: string;
let makeVariables: unit => Js.Json.t;
};
15 changes: 15 additions & 0 deletions tests_bucklescript/__tests__/scalarsArgs.rei
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
18 changes: 18 additions & 0 deletions tests_bucklescript/__tests__/scalarsInput.rei
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
1 change: 1 addition & 0 deletions tests_bucklescript/__tests__/skipDirectives.rei
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,5 @@ module MyQuery: {
};

let query: string;
let makeVariables: (~var: bool, unit) => Js.Json.t;
};
1 change: 1 addition & 0 deletions tests_bucklescript/__tests__/subscription.rei
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,5 @@ module MyQuery: {
"variables": Js.Json.t,
};
let query: string;
let makeVariables: unit => Js.Json.t;
};
1 change: 1 addition & 0 deletions tests_bucklescript/__tests__/typename.rei
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,5 @@ module MyQuery: {
"variables": Js.Json.t,
};
let query: string;
let makeVariables: unit => Js.Json.t;
};
2 changes: 2 additions & 0 deletions tests_bucklescript/__tests__/union.rei
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,6 @@ module MyQuery: {
"variables": Js.Json.t,
};
let query: string;

let makeVariables: unit => Js.Json.t;
};
1 change: 1 addition & 0 deletions tests_bucklescript/__tests__/unionPartial.rei
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,5 @@ module MyQuery: {
"variables": Js.Json.t,
};
let query: string;
let makeVariables: unit => Js.Json.t;
};
1 change: 1 addition & 0 deletions tests_bucklescript/__tests__/variant.rei
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,5 @@ module MyQuery: {
"variables": Js.Json.t,
};
let query: string;
let makeVariables: unit => Js.Json.t;
};

0 comments on commit 667673c

Please sign in to comment.