Skip to content

Commit 74b1b9f

Browse files
committed
feat: update output for rescript v12
1 parent d995183 commit 74b1b9f

4 files changed

Lines changed: 73 additions & 52 deletions

File tree

src/ppx/output_module.ml

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -177,25 +177,34 @@ let rec emit_json config = function
177177
emit_json config value;
178178
]))
179179
in
180-
add_uapp [%expr Js.Json.object_ (Js.Dict.fromArray [%e pairs])]
180+
add_uapp
181+
(Ast_helper.Exp.apply
182+
(Ast_helper.Exp.ident
183+
{
184+
txt =
185+
Longident.Ldot
186+
(Longident.Ldot (Longident.Lident "JSON", "Encode"), "object");
187+
loc = Location.none;
188+
})
189+
[ (Nolabel, [%expr Dict.fromArray [%e pairs]]) ])
181190
| `List ls ->
182191
let values = Ast_helper.Exp.array (List.map (emit_json config) ls) in
183-
add_uapp [%expr Js.Json.array [%e values]]
184-
| `Bool true -> [%expr Js.Json.boolean true]
185-
| `Bool false -> [%expr Js.Json.boolean false]
186-
| `Null -> [%expr Obj.magic Js.Undefined.empty]
192+
add_uapp [%expr JSON.Encode.array [%e values]]
193+
| `Bool true -> [%expr JSON.Encode.bool true]
194+
| `Bool false -> [%expr JSON.Encode.bool false]
195+
| `Null -> [%expr JSON.Encode.null]
187196
| `String s ->
188197
add_uapp
189198
[%expr
190-
Js.Json.string
199+
JSON.Encode.string
191200
[%e Ast_helper.Exp.constant (Pconst_string (s, Location.none, None))]]
192201
| `Int i ->
193202
add_uapp
194203
[%expr
195-
Js.Json.number
204+
JSON.Encode.float
196205
[%e Ast_helper.Exp.constant (Pconst_float (string_of_int i, None))]]
197206
| `StringExpr parts ->
198-
add_uapp [%expr Js.Json.string [%e emit_printed_query ~config parts]]
207+
add_uapp [%expr JSON.Encode.string [%e emit_printed_query ~config parts]]
199208

200209
let wrap_template_tag ?import ?location ?template_tag source =
201210
match (import, location, template_tag) with
@@ -605,10 +614,10 @@ let generate_operation_signature config variable_defs res_structure =
605614
]
606615
| false ->
607616
[
608-
[%sigi: external unsafe_fromJson : Js.Json.t -> Raw.t = "%identity"];
609-
[%sigi: external toJson : Raw.t -> Js.Json.t = "%identity"];
617+
[%sigi: external unsafe_fromJson : JSON.t -> Raw.t = "%identity"];
618+
[%sigi: external toJson : Raw.t -> JSON.t = "%identity"];
610619
[%sigi:
611-
external variablesToJson : Raw.t_variables -> Js.Json.t = "%identity"];
620+
external variablesToJson : Raw.t_variables -> JSON.t = "%identity"];
612621
]);
613622
]
614623
|> List.concat
@@ -796,10 +805,10 @@ let generate_operation_implementation config variable_defs _has_error operation
796805
]
797806
| false ->
798807
[
799-
[%stri external unsafe_fromJson : Js.Json.t -> Raw.t = "%identity"];
800-
[%stri external toJson : Raw.t -> Js.Json.t = "%identity"];
808+
[%stri external unsafe_fromJson : JSON.t -> Raw.t = "%identity"];
809+
[%stri external toJson : Raw.t -> JSON.t = "%identity"];
801810
[%stri
802-
external variablesToJson : Raw.t_variables -> Js.Json.t
811+
external variablesToJson : Raw.t_variables -> JSON.t
803812
= "%identity"];
804813
]);
805814
]
@@ -927,8 +936,8 @@ let generate_fragment_signature config name variable_definitions _has_error
927936
]
928937
| false ->
929938
[
930-
[%sigi: external unsafe_fromJson : Js.Json.t -> Raw.t = "%identity"];
931-
[%sigi: external toJson : Raw.t -> Js.Json.t = "%identity"];
939+
[%sigi: external unsafe_fromJson : JSON.t -> Raw.t = "%identity"];
940+
[%sigi: external toJson : Raw.t -> JSON.t = "%identity"];
932941
]);
933942
]
934943

