-
Notifications
You must be signed in to change notification settings - Fork 165
Expand file tree
/
Copy pathhandlers.ts
More file actions
89 lines (82 loc) · 3.33 KB
/
Copy pathhandlers.ts
File metadata and controls
89 lines (82 loc) · 3.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import { HttpApiBuilder } from "effect/unstable/httpapi";
import { Context, Effect } from "effect";
import { addGroup, capture } from "@executor-js/api";
import type { GraphqlPluginExtension } from "../sdk/plugin";
import { GraphqlGroup } from "./group";
// ---------------------------------------------------------------------------
// Service tag
//
// Holds the plugin's extension. The host app provides an already-wrapped
// extension via `Layer.succeed(GraphqlExtensionService, executor.graphql)`.
// Handlers see plugin SDK errors in the typed channel (matched by
// `.addError(...)` on the group) and `InternalError` for `StorageError`
// translated by `capture` at the HTTP edge.
// ---------------------------------------------------------------------------
export class GraphqlExtensionService extends Context.Service<
GraphqlExtensionService,
GraphqlPluginExtension
>()("GraphqlExtensionService") {}
// ---------------------------------------------------------------------------
// Composed API — core + graphql group
// ---------------------------------------------------------------------------
const ExecutorApiWithGraphql = addGroup(GraphqlGroup);
// ---------------------------------------------------------------------------
// Handlers
// ---------------------------------------------------------------------------
export const GraphqlHandlers = HttpApiBuilder.group(ExecutorApiWithGraphql, "graphql", (handlers) =>
handlers
.handle("addIntegration", ({ payload }) =>
capture(
Effect.gen(function* () {
const ext = yield* GraphqlExtensionService;
const result = yield* ext.addIntegration({
endpoint: payload.endpoint,
slug: payload.slug,
name: payload.name,
introspectionJson: payload.introspectionJson,
headers: payload.headers,
queryParams: payload.queryParams,
authenticationTemplate: payload.authenticationTemplate,
});
return { slug: result.slug, name: result.name };
}),
),
)
.handle("getIntegration", ({ params: path }) =>
capture(
Effect.gen(function* () {
const ext = yield* GraphqlExtensionService;
return yield* ext.getIntegration(path.slug);
}),
),
)
.handle("getConfig", ({ params: path }) =>
capture(
Effect.gen(function* () {
const ext = yield* GraphqlExtensionService;
const config = yield* ext.getConfig(path.slug);
return config
? {
endpoint: config.endpoint,
name: config.name,
headers: config.headers ? { ...config.headers } : undefined,
queryParams: config.queryParams ? { ...config.queryParams } : undefined,
authenticationTemplate: [...config.authenticationTemplate],
}
: null;
}),
),
)
.handle("configure", ({ params: path, payload }) =>
capture(
Effect.gen(function* () {
const ext = yield* GraphqlExtensionService;
const authenticationTemplate = yield* ext.configureAuth(path.slug, {
authenticationTemplate: payload.authenticationTemplate,
mode: payload.mode ?? "merge",
});
return { authenticationTemplate: [...authenticationTemplate] };
}),
),
),
);