Skip to content

Commit

Permalink
add experimental non React mode to the PPX
Browse files Browse the repository at this point in the history
  • Loading branch information
zth committed Dec 5, 2024
1 parent 78c505a commit 89094ab
Show file tree
Hide file tree
Showing 7 changed files with 156 additions and 121 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

- **Upgrade versions**: `react-relay` and `relay-runtime` to `18.2.0`.
- Add support for `Fragment.waitForFragmentData`, a new API in Relay 18.2 that lets you wait for fragment data outside of React.
- Experimental: Add a "non React" mode to the PPX, which makes sure only APIs that don't rely on React directly are exposed. This is intended to be a way to simplify using RescriptRelay without React. Activate by passing `-non-react` to the PPX, like `"ppx-flags": [["rescript-relay/ppx", "-non-react"]]`.

# 3.0.1

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,9 @@ let () =
(Arg.Unit (fun () -> RescriptRelayPpxLibrary.UncurriedUtils.enabled := true))
~doc:"Run in uncurried mode"

let () =
Driver.add_arg "-non-react"
(Arg.Unit (fun () -> RescriptRelayPpxLibrary.NonReactUtils.enabled := true))
~doc:"Run non-React mode"

let _ = Driver.run_as_ppx_rewriter ()
46 changes: 28 additions & 18 deletions packages/rescript-relay/rescript-relay-ppx/library/Fragment.ml
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,28 @@ let make ~loc ~moduleName ~refetchableQueryName ~extractedConnectionInfo
[%t typeFromGeneratedModule ["Types"; "fragment"]] =
[%e valFromGeneratedModule ["Internal"; "convertFragment"]]];
];
(match hasAutocodesplitDirective with
| true ->
(match (!NonReactUtils.enabled, hasAutocodesplitDirective) with
| true, _ -> []
| false, true ->
[
[%stri
module CodesplitComponents =
[%m
moduleIdentFromGeneratedModule ["CodesplitComponents"]]];
]
| false -> []);
(match hasInlineDirective with
| false ->
| false, false -> []);
[
[%stri
let waitForFragmentData ~environment fRef =
RescriptRelay_Fragment.waitForFragmentData ~environment
~convertFragment
~fRef:
(fRef |. [%e valFromGeneratedModule ["getFragmentRef"]])
~node:[%e valFromGeneratedModule ["node"]]];
];
(match (!NonReactUtils.enabled, hasInlineDirective) with
| true, _ -> []
| false, false ->
[
[%stri
let use fRef :
Expand All @@ -42,13 +53,6 @@ let make ~loc ~moduleName ~refetchableQueryName ~extractedConnectionInfo
~fRef:
(fRef |. [%e valFromGeneratedModule ["getFragmentRef"]])
~node:[%e valFromGeneratedModule ["node"]]];
[%stri
let waitForFragmentData ~environment fRef =
RescriptRelay_Fragment.waitForFragmentData ~environment
~convertFragment
~fRef:
(fRef |. [%e valFromGeneratedModule ["getFragmentRef"]])
~node:[%e valFromGeneratedModule ["node"]]];
[%stri
let useOpt fRef :
[%t typeFromGeneratedModule ["Types"; "fragment"]] option
Expand All @@ -62,6 +66,11 @@ let make ~loc ~moduleName ~refetchableQueryName ~extractedConnectionInfo
|. [%e valFromGeneratedModule ["getFragmentRef"]])
| None -> None)
~node:[%e valFromGeneratedModule ["node"]]];
]
| false, true -> []);
(match hasInlineDirective with
| false ->
[
(if isPlural then
[%stri
let readResolverFragment fRef :
Expand Down Expand Up @@ -92,8 +101,9 @@ let make ~loc ~moduleName ~refetchableQueryName ~extractedConnectionInfo
~node:[%e valFromGeneratedModule ["node"]]]);
]
| true -> []);
(match hasInlineDirective with
| true ->
(match (!NonReactUtils.enabled, hasInlineDirective) with
| true, _ -> []
| false, true ->
[
[%stri
let readInline fRef :
Expand All @@ -103,10 +113,10 @@ let make ~loc ~moduleName ~refetchableQueryName ~extractedConnectionInfo
(fRef |. [%e valFromGeneratedModule ["getFragmentRef"]])
~node:[%e valFromGeneratedModule ["node"]]];
]
| false -> []);
(match refetchableQueryName with
| None -> []
| Some refetchableQueryName ->
| false, false -> []);
(match (!NonReactUtils.enabled, refetchableQueryName) with
| true, _ | false, None -> []
| false, Some refetchableQueryName ->
let typeFromRefetchableModule =
makeTypeAccessor ~loc ~moduleName:refetchableQueryName
in
Expand Down
73 changes: 39 additions & 34 deletions packages/rescript-relay/rescript-relay-ppx/library/Mutation.ml
Original file line number Diff line number Diff line change
Expand Up @@ -9,39 +9,44 @@ let make ~loc ~moduleName =
(Pmod_structure
(List.concat
[
[
[%stri [@@@ocaml.warning "-32-34-60"]];
[%stri include [%m moduleIdentFromGeneratedModule ["Utils"]]];
[%stri module Operation = [%m moduleIdentFromGeneratedModule []]];
[%stri
module Types = [%m moduleIdentFromGeneratedModule ["Types"]]];
[%stri
let convertVariables :
[%t typeFromGeneratedModule ["Types"; "variables"]] ->
[%t typeFromGeneratedModule ["Types"; "variables"]] =
[%e valFromGeneratedModule ["Internal"; "convertVariables"]]];
[%stri
let convertResponse :
[%t typeFromGeneratedModule ["Types"; "response"]] ->
[%t typeFromGeneratedModule ["Types"; "response"]] =
[%e valFromGeneratedModule ["Internal"; "convertResponse"]]];
[%stri
let convertWrapRawResponse :
[%t typeFromGeneratedModule ["Types"; "rawResponse"]] ->
[%t typeFromGeneratedModule ["Types"; "rawResponse"]] =
[%e
valFromGeneratedModule
["Internal"; "convertWrapRawResponse"]]];
[%stri
let commitMutation =
RescriptRelay_Mutation.commitMutation ~convertVariables
~convertResponse ~convertWrapRawResponse
~node:[%e valFromGeneratedModule ["node"]]];
[%stri
let use =
RescriptRelay_Mutation.useMutation ~convertVariables
~convertResponse ~convertWrapRawResponse
~node:[%e valFromGeneratedModule ["node"]]];
];
([
[%stri [@@@ocaml.warning "-32-34-60"]];
[%stri include [%m moduleIdentFromGeneratedModule ["Utils"]]];
[%stri module Operation = [%m moduleIdentFromGeneratedModule []]];
[%stri
module Types = [%m moduleIdentFromGeneratedModule ["Types"]]];
[%stri
let convertVariables :
[%t typeFromGeneratedModule ["Types"; "variables"]] ->
[%t typeFromGeneratedModule ["Types"; "variables"]] =
[%e valFromGeneratedModule ["Internal"; "convertVariables"]]];
[%stri
let convertResponse :
[%t typeFromGeneratedModule ["Types"; "response"]] ->
[%t typeFromGeneratedModule ["Types"; "response"]] =
[%e valFromGeneratedModule ["Internal"; "convertResponse"]]];
[%stri
let convertWrapRawResponse :
[%t typeFromGeneratedModule ["Types"; "rawResponse"]] ->
[%t typeFromGeneratedModule ["Types"; "rawResponse"]] =
[%e
valFromGeneratedModule
["Internal"; "convertWrapRawResponse"]]];
[%stri
let commitMutation =
RescriptRelay_Mutation.commitMutation ~convertVariables
~convertResponse ~convertWrapRawResponse
~node:[%e valFromGeneratedModule ["node"]]];
]
@
if not !NonReactUtils.enabled then
[
[%stri
let use =
RescriptRelay_Mutation.useMutation ~convertVariables
~convertResponse ~convertWrapRawResponse
~node:[%e valFromGeneratedModule ["node"]]];
]
else []);
]
|> List.map UncurriedUtils.mapStructureItem))
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
let enabled = ref false
150 changes: 81 additions & 69 deletions packages/rescript-relay/rescript-relay-ppx/library/Query.ml
Original file line number Diff line number Diff line change
Expand Up @@ -9,81 +9,93 @@ let make ~loc ~moduleName ~hasRawResponseType ~hasAutocodesplitDirective =
(Pmod_structure
(List.concat
[
[
[%stri [@@@ocaml.warning "-32-34-60"]];
[%stri include [%m moduleIdentFromGeneratedModule ["Utils"]]];
[%stri module Operation = [%m moduleIdentFromGeneratedModule []]];
[%stri
module Types = [%m moduleIdentFromGeneratedModule ["Types"]]];
[%stri
let convertVariables :
[%t typeFromGeneratedModule ["Types"; "variables"]] ->
[%t typeFromGeneratedModule ["Types"; "variables"]] =
[%e valFromGeneratedModule ["Internal"; "convertVariables"]]];
[%stri
let convertResponse :
[%t typeFromGeneratedModule ["Types"; "response"]] ->
[%t typeFromGeneratedModule ["Types"; "response"]] =
[%e valFromGeneratedModule ["Internal"; "convertResponse"]]];
[%stri
let convertWrapRawResponse :
[%t typeFromGeneratedModule ["Types"; "rawResponse"]] ->
[%t typeFromGeneratedModule ["Types"; "rawResponse"]] =
[%e
valFromGeneratedModule
["Internal"; "convertWrapRawResponse"]]];
[%stri
external mkQueryRefOpt :
[%t typeFromGeneratedModule ["queryRef"]] option ->
[%t typeFromGeneratedModule ["queryRef"]] option = "%identity"];
[%stri
external mkQueryRef :
[%t typeFromGeneratedModule ["queryRef"]] ->
[%t typeFromGeneratedModule ["queryRef"]] = "%identity"];
[%stri
let use =
RescriptRelay_Query.useQuery ~convertVariables
~convertResponse
~node:[%e valFromGeneratedModule ["node"]]];
[%stri
let useLoader =
RescriptRelay_Query.useLoader ~convertVariables
~mkQueryRef:mkQueryRefOpt
~node:[%e valFromGeneratedModule ["node"]]];
[%stri
let usePreloaded =
RescriptRelay_Query.usePreloaded ~convertResponse ~mkQueryRef
~node:[%e valFromGeneratedModule ["node"]]];
[%stri
let fetch =
RescriptRelay_Query.fetch ~convertResponse ~convertVariables
~node:[%e valFromGeneratedModule ["node"]]];
[%stri
let fetchPromised =
RescriptRelay_Query.fetchPromised ~convertResponse
~convertVariables
~node:[%e valFromGeneratedModule ["node"]]];
[%stri
let retain =
RescriptRelay_Query.retain ~convertVariables
~node:[%e valFromGeneratedModule ["node"]]];
(match hasRawResponseType with
| true ->
([
[%stri [@@@ocaml.warning "-32-34-60"]];
[%stri include [%m moduleIdentFromGeneratedModule ["Utils"]]];
[%stri module Operation = [%m moduleIdentFromGeneratedModule []]];
[%stri
module Types = [%m moduleIdentFromGeneratedModule ["Types"]]];
[%stri
let convertVariables :
[%t typeFromGeneratedModule ["Types"; "variables"]] ->
[%t typeFromGeneratedModule ["Types"; "variables"]] =
[%e valFromGeneratedModule ["Internal"; "convertVariables"]]];
[%stri
let convertResponse :
[%t typeFromGeneratedModule ["Types"; "response"]] ->
[%t typeFromGeneratedModule ["Types"; "response"]] =
[%e valFromGeneratedModule ["Internal"; "convertResponse"]]];
[%stri
let convertWrapRawResponse :
[%t typeFromGeneratedModule ["Types"; "rawResponse"]] ->
[%t typeFromGeneratedModule ["Types"; "rawResponse"]] =
[%e
valFromGeneratedModule
["Internal"; "convertWrapRawResponse"]]];
[%stri
external mkQueryRefOpt :
[%t typeFromGeneratedModule ["queryRef"]] option ->
[%t typeFromGeneratedModule ["queryRef"]] option
= "%identity"];
[%stri
external mkQueryRef :
[%t typeFromGeneratedModule ["queryRef"]] ->
[%t typeFromGeneratedModule ["queryRef"]] = "%identity"];
]
@
if not !NonReactUtils.enabled then
[
[%stri
let use =
RescriptRelay_Query.useQuery ~convertVariables
~convertResponse
~node:[%e valFromGeneratedModule ["node"]]];
[%stri
let commitLocalPayload =
RescriptRelay_Query.commitLocalPayload ~convertVariables
~convertWrapRawResponse
~node:[%e valFromGeneratedModule ["node"]]]
| false -> [%stri ()]);
];
(match hasAutocodesplitDirective with
| true ->
let useLoader =
RescriptRelay_Query.useLoader ~convertVariables
~mkQueryRef:mkQueryRefOpt
~node:[%e valFromGeneratedModule ["node"]]];
[%stri
let usePreloaded =
RescriptRelay_Query.usePreloaded ~convertResponse
~mkQueryRef
~node:[%e valFromGeneratedModule ["node"]]];
]
else
[]
@ [
[%stri
let fetch =
RescriptRelay_Query.fetch ~convertResponse
~convertVariables
~node:[%e valFromGeneratedModule ["node"]]];
[%stri
let fetchPromised =
RescriptRelay_Query.fetchPromised ~convertResponse
~convertVariables
~node:[%e valFromGeneratedModule ["node"]]];
[%stri
let retain =
RescriptRelay_Query.retain ~convertVariables
~node:[%e valFromGeneratedModule ["node"]]];
(match hasRawResponseType with
| true ->
[%stri
let commitLocalPayload =
RescriptRelay_Query.commitLocalPayload ~convertVariables
~convertWrapRawResponse
~node:[%e valFromGeneratedModule ["node"]]]
| false -> [%stri ()]);
]);
(match (!NonReactUtils.enabled, hasAutocodesplitDirective) with
| true, _ -> []
| false, true ->
[
[%stri
module CodesplitComponents =
[%m
moduleIdentFromGeneratedModule ["CodesplitComponents"]]];
]
| false -> []);
| false, false -> []);
]
|> List.map UncurriedUtils.mapStructureItem))
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ open Ppxlib
open Util
module Util = Util
module UncurriedUtils = UncurriedUtils
module NonReactUtils = NonReactUtils
let endsWithRegexp = Str.regexp ".*Resolver$"
let commonExtension =
Extension.declare "relay" Extension.Context.module_expr
Expand Down

0 comments on commit 89094ab

Please sign in to comment.