@@ -1052,8 +1061,8 @@ let generate_fragment_implementation config name
10521061
]
10531062
| false ->
10541063
[
1055-
[%stri external unsafe_fromJson : Js.Json.t -> Raw.t = "%identity"];
1056-
[%stri external toJson : Raw.t -> Js.Json.t = "%identity"];
1064+
[%stri external unsafe_fromJson : JSON.t -> Raw.t = "%identity"];
1065+
[%stri external toJson : Raw.t -> JSON.t = "%identity"];
10571066
]);
10581067
]
10591068
|> List.concat

src/ppx/output_parser.ml

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ let make_error_raiser message =
5050
[%expr raise (Failure ("graphql-ppx: " ^ [%e message]))]
5151
else [%expr raise (Failure "Unexpected GraphQL query response")]
5252
else if Ppx_config.verbose_error_handling () then
53-
[%expr Js.Exn.raiseError ("graphql-ppx: " ^ [%e message])]
54-
else [%expr Js.Exn.raiseError "Unexpected GraphQL query response"]
53+
[%expr throw (Exn.Error ("graphql-ppx: " ^ [%e message]))]
54+
else [%expr throw (Exn.Error "Unexpected GraphQL query response")]
5555

5656
let raw_value loc = ([%expr value] [@metaloc loc])
5757

@@ -174,7 +174,7 @@ and generate_array_decoder config loc inner path definition =
174174
[@metaloc loc]
175175
| false ->
176176
[%expr
177-
Js.Array2.map value (fun value ->
177+
Array.map value (fun value ->
178178
[%e generate_parser config path definition inner])]
179179
[@metaloc loc]
180180

@@ -207,7 +207,7 @@ and generate_object_decoder ~config ~loc ~name:_name ~path ~definition
207207
| true ->
208208
[%expr
209209
Obj.magic
210-
(Js.Dict.unsafeGet (Obj.magic value)
210+
(Dict.getUnsafe (Obj.magic value)
211211
[%e const_str_expr key])]
212212
| false ->
213213
Ast_helper.Exp.field
@@ -286,13 +286,13 @@ and generate_poly_variant_selection_set_decoder config loc name fields path
286286
| false ->
287287
[%expr
288288
let temp =
289-
Js.Dict.unsafeGet (Obj.magic value) [%e const_str_expr field]
289+
Dict.getUnsafe (Obj.magic value) [%e const_str_expr field]
290290
in
291-
match Js.Json.decodeNull temp with
291+
match JSON.Decode.null temp with
292+
| Some _ -> [%e generator_loop next]
292293
| None ->
293294
let value = temp in
294-
[%e variant_decoder]
295-
| Some _ -> [%e generator_loop next]]
295+
[%e variant_decoder]]
296296
[@metaloc loc])
297297
| [] ->
298298
make_error_raiser
@@ -313,7 +313,19 @@ and generate_poly_variant_selection_set_decoder config loc name fields path
313313
[@metaloc loc]
314314
| false ->
315315
[%expr
316-
match Js.Json.decodeObject (Obj.magic value : Js.Json.t) with
316+
match
317+
(Ast_helper.Exp.apply
318+
(Ast_helper.Exp.ident
319+
{
320+
txt =
321+
Longident.Ldot
322+
( Longident.Ldot
323+
(Longident.Lident "JSON", "Decode"),
324+
"object" );
325+
loc = Location.none;
326+
})
327+
[ (Nolabel, [%expr (Obj.magic value : JSON.t)]) ])
328+
with
317329
| None ->
318330
[%e
319331
make_error_raiser
@@ -370,7 +382,7 @@ and generate_poly_variant_interface_decoder config loc _name fragments path
370382
| false ->
371383
[%expr
372384
let typename =
373-
(Obj.magic (Js.Dict.unsafeGet (Obj.magic value) "__typename") : string)
385+
(Obj.magic (Dict.getUnsafe (Obj.magic value) "__typename") : string)
374386
in
375387
([%e typename_matcher] : [%t base_type_name (generate_type_name path)])]
376388
[@metaloc loc]
@@ -437,7 +449,7 @@ and generate_poly_variant_union_decoder config loc _name fragments
437449
Location.txt = Longident.parse "value";
438450
loc = Location.none;
439451
}]
440-
: Js.Json.t)]))
452+
: JSON.t)]))
441453
in
442454
let typename_matcher =
443455
Ast_helper.Exp.match_ [%expr typename]
@@ -456,7 +468,7 @@ and generate_poly_variant_union_decoder config loc _name fragments
456468
else
457469
[%expr
458470
let typename =
459-
(Obj.magic (Js.Dict.unsafeGet (Obj.magic value) "__typename") : string)
471+
(Obj.magic (Dict.getUnsafe (Obj.magic value) "__typename") : string)
460472
in
461473
([%e typename_matcher] : [%t base_type_name (generate_type_name path)])]
462474
[@metaloc loc]

