zth / rescript-relay

Use Relay with ReScript.
https://rescript-relay-documentation.vercel.app/docs/getting-started
339 stars 53 forks source link

The relay_resolver.error support #478

Closed DZakh closed 8 months ago

DZakh commented 10 months ago

I've used the pnpm patch for now, since we use rescript-relay V2 anyways. But maybe it may be added to V3:

/src/RescriptRelay.res
@@ -587,16 +587,18 @@ module Store = {
 }

 module RequiredFieldLogger = {
-  type kind = [#"missing_field.log" | #"missing_field.throw"]
-
-  type arg = {"kind": kind, "owner": string, "fieldPath": string}
-
-  type js = arg => unit
-
-  type t = (~kind: kind, ~owner: string, ~fieldPath: string) => unit
-
-  let toJs: t => js = f => arg =>
-    f(~kind=arg["kind"], ~owner=arg["owner"], ~fieldPath=arg["fieldPath"])
+  @tag("kind")
+  type arg =
+    | @as("missing_field.log") MissingFieldLog({owner: string, fieldPath: string})
+    | @as("missing_field.throw") MissingFieldThrow({owner: string, fieldPath: string})
+    | @as("relay_resolver.error")
+    RelayResolverError({
+        owner: string,
+        fieldPath: string,
+        error: Js.Exn.t,
+      })
+
+  type t = arg => unit
 }

 module Environment = {
@@ -612,7 +614,7 @@ module Environment = {
     treatMissingFieldsAsNull: bool,
     missingFieldHandlers: array<MissingFieldHandler.t>,
     @optional
-    requiredFieldLogger: RequiredFieldLogger.js,
+    requiredFieldLogger: RequiredFieldLogger.t,
     @optional
     isServer: bool,
   }
@@ -640,7 +642,7 @@ module Environment = {
         | Some(handlers) => handlers->Belt.Array.concat([nodeInterfaceMissingFieldHandler])
         | None => [nodeInterfaceMissingFieldHandler]
         },
-        ~requiredFieldLogger=?requiredFieldLogger->Belt.Option.map(RequiredFieldLogger.toJs),
+        ~requiredFieldLogger?,
         ~isServer?,
         (),
       ),

/src/RescriptRelay.resi
@@ -729,10 +729,19 @@ module Disposable: {

 /**A required field logger, which gets called when a field annotated with the @required directive was missing from the response*/
 module RequiredFieldLogger: {
-  type kind = [#"missing_field.log" | #"missing_field.throw"]
+  @tag("kind")
+  type arg =
+    | @as("missing_field.log") MissingFieldLog({owner: string, fieldPath: string})
+    | @as("missing_field.throw") MissingFieldThrow({owner: string, fieldPath: string})
+    | @as("relay_resolver.error")
+    RelayResolverError({
+        owner: string,
+        fieldPath: string,
+        error: Js.Exn.t,
+      })

   /**A required field logger, which gets called when a field annotated with the @required directive was missing from the response*/
-  type t = (~kind: kind, ~owner: string, ~fieldPath: string) => unit
+  type t = arg => unit
 }
zth commented 10 months ago

PR definitely welcome!

zth commented 9 months ago

@DZakh PR? ;)

DZakh commented 9 months ago

We are still on V2, but I can prepare it during the week