Skip to content

Commit fb16f9a

Browse files
committed
updates for latest
1 parent b78fb2d commit fb16f9a

File tree

4 files changed

+55
-51
lines changed

4 files changed

+55
-51
lines changed

_project/api/_src/Usecases/Graph.Controllers.ts

Lines changed: 34 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@ import { GraphRsc } from "@effect-app-boilerplate/resources"
66
import type { GraphMutationResponse } from "@effect-app-boilerplate/resources/Graph/Mutation"
77
import type { GraphQueryRequest, GraphQueryResponse } from "@effect-app-boilerplate/resources/Graph/Query"
88
import { dropUndefined } from "@effect-app/core/utils"
9-
import { makeRequestId, RequestContext } from "@effect-app/infra/RequestContext"
9+
import { RequestContext } from "@effect-app/infra/RequestContext"
1010
import { RequestContextContainer } from "@effect-app/infra/services/RequestContextContainer"
11+
import { RequestId } from "@effect-app/prelude/ids"
1112
import type { CTX } from "api/lib/routing.js"
1213
import 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 =>

_project/resources/_src/Graph/Mutation.ts

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,8 @@ const makeMutationInput_ = makeMutationInput(GraphQueryRequest.Api.props)
1414
@allowRoles("user")
1515
export class GraphMutationRequest extends Post("/graph/mutate")<GraphMutationRequest>()(
1616
{
17-
CreatePost: optProp(
18-
makeMutationInput_(CreatePost.CreatePostRequest)
19-
)
17+
CreatePost: makeMutationInput_(CreatePost.CreatePostRequest)
18+
.optional
2019
// UpdatePurchaseOrder: optProp(
2120
// makeMutationInput_(PurchaseOrders.Update.UpdatePurchaseOrderRequest)
2221
// )
@@ -25,29 +24,28 @@ export class GraphMutationRequest extends Post("/graph/mutate")<GraphMutationReq
2524

2625
const PostResult = props({
2726
...GraphQueryResponse.Api.props,
28-
result: optProp(BlogPost)
27+
result: BlogPost.optional
2928
})
3029

3130
@useClassFeaturesForSchema
3231
export class GraphMutationResponse extends Model<GraphMutationResponse>()({
3332
// TODO: Support guaranteed optional sub-queries, like on Create/Update of PO
3433
// guarantee an optional return of PurchaseOrder
3534
// first must enable PO cache for guarantee.
36-
CreatePost: optProp(
37-
either(
38-
MutationErrors,
39-
props({
40-
response: prop(CreatePost.CreatePostResponse),
41-
query: optProp(PostResult)
42-
})
43-
)
35+
CreatePost: either(
36+
MutationErrors,
37+
props({
38+
response: CreatePost.CreatePostResponse,
39+
query: PostResult.optional
40+
})
4441
)
42+
.optional
4543
// UpdatePurchaseOrder: optProp(
4644
// either(
4745
// MutationErrors,
4846
// props({
49-
// response: optProp(Void),
50-
// query: optProp(POResult)
47+
// response: Void.optional,
48+
// query: POResult.optional
5149
// })
5250
// )
5351
// )

_project/resources/_src/Graph/Query.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { either } from "@effect-app/prelude/schema"
2+
import * as BlogRsc from "../Blog.js"
23
import { QueryErrors } from "../errors.js"
3-
import { BlogRsc } from "../index.js"
44
import { makeInput } from "./utils.js"
55

66
@useClassFeaturesForSchema
@@ -17,8 +17,8 @@ export class GraphQueryRequest extends Post("/graph/query")<GraphQueryRequest>()
1717
// ),
1818
// AllMeTasks: optProp(makeInput(Me.Tasks.AllMeTasksRequest, true)),
1919

20-
FindBlogPost: optProp(makeInput(BlogRsc.FindPost.FindPostRequest)),
21-
GetAllBlogPosts: optProp(makeInput(BlogRsc.GetPosts.GetPostsRequest))
20+
FindBlogPost: makeInput(BlogRsc.FindPost.FindPostRequest).optional,
21+
GetAllBlogPosts: makeInput(BlogRsc.GetPosts.GetPostsRequest).optional
2222
}) {}
2323

2424
@useClassFeaturesForSchema
@@ -33,7 +33,5 @@ export class GraphQueryResponse extends Model<GraphQueryResponse>()({
3333
// ),
3434
// AllMeTasks: optProp(either(QueryErrors, Me.Tasks.AllMeTasksResponse)),
3535

36-
FindBlogPost: optProp(
37-
either(QueryErrors, BlogRsc.FindPost.FindPostResponse)
38-
)
36+
FindBlogPost: either(QueryErrors, BlogRsc.FindPost.FindPostResponse).optional
3937
}) {}

_project/resources/_src/Graph/utils.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,21 @@ import type { Property, PropertyRecord, SchemaAny, SchemaProperties } from "@eff
33
export function makeInput<Self extends SchemaAny>(
44
a: Self
55
): SchemaProperties<{
6-
input: Property<Self, "required", None, None>
6+
input: Property<Self, "required", None<any>, None<any>>
77
}>
88
export function makeInput<Self extends SchemaAny>(
99
a: Self,
1010
noInput: true
1111
): SchemaProperties<{
12-
input: Property<Self, "optional", None, None>
12+
input: Property<Self, "optional", None<any>, None<any>>
1313
}>
1414
export function makeInput<Self extends SchemaAny>(a: Self, noInput?: boolean): any {
15-
return props(noInput ? { input: optProp(a) } : { input: prop(a) })
15+
return props(noInput ? { input: a.optional } : { input: a })
1616
}
1717

1818
export function makeMutationInput<Props extends PropertyRecord>(baseSchemaProps: Props) {
1919
return <Self extends SchemaAny>(a: Self) => {
20-
const query = props({ ...baseSchemaProps, result: optProp(bool) })
21-
return props({ input: prop(a), query: optProp(query) })
20+
const query = props({ ...baseSchemaProps, result: bool.optional })
21+
return props({ input: a, query: query.optional })
2222
}
2323
}

0 commit comments

Comments
 (0)