src/ppx/output_serializer.ml

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,8 @@ let rec serialize_type = function
8282
[%expr
8383
fun a ->
8484
match a with
85-
| None -> Js.Nullable.undefined
86-
| Some b -> Js.Nullable.return ([%e serialize_type inner] b)])
85+
| None -> Nullable.undefined
86+
| Some b -> Nullable.make ([%e serialize_type inner] b)])
8787
| List inner -> (
8888
match Ppx_config.native () with
8989
| true ->
@@ -92,7 +92,7 @@ let rec serialize_type = function
9292
`List
9393
(Array.map (fun b -> [%e serialize_type inner] b) a |> Array.to_list)]
9494
| false ->
95-
[%expr fun a -> Js.Array2.map a (fun b -> [%e serialize_type inner] b)])
95+
[%expr fun a -> Array.map a (fun b -> [%e serialize_type inner] b)])
9696
| Type (Object _) -> [%expr fun v -> None]
9797
| Type (Union _) -> [%expr fun v -> None]
9898
| Type (Interface _) -> [%expr fun v -> None]
@@ -361,9 +361,9 @@ let rec generate_nullable_encoder config loc inner path definition =
361361
[%expr
362362
match value with
363363
| Some value ->
364-
Js.Nullable.return
364+
Nullable.make
365365
[%e generate_serializer config path definition None inner]
366-
| None -> Js.Nullable.null]
366+
| None -> Nullable.null]
367367
[@metaloc loc]
368368

369369
and generate_array_encoder config loc inner path definition =
@@ -378,7 +378,7 @@ and generate_array_encoder config loc inner path definition =
378378
[@metaloc loc]
379379
| false ->
380380
[%expr
381-
Js.Array2.map value (fun value ->
381+
Array.map value (fun value ->
382382
[%e generate_serializer config path definition None inner])]
383383
[@metaloc loc]
384384

@@ -431,7 +431,7 @@ and generate_object_encoder config loc _name fields path definition
431431
| [] -> (
432432
match Ppx_config.native () with
433433
| true -> [%expr `Assoc []]
434-
| false -> [%expr Js.Dict.empty])
434+
| false -> [%expr Dict.fromArray [||]])
435435
| fields ->
436436
let record =
437437
Ast_helper.Exp.record
@@ -548,7 +548,7 @@ and generate_object_encoder config loc _name fields path definition
548548
(Obj.magic
549549
([%e ident_from_string (name ^ ".serialize")]
550550
[%e get_field key existing_record path])
551-
: Js.Json.t)])
551+
: JSON.t)])
552552
:: acc)
553553
[]
554554
|> List.rev
@@ -571,7 +571,7 @@ and generate_object_encoder config loc _name fields path definition
571571
[%e
572572
generate_poly_variant_interface_encoder config loc
573573
interface_name fragments path definition]
574-
: Js.Json.t)])
574+
: JSON.t)])
575575
:: fields
576576
in
577577
match Ppx_config.native () with
@@ -582,10 +582,10 @@ and generate_object_encoder config loc _name fields path definition
582582
| false ->
583583
[%expr
584584
(Obj.magic
585-
(Js.Array2.reduce
585+
(Array.reduce
586586
[%e fields |> Ast_helper.Exp.array]
587-
Graphql_ppx_runtime.deepMerge
588-
(Obj.magic [%e do_obj_constructor ()] : Js.Json.t))
587+
(Obj.magic [%e do_obj_constructor ()] : JSON.t)
588+
Graphql_ppx_runtime.deepMerge)
589589
: [%t base_type_name ("Raw." ^ generate_type_name path)])]
590590
in
591591
match is_opaque with
@@ -627,7 +627,7 @@ and generate_poly_variant_union_encoder config _loc _name fragments _exhaustive
627627
[%expr
628628
(Obj.magic
629629
(Graphql_ppx_runtime.assign_typename
630-
(Obj.magic [%e expr] : Js.Json.t)
630+
(Obj.magic [%e expr] : JSON.t)
631631
[%e const_str_expr type_name])
632632
: [%t raw_type_name])]
633633
| _ -> [%expr (Obj.magic [%e expr] : [%t raw_type_name])])))
@@ -660,7 +660,7 @@ and generate_poly_variant_selection_set_encoder _config _loc _name _fields _path
660660
let e =
661661
match Ppx_config.native () with
662662
| true -> [%expr `Null]
663-
| false -> [%expr Obj.magic Js.Json.null]
663+
| false -> [%expr Obj.magic JSON.Encode.null]
664664
in
665665
[%expr
666666
let _temp = value in
@@ -697,7 +697,7 @@ and generate_poly_variant_interface_encoder config _loc name fragments path
697697
| true -> [%expr `Assoc []]
698698
| false ->
699699
[%expr
700-
(Obj.magic (Js.Dict.empty ())
700+
(Obj.magic (Dict.fromArray [||])
701701
: [%t base_type_name ("Raw." ^ generate_type_name (name :: path))])])
702702
in
703703
let typename_matcher =

