diff --git a/CHANGELOG.md b/CHANGELOG.md index e6a05a4b..227f0415 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/packages/rescript-relay/rescript-relay-ppx/bin/RescriptRelayPpxApp.ml b/packages/rescript-relay/rescript-relay-ppx/bin/RescriptRelayPpxApp.ml index 94650712..c2ed3e75 100755 --- a/packages/rescript-relay/rescript-relay-ppx/bin/RescriptRelayPpxApp.ml +++ b/packages/rescript-relay/rescript-relay-ppx/bin/RescriptRelayPpxApp.ml @@ -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 () diff --git a/packages/rescript-relay/rescript-relay-ppx/library/Fragment.ml b/packages/rescript-relay/rescript-relay-ppx/library/Fragment.ml index db428a39..13c12bad 100644 --- a/packages/rescript-relay/rescript-relay-ppx/library/Fragment.ml +++ b/packages/rescript-relay/rescript-relay-ppx/library/Fragment.ml @@ -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 : @@ -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 @@ -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 : @@ -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 : @@ -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 diff --git a/packages/rescript-relay/rescript-relay-ppx/library/Mutation.ml b/packages/rescript-relay/rescript-relay-ppx/library/Mutation.ml index 01e6931a..ef69f805 100644 --- a/packages/rescript-relay/rescript-relay-ppx/library/Mutation.ml +++ b/packages/rescript-relay/rescript-relay-ppx/library/Mutation.ml @@ -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)) diff --git a/packages/rescript-relay/rescript-relay-ppx/library/NonReactUtils.ml b/packages/rescript-relay/rescript-relay-ppx/library/NonReactUtils.ml new file mode 100644 index 00000000..40925dc6 --- /dev/null +++ b/packages/rescript-relay/rescript-relay-ppx/library/NonReactUtils.ml @@ -0,0 +1 @@ +let enabled = ref false diff --git a/packages/rescript-relay/rescript-relay-ppx/library/Query.ml b/packages/rescript-relay/rescript-relay-ppx/library/Query.ml index 43819151..a71d4262 100644 --- a/packages/rescript-relay/rescript-relay-ppx/library/Query.ml +++ b/packages/rescript-relay/rescript-relay-ppx/library/Query.ml @@ -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)) diff --git a/packages/rescript-relay/rescript-relay-ppx/library/RescriptRelayPpxLibrary.ml b/packages/rescript-relay/rescript-relay-ppx/library/RescriptRelayPpxLibrary.ml index 3c732002..86b53e60 100644 --- a/packages/rescript-relay/rescript-relay-ppx/library/RescriptRelayPpxLibrary.ml +++ b/packages/rescript-relay/rescript-relay-ppx/library/RescriptRelayPpxLibrary.ml @@ -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