Skip to content

Commit 66f03b0

Browse files
committed
updates for latest
1 parent 934acbc commit 66f03b0

File tree

4 files changed

+54
-51
lines changed

4 files changed

+54
-51
lines changed

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

Lines changed: 33 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ 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, RequestId } from "@effect-app/infra/RequestContext"
1010
import { RequestContextContainer } from "@effect-app/infra/services/RequestContextContainer"
1111
import type { CTX } from "api/lib/routing.js"
1212
import BlogControllers from "./Blog.Controllers.js"
@@ -25,24 +25,26 @@ function request<Key extends string>(
2525
) => {
2626
const q = req[name]
2727
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-
))
28+
? Effect
29+
.gen(function*($) {
30+
yield* $(RequestContextContainer.flatMap((_) =>
31+
_.update((ctx) =>
32+
RequestContext.inherit(ctx, {
33+
id: RequestId.make(),
34+
locale: ctx.locale,
35+
name: ReasonableString(name) // TODO: Use name from handler.Request
36+
})
37+
)
38+
))
3839

39-
const ctx = yield* $(RequestContextContainer.get)
40+
const ctx = yield* $(RequestContextContainer.get)
4041

41-
const r = yield* $(
42-
handler(q.input ?? {}, { ...context, context: ctx })
43-
)
44-
return r
45-
})["|>"](Effect.either)
42+
const r = yield* $(
43+
handler(q.input ?? {}, { ...context, context: ctx })
44+
)
45+
return r
46+
})
47+
["|>"](Effect.either)
4648
: NoResponse
4749
}
4850
}
@@ -89,17 +91,20 @@ function mutation<Key extends string>(
8991
resultQuery?: (inp: A, ctx: CTX) => Effect<R2, E2, A2>
9092
) => {
9193
const q = req[name]
92-
return f(name, handler).flatMap(x =>
94+
return f(name, handler).flatMap((x) =>
9395
!x
9496
? Effect(x)
9597
: x.isLeft()
96-
? Effect(x)
98+
? Effect(Either.left(x.left))
9799
: (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 }))
100+
? Effect
101+
.allPar({
102+
query: Query.flatMap((_) => _(q.query!, ctx)),
103+
result: resultQuery ? resultQuery(x.right, ctx) : emptyResponse
104+
})
105+
.map(({ query, result }) => ({ ...query, result })) // TODO: Replace $ variables in the query parameters baed on mutation output!
106+
: emptyResponse)
107+
.map((query) => Either(query ? { query, response: x.right } : { response: x.right }))
103108
)
104109
}
105110
}
@@ -116,8 +121,10 @@ const Mutation = graph.matchMutation.withEffect(
116121
"CreatePost",
117122
blogPostControllers.CreatePost.h,
118123
(id, ctx) =>
119-
blogPostControllers.FindPost.h({ id }, ctx)
120-
.flatMap(x => (!x ? Effect.die("Post went away?") : Effect(x)))
124+
blogPostControllers
125+
.FindPost
126+
.h({ id }, ctx)
127+
.flatMap((x) => (!x ? Effect.die("Post went away?") : Effect(x)))
121128
)
122129
// UpdatePurchaseOrder: handle("UpdatePurchaseOrder", UpdatePurchaseOrder.h, () =>
123130
// 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)