Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix mutation optional variables #64

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
243 changes: 135 additions & 108 deletions graphql_schema.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions schema.gql
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ type Query {

type Mutation {
mutationWithError: MutationWithErrorResult!
mutationWithOptionalArgument(field: String): MutationWithErrorResult!
}

type Subscription {
Expand Down
10 changes: 8 additions & 2 deletions src/bucklescript/output_bucklescript_encoder.ml
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ let rec parser_for_type schema loc type_ref =
| Some ty ->
function_name_string ty |> ident_from_string (conv_loc loc)

let filter_out_null_values =
[%expr Js.Array.filter (fun (_, value) -> value <> Js.Json.null)]

let json_of_fields schema loc expr fields =
let field_array_exprs = fields |> List.map
(fun {am_name; am_arg_type; _} ->
Expand All @@ -79,8 +82,11 @@ let json_of_fields schema loc expr fields =
[%e parser] ([%e expr] ## [%e ident_from_string (conv_loc loc) am_name])
)] [@metaloc conv_loc loc]) in
let field_array = Ast_helper.Exp.array field_array_exprs in
[%expr Js.Json.object_ (Js.Dict.fromArray [%e field_array])] [@metaloc conv_loc loc]

[%expr Js.Json.object_ (
[%e field_array]
|> [%e filter_out_null_values]
|> Js.Dict.fromArray
)] [@metaloc conv_loc loc]
let generate_encoder config (spanning, x) =
let loc = config.map_loc spanning.span in
let body = match x with
Expand Down
6 changes: 5 additions & 1 deletion src/bucklescript/output_bucklescript_unifier.ml
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,11 @@ let make_make_fun config variable_defs =
|> Ast_helper.Exp.array in
let loc = config.map_loc span |> conv_loc in
let variable_ctor_body =
[%expr Js.Json.object_ (Js.Dict.fromArray [%e make_var_ctor item])] [@metaloc loc]
[%expr Js.Json.object_ (
[%e make_var_ctor item]
|> [%e Output_bucklescript_encoder.filter_out_null_values]
|> Js.Dict.fromArray
)] [@metaloc loc]
in
(
make_labelled_function item (make_make_triple loc variable_ctor_body),
Expand Down
3 changes: 0 additions & 3 deletions tests_bucklescript/__tests__/listsArgs.re
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ Jest.(describe("Lists as query arguments", () => {
())##variables)
== Js.Json.parseExn({|
{
"nullableOfNullable": null,
"nullableOfNonNullable": null,
"nonNullableOfNullable": [],
"nonNullableOfNonNullable": []
}
Expand All @@ -43,7 +41,6 @@ Jest.(describe("Lists as query arguments", () => {
== Js.Json.parseExn({|
{
"nullableOfNullable": ["x", null, "y"],
"nullableOfNonNullable": null,
"nonNullableOfNullable": ["a", null, "b"],
"nonNullableOfNonNullable": ["1", "2", "3"]
}
Expand Down
1 change: 0 additions & 1 deletion tests_bucklescript/__tests__/listsInput.re
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ Jest.(describe("Lists as query arguments through input object", () => {
{
"arg": {
"nullableOfNullable": ["x", null, "y"],
"nullableOfNonNullable": null,
"nonNullableOfNullable": ["a", null, "b"],
"nonNullableOfNonNullable": ["1", "2", "3"]
}
Expand Down
19 changes: 19 additions & 0 deletions tests_bucklescript/__tests__/mutationWithOptionalArgument.re
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module MyMutation = [%graphql {|
mutation($field: String) {
mutationWithOptionalArgument(field: $field) {
value {
stringField
}
}
}
|}];

Jest.(describe("Mutation", () => {
open Expect;
open! Expect.Operators;

test("Omits not provided optional arguments", () => {
let variables = MyMutation.make(())##variables;
expect(variables) == Js.Json.parseExn({|{}|});
});
}));
13 changes: 13 additions & 0 deletions tests_bucklescript/__tests__/mutationWithOptionalArgument.rei
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module MyMutation: {
type t = Js.t({
.
mutationWithOptionalArgument: Js.t({ .
value: option(Js.t({. stringField: string})),
}),
});

let make: (~field: string=?, unit) => Js.t({ . parse: Js.Json.t => t, query: string, variables: Js.Json.t });
let makeWithVariables: {. "field": option(string) } => Js.t({ . parse: Js.Json.t => t, query: string, variables: Js.Json.t });

let query: string;
};
2 changes: 0 additions & 2 deletions tests_bucklescript/__tests__/recursiveInput.re
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,8 @@ Jest.(describe("Recursive input types", () => {
== Js.Json.parseExn({| {
"arg": {
"otherField": "test",
"enum": null,
"inner": {
"otherField": "inner",
"inner": null,
"enum": "SECOND"
}
}
Expand Down
5 changes: 0 additions & 5 deletions tests_bucklescript/__tests__/scalarsArgs.re
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,10 @@ Jest.(describe("Scalars as arguments", () => {
~id="an ID",
())##variables)
== Js.Json.parseExn({| {
"nullableString": null,
"string": "a string",
"nullableInt": null,
"int": 123,
"nullableFloat": null,
"float": 1234.5,
"nullableBoolean": null,
"boolean": true,
"nullableID": null,
"id": "an ID"
} |}));

Expand Down