src/ppx/output_types.ml

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ let make_error_raiser message =
1313
[%expr raise (Failure ("graphql-ppx: " ^ [%e message]))]
1414
else [%expr raise (Failure "Unexpected GraphQL query response")]
1515
else if Ppx_config.verbose_error_handling () then
16-
[%expr Js.Exn.raiseError ("graphql-ppx: " ^ [%e message])]
17-
else [%expr Js.Exn.raiseError "Unexpected GraphQL query response"]
16+
[%expr throw (Exn.Error ("graphql-ppx: " ^ [%e message]))]
17+
else [%expr throw (Exn.Error "Unexpected GraphQL query response")]
1818

1919
let const_str_expr s = Ast_helper.Exp.constant (Pconst_string (s, loc, None))
2020

@@ -24,7 +24,7 @@ let rec generate_type ?atLoc ~config ~path ~raw = function
2424
if raw then
2525
base_type
2626
~inner:[ generate_type ~config ~path ~raw inner ]
27-
"Js.Nullable.t"
27+
"Nullable.t"
2828
else
2929
base_type ?loc:atLoc
3030
~inner:[ generate_type ~config ~path ~raw inner ]
@@ -44,7 +44,7 @@ let rec generate_type ?atLoc ~config ~path ~raw = function
4444
base_type ?loc:atLoc
4545
(match Ppx_config.native () with
4646
| true -> "Graphql_ppx_runtime.Json.t"
47-
| false -> "Js.Json.t")
47+
| false -> "JSON.t")
4848
| Res_object { type_name } | Res_record { type_name } -> (
4949
match (type_name, raw) with
5050
| Some type_name, false -> base_type ?loc:atLoc type_name
@@ -292,7 +292,7 @@ let generate_variant_union ~emit_locations config
292292
base_type
293293
(match Ppx_config.native () with
294294
| true -> "Graphql_ppx_runtime.Json.t"
295-
| false -> "Js.Json.t");
295+
| false -> "JSON.t");
296296
] );
297297
prf_loc =
298298
(match emit_locations with
@@ -631,7 +631,7 @@ let rec generate_arg_type ?(nulls = true) raw originalLoc =
631631
base_type ?loc
632632
(match Ppx_config.native () with
633633
| true -> "Graphql_ppx_runtime.Json.t"
634-
| false -> "Js.Json.t")
634+
| false -> "JSON.t")
635635
| Type (Enum enum_meta) ->
636636
if raw then base_type "string"
637637
else
@@ -662,7 +662,7 @@ let rec generate_arg_type ?(nulls = true) raw originalLoc =
662662
| true ->
663663
base_type ?loc
664664
~inner:[ generate_arg_type raw (conv_loc_from_ast Location.none) inner ]
665-
(match raw with true -> "Js.Nullable.t" | false -> "option")
665+
(match raw with true -> "Nullable.t" | false -> "option")
666666
| false -> generate_arg_type raw (conv_loc_from_ast Location.none) inner)
667667
| List inner ->
668668
base_type ?loc

0 commit comments

Comments
 (0)