@@ -6,8 +6,9 @@ import { GraphRsc } from "@effect-app-boilerplate/resources"
66import type { GraphMutationResponse } from "@effect-app-boilerplate/resources/Graph/Mutation"
77import type { GraphQueryRequest , GraphQueryResponse } from "@effect-app-boilerplate/resources/Graph/Query"
88import { dropUndefined } from "@effect-app/core/utils"
9- import { makeRequestId , RequestContext } from "@effect-app/infra/RequestContext"
9+ import { RequestContext } from "@effect-app/infra/RequestContext"
1010import { RequestContextContainer } from "@effect-app/infra/services/RequestContextContainer"
11+ import { RequestId } from "@effect-app/prelude/ids"
1112import type { CTX } from "api/lib/routing.js"
1213import BlogControllers from "./Blog.Controllers.js"
1314
@@ -25,24 +26,26 @@ function request<Key extends string>(
2526 ) => {
2627 const q = req [ name ]
2728 return q
28- ? Effect . gen ( function * ( $ ) {
29- yield * $ ( RequestContextContainer . flatMap ( _ =>
30- _ . update ( ctx =>
31- RequestContext . inherit ( ctx , {
32- id : makeRequestId ( ) ,
33- locale : ctx . locale ,
34- name : ReasonableString ( name ) // TODO: Use name from handler.Request
35- } )
36- )
37- ) )
29+ ? Effect
30+ . gen ( function * ( $ ) {
31+ yield * $ ( RequestContextContainer . flatMap ( ( _ ) =>
32+ _ . update ( ( ctx ) =>
33+ RequestContext . inherit ( ctx , {
34+ id : RequestId . make ( ) ,
35+ locale : ctx . locale ,
36+ name : ReasonableString ( name ) // TODO: Use name from handler.Request
37+ } )
38+ )
39+ ) )
3840
39- const ctx = yield * $ ( RequestContextContainer . get )
41+ const ctx = yield * $ ( RequestContextContainer . get )
4042
41- const r = yield * $ (
42- handler ( q . input ?? { } , { ...context , context : ctx } )
43- )
44- return r
45- } ) [ "|>" ] ( Effect . either )
43+ const r = yield * $ (
44+ handler ( q . input ?? { } , { ...context , context : ctx } )
45+ )
46+ return r
47+ } )
48+ [ "|>" ] ( Effect . either )
4649 : NoResponse
4750 }
4851}
@@ -89,17 +92,20 @@ function mutation<Key extends string>(
8992 resultQuery ?: ( inp : A , ctx : CTX ) => Effect < R2 , E2 , A2 >
9093 ) => {
9194 const q = req [ name ]
92- return f ( name , handler ) . flatMap ( x =>
95+ return f ( name , handler ) . flatMap ( ( x ) =>
9396 ! x
9497 ? Effect ( x )
9598 : x . isLeft ( )
96- ? Effect ( x )
99+ ? Effect ( Either . left ( x . left ) )
97100 : ( q ?. query
98- ? Effect . allPar ( {
99- query : Query . flatMap ( _ => _ ( q . query ! , ctx ) ) ,
100- result : resultQuery ? resultQuery ( x . right , ctx ) : emptyResponse
101- } ) . map ( ( { query, result } ) => ( { ...query , result } ) ) // TODO: Replace $ variables in the query parameters baed on mutation output!
102- : emptyResponse ) . map ( query => Either ( query ? { query, response : x . right } : { response : x . right } ) )
101+ ? Effect
102+ . allPar ( {
103+ query : Query . flatMap ( ( _ ) => _ ( q . query ! , ctx ) ) ,
104+ result : resultQuery ? resultQuery ( x . right , ctx ) : emptyResponse
105+ } )
106+ . map ( ( { query, result } ) => ( { ...query , result } ) ) // TODO: Replace $ variables in the query parameters baed on mutation output!
107+ : emptyResponse )
108+ . map ( ( query ) => Either ( query ? { query, response : x . right } : { response : x . right } ) )
103109 )
104110 }
105111}
@@ -116,8 +122,10 @@ const Mutation = graph.matchMutation.withEffect(
116122 "CreatePost" ,
117123 blogPostControllers . CreatePost . h ,
118124 ( id , ctx ) =>
119- blogPostControllers . FindPost . h ( { id } , ctx )
120- . flatMap ( x => ( ! x ? Effect . die ( "Post went away?" ) : Effect ( x ) ) )
125+ blogPostControllers
126+ . FindPost
127+ . h ( { id } , ctx )
128+ . flatMap ( ( x ) => ( ! x ? Effect . die ( "Post went away?" ) : Effect ( x ) ) )
121129 )
122130 // UpdatePurchaseOrder: handle("UpdatePurchaseOrder", UpdatePurchaseOrder.h, () =>
123131 // FindPurchaseOrder.h(req.UpdatePurchaseOrder!.input).flatMap(x =>
0 commit comments