Skip to content

Commit

Permalink
Merge pull request mhallin#53 from sothebys/bugfix-one-field-query-wi…
Browse files Browse the repository at this point in the history
…th-bs-record

Bugfix one field query with bs record
  • Loading branch information
baransu authored Dec 4, 2019
2 parents 19db453 + 6bec925 commit e231d19
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/bucklescript/output_bucklescript_decoder.re
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,11 @@ and generate_record_decoder = (config, loc, name, fields) => {
Some(Pat.var({loc, txt: "field_" ++ field}))
| Fr_fragment_spread(_) => None,
)
|> Pat.tuple
|> (
fun
| [field_pattern] => field_pattern
| field_patterns => Pat.tuple(field_patterns)
)
);

let field_decoder_tuple =
Expand Down Expand Up @@ -279,7 +283,11 @@ and generate_record_decoder = (config, loc, name, fields) => {
}
| Fr_fragment_spread(_) => None,
)
|> Exp.tuple
|> (
fun
| [field_decoder] => field_decoder
| field_decoders => Exp.tuple(field_decoders)
)
);

let record_fields =
Expand Down
24 changes: 24 additions & 0 deletions tests_bucklescript/__tests__/record.re
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ type dog = {
barkVolume: float,
};

type oneFieldQuery = {nullableString: option(string)};

module MyQuery = [%graphql
{|
{
Expand All @@ -19,6 +21,16 @@ module MyQuery = [%graphql
|}
];

module OneFieldQuery = [%graphql
{|
{
variousScalars @bsRecord {
nullableString
}
}
|}
];

module ExternalFragmentQuery = [%graphql
{|
fragment Fragment on VariousScalars @bsRecord {
Expand Down Expand Up @@ -81,6 +93,18 @@ Jest.(
== {"variousScalars": expected};
});

test("Decodes a record with one field in a selection", () => {
let expected = {nullableString: Some("a string")};
expect(
OneFieldQuery.parse(
Js.Json.parseExn(
{|{"variousScalars": {"nullableString": "a string"}}|},
),
),
)
|> toEqual({"variousScalars": expected});
});

test("Decodes a record in an external fragment", () => {
let expected = {string: "a string", int: 123};
expect(
Expand Down

0 comments on commit e231d19

Please sign in to comment.