effect 4.0.2 → 4.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/AGENTS.md +24 -0
- package/CLAUDE.md +24 -0
- package/ai-docs/package.json +2 -2
- package/ai-docs/src/52_graphql/10_basics.ts +111 -0
- package/ai-docs/src/52_graphql/20_testing.ts +45 -0
- package/ai-docs/src/52_graphql/30_batching.ts +116 -0
- package/ai-docs/src/52_graphql/40_aliased-batching.ts +122 -0
- package/ai-docs/src/52_graphql/50_errors-and-retries.ts +74 -0
- package/ai-docs/src/52_graphql/index.md +6 -0
- package/dist/Context.d.ts.map +1 -1
- package/dist/Context.js +12 -3
- package/dist/Context.js.map +1 -1
- package/dist/Crypto.d.ts +33 -5
- package/dist/Crypto.d.ts.map +1 -1
- package/dist/Crypto.js +8 -2
- package/dist/Crypto.js.map +1 -1
- package/dist/DateTime.d.ts +9 -6
- package/dist/DateTime.d.ts.map +1 -1
- package/dist/DateTime.js +9 -6
- package/dist/DateTime.js.map +1 -1
- package/dist/Duration.d.ts +6 -6
- package/dist/Duration.d.ts.map +1 -1
- package/dist/Duration.js +27 -8
- package/dist/Duration.js.map +1 -1
- package/dist/FileSystem.d.ts +44 -2
- package/dist/FileSystem.d.ts.map +1 -1
- package/dist/FileSystem.js.map +1 -1
- package/dist/RequestResolver.d.ts.map +1 -1
- package/dist/RequestResolver.js +4 -10
- package/dist/RequestResolver.js.map +1 -1
- package/dist/Scheduler.d.ts +9 -0
- package/dist/Scheduler.d.ts.map +1 -1
- package/dist/Scheduler.js +9 -0
- package/dist/Scheduler.js.map +1 -1
- package/dist/Schema.d.ts +481 -1
- package/dist/Schema.d.ts.map +1 -1
- package/dist/Schema.js +362 -4
- package/dist/Schema.js.map +1 -1
- package/dist/SchemaGetter.d.ts +4 -0
- package/dist/SchemaGetter.d.ts.map +1 -1
- package/dist/SchemaGetter.js +52 -33
- package/dist/SchemaGetter.js.map +1 -1
- package/dist/Sink.d.ts.map +1 -1
- package/dist/Sink.js +38 -17
- package/dist/Sink.js.map +1 -1
- package/dist/Stdio.d.ts +7 -1
- package/dist/Stdio.d.ts.map +1 -1
- package/dist/Stdio.js +1 -0
- package/dist/Stdio.js.map +1 -1
- package/dist/Stream.d.ts +22 -11
- package/dist/Stream.d.ts.map +1 -1
- package/dist/Stream.js +21 -6
- package/dist/Stream.js.map +1 -1
- package/dist/Utils.js +8 -3
- package/dist/Utils.js.map +1 -1
- package/dist/ai/AnthropicStructuredOutput.js +0 -2
- package/dist/ai/AnthropicStructuredOutput.js.map +1 -1
- package/dist/ai/Chat.d.ts +2 -2
- package/dist/ai/Chat.d.ts.map +1 -1
- package/dist/ai/LanguageModel.d.ts +38 -7
- package/dist/ai/LanguageModel.d.ts.map +1 -1
- package/dist/ai/LanguageModel.js +25 -7
- package/dist/ai/LanguageModel.js.map +1 -1
- package/dist/ai/McpServer.d.ts +61 -0
- package/dist/ai/McpServer.d.ts.map +1 -1
- package/dist/ai/McpServer.js +196 -44
- package/dist/ai/McpServer.js.map +1 -1
- package/dist/ai/Prompt.d.ts.map +1 -1
- package/dist/ai/Prompt.js +28 -5
- package/dist/ai/Prompt.js.map +1 -1
- package/dist/ai/Response.d.ts +4 -3
- package/dist/ai/Response.d.ts.map +1 -1
- package/dist/ai/Response.js.map +1 -1
- package/dist/ai/internal/mcpProtocol/v2026_07_28.js +5 -0
- package/dist/ai/internal/mcpProtocol/v2026_07_28.js.map +1 -1
- package/dist/ai/internal/mcpProtocol.js.map +1 -1
- package/dist/ai/internal/mcpRuntime.js +1 -0
- package/dist/ai/internal/mcpRuntime.js.map +1 -1
- package/dist/cli/CliOutput.d.ts +27 -0
- package/dist/cli/CliOutput.d.ts.map +1 -1
- package/dist/cli/CliOutput.js +33 -10
- package/dist/cli/CliOutput.js.map +1 -1
- package/dist/cli/Command.d.ts.map +1 -1
- package/dist/cli/Command.js +6 -3
- package/dist/cli/Command.js.map +1 -1
- package/dist/cli/Completions.d.ts +3 -3
- package/dist/cli/Completions.d.ts.map +1 -1
- package/dist/cli/Completions.js +8 -5
- package/dist/cli/Completions.js.map +1 -1
- package/dist/cli/GlobalFlag.d.ts +5 -4
- package/dist/cli/GlobalFlag.d.ts.map +1 -1
- package/dist/cli/GlobalFlag.js +3 -3
- package/dist/cli/GlobalFlag.js.map +1 -1
- package/dist/cli/Prompt.d.ts +17 -1
- package/dist/cli/Prompt.d.ts.map +1 -1
- package/dist/cli/Prompt.js +119 -114
- package/dist/cli/Prompt.js.map +1 -1
- package/dist/cli/internal/ansi.d.ts +0 -1
- package/dist/cli/internal/ansi.d.ts.map +1 -1
- package/dist/cli/internal/ansi.js +20 -3
- package/dist/cli/internal/ansi.js.map +1 -1
- package/dist/cli/internal/completions/powershell.d.ts +2 -0
- package/dist/cli/internal/completions/powershell.d.ts.map +1 -0
- package/dist/cli/internal/completions/powershell.js +273 -0
- package/dist/cli/internal/completions/powershell.js.map +1 -0
- package/dist/cli/internal/completions/zsh.js +3 -2
- package/dist/cli/internal/completions/zsh.js.map +1 -1
- package/dist/cli/internal/wizard.d.ts +3 -3
- package/dist/cli/internal/wizard.d.ts.map +1 -1
- package/dist/cli/internal/wizard.js +21 -18
- package/dist/cli/internal/wizard.js.map +1 -1
- package/dist/cluster/ClusterSchema.d.ts +28 -0
- package/dist/cluster/ClusterSchema.d.ts.map +1 -1
- package/dist/cluster/ClusterSchema.js +30 -0
- package/dist/cluster/ClusterSchema.js.map +1 -1
- package/dist/cluster/ClusterWorkflowEngine.d.ts.map +1 -1
- package/dist/cluster/ClusterWorkflowEngine.js +56 -26
- package/dist/cluster/ClusterWorkflowEngine.js.map +1 -1
- package/dist/cluster/MessageStorage.d.ts +18 -3
- package/dist/cluster/MessageStorage.d.ts.map +1 -1
- package/dist/cluster/MessageStorage.js +31 -11
- package/dist/cluster/MessageStorage.js.map +1 -1
- package/dist/cluster/Runners.d.ts.map +1 -1
- package/dist/cluster/Runners.js +8 -2
- package/dist/cluster/Runners.js.map +1 -1
- package/dist/cluster/Sharding.d.ts.map +1 -1
- package/dist/cluster/Sharding.js +187 -66
- package/dist/cluster/Sharding.js.map +1 -1
- package/dist/cluster/SqlMessageStorage.d.ts +59 -19
- package/dist/cluster/SqlMessageStorage.d.ts.map +1 -1
- package/dist/cluster/SqlMessageStorage.js +84 -55
- package/dist/cluster/SqlMessageStorage.js.map +1 -1
- package/dist/cluster/SqlRunnerStorage.d.ts +68 -19
- package/dist/cluster/SqlRunnerStorage.d.ts.map +1 -1
- package/dist/cluster/SqlRunnerStorage.js +191 -113
- package/dist/cluster/SqlRunnerStorage.js.map +1 -1
- package/dist/cluster/internal/entityManager.js +104 -37
- package/dist/cluster/internal/entityManager.js.map +1 -1
- package/dist/encoding/Sse.d.ts +6 -0
- package/dist/encoding/Sse.d.ts.map +1 -1
- package/dist/encoding/Sse.js +62 -70
- package/dist/encoding/Sse.js.map +1 -1
- package/dist/graphql/GraphQL.d.ts +349 -0
- package/dist/graphql/GraphQL.d.ts.map +1 -0
- package/dist/graphql/GraphQL.js +207 -0
- package/dist/graphql/GraphQL.js.map +1 -0
- package/dist/graphql/GraphQLClient.d.ts +562 -0
- package/dist/graphql/GraphQLClient.d.ts.map +1 -0
- package/dist/graphql/GraphQLClient.js +498 -0
- package/dist/graphql/GraphQLClient.js.map +1 -0
- package/dist/graphql/GraphQLClientError.d.ts +445 -0
- package/dist/graphql/GraphQLClientError.d.ts.map +1 -0
- package/dist/graphql/GraphQLClientError.js +479 -0
- package/dist/graphql/GraphQLClientError.js.map +1 -0
- package/dist/graphql/GraphQLGroup.d.ts +118 -0
- package/dist/graphql/GraphQLGroup.d.ts.map +1 -0
- package/dist/graphql/GraphQLGroup.js +109 -0
- package/dist/graphql/GraphQLGroup.js.map +1 -0
- package/dist/graphql/GraphQLMiddleware.d.ts +249 -0
- package/dist/graphql/GraphQLMiddleware.d.ts.map +1 -0
- package/dist/graphql/GraphQLMiddleware.js +130 -0
- package/dist/graphql/GraphQLMiddleware.js.map +1 -0
- package/dist/graphql/GraphQLProtocol.d.ts +731 -0
- package/dist/graphql/GraphQLProtocol.d.ts.map +1 -0
- package/dist/graphql/GraphQLProtocol.js +719 -0
- package/dist/graphql/GraphQLProtocol.js.map +1 -0
- package/dist/graphql/GraphQLResolver.d.ts +453 -0
- package/dist/graphql/GraphQLResolver.d.ts.map +1 -0
- package/dist/graphql/GraphQLResolver.js +552 -0
- package/dist/graphql/GraphQLResolver.js.map +1 -0
- package/dist/graphql/index.d.ts +40 -0
- package/dist/graphql/index.d.ts.map +1 -0
- package/dist/graphql/index.js +41 -0
- package/dist/graphql/index.js.map +1 -0
- package/dist/http/FindMyWay/internal/router.js +3 -6
- package/dist/http/FindMyWay/internal/router.js.map +1 -1
- package/dist/http/HttpClient.d.ts +21 -0
- package/dist/http/HttpClient.d.ts.map +1 -1
- package/dist/http/HttpClient.js +63 -12
- package/dist/http/HttpClient.js.map +1 -1
- package/dist/http/HttpEffect.d.ts.map +1 -1
- package/dist/http/HttpEffect.js +14 -7
- package/dist/http/HttpEffect.js.map +1 -1
- package/dist/http/HttpMiddleware.d.ts.map +1 -1
- package/dist/http/HttpMiddleware.js +31 -32
- package/dist/http/HttpMiddleware.js.map +1 -1
- package/dist/http/HttpRouter.d.ts +35 -0
- package/dist/http/HttpRouter.d.ts.map +1 -1
- package/dist/http/HttpRouter.js +35 -0
- package/dist/http/HttpRouter.js.map +1 -1
- package/dist/http/HttpServerResponse.d.ts +8 -6
- package/dist/http/HttpServerResponse.d.ts.map +1 -1
- package/dist/http/HttpServerResponse.js +27 -16
- package/dist/http/HttpServerResponse.js.map +1 -1
- package/dist/http/HttpTraceContext.d.ts.map +1 -1
- package/dist/http/HttpTraceContext.js +4 -2
- package/dist/http/HttpTraceContext.js.map +1 -1
- package/dist/http/Multipart.d.ts.map +1 -1
- package/dist/http/Multipart.js +15 -1
- package/dist/http/Multipart.js.map +1 -1
- package/dist/http-api/HttpApiScalar.d.ts +5 -0
- package/dist/http-api/HttpApiScalar.d.ts.map +1 -1
- package/dist/http-api/HttpApiScalar.js.map +1 -1
- package/dist/internal/dateTime.js +40 -35
- package/dist/internal/dateTime.js.map +1 -1
- package/dist/internal/effect.js +19 -9
- package/dist/internal/effect.js.map +1 -1
- package/dist/internal/graphql.d.ts +2 -0
- package/dist/internal/graphql.d.ts.map +1 -0
- package/dist/internal/graphql.js +195 -0
- package/dist/internal/graphql.js.map +1 -0
- package/dist/internal/graphqlHeaders.d.ts +4 -0
- package/dist/internal/graphqlHeaders.d.ts.map +1 -0
- package/dist/internal/graphqlHeaders.js +11 -0
- package/dist/internal/graphqlHeaders.js.map +1 -0
- package/dist/internal/request.js +20 -1
- package/dist/internal/request.js.map +1 -1
- package/dist/internal/schema/toCodeDocument.js +4 -1
- package/dist/internal/schema/toCodeDocument.js.map +1 -1
- package/dist/internal/schema/toJsonSchemaDocument.js +29 -5
- package/dist/internal/schema/toJsonSchemaDocument.js.map +1 -1
- package/dist/internal/version.js +1 -1
- package/dist/net/AddressResolver.d.ts +145 -0
- package/dist/net/AddressResolver.d.ts.map +1 -0
- package/dist/net/AddressResolver.js +114 -0
- package/dist/net/AddressResolver.js.map +1 -0
- package/dist/net/Dns.d.ts +596 -0
- package/dist/net/Dns.d.ts.map +1 -0
- package/dist/net/Dns.js +549 -0
- package/dist/net/Dns.js.map +1 -0
- package/dist/net/Host.d.ts +369 -0
- package/dist/net/Host.d.ts.map +1 -0
- package/dist/net/Host.js +397 -0
- package/dist/net/Host.js.map +1 -0
- package/dist/net/IpInterface.d.ts +76 -3
- package/dist/net/IpInterface.d.ts.map +1 -1
- package/dist/net/IpInterface.js +53 -4
- package/dist/net/IpInterface.js.map +1 -1
- package/dist/net/IpNetwork.d.ts +82 -8
- package/dist/net/IpNetwork.d.ts.map +1 -1
- package/dist/net/IpNetwork.js +60 -4
- package/dist/net/IpNetwork.js.map +1 -1
- package/dist/net/NetAddress.d.ts +396 -35
- package/dist/net/NetAddress.d.ts.map +1 -1
- package/dist/net/NetAddress.js +358 -34
- package/dist/net/NetAddress.js.map +1 -1
- package/dist/net/index.d.ts +15 -0
- package/dist/net/index.d.ts.map +1 -1
- package/dist/net/index.js +15 -0
- package/dist/net/index.js.map +1 -1
- package/dist/observability/OtlpMetrics.d.ts.map +1 -1
- package/dist/observability/OtlpMetrics.js +15 -21
- package/dist/observability/OtlpMetrics.js.map +1 -1
- package/dist/observability/PrometheusMetrics.js +1 -0
- package/dist/observability/PrometheusMetrics.js.map +1 -1
- package/dist/persistence/PersistedQueue.d.ts +32 -3
- package/dist/persistence/PersistedQueue.d.ts.map +1 -1
- package/dist/persistence/PersistedQueue.js +75 -16
- package/dist/persistence/PersistedQueue.js.map +1 -1
- package/dist/rpc/RpcClient.d.ts +22 -6
- package/dist/rpc/RpcClient.d.ts.map +1 -1
- package/dist/rpc/RpcClient.js +12 -5
- package/dist/rpc/RpcClient.js.map +1 -1
- package/dist/rpc/RpcServer.d.ts +6 -0
- package/dist/rpc/RpcServer.d.ts.map +1 -1
- package/dist/rpc/RpcServer.js +78 -29
- package/dist/rpc/RpcServer.js.map +1 -1
- package/dist/socket/DatagramSocket.d.ts +456 -0
- package/dist/socket/DatagramSocket.d.ts.map +1 -0
- package/dist/socket/DatagramSocket.js +535 -0
- package/dist/socket/DatagramSocket.js.map +1 -0
- package/dist/socket/Socket.d.ts +20 -1
- package/dist/socket/Socket.d.ts.map +1 -1
- package/dist/socket/Socket.js +9 -1
- package/dist/socket/Socket.js.map +1 -1
- package/dist/socket/index.d.ts +5 -0
- package/dist/socket/index.d.ts.map +1 -1
- package/dist/socket/index.js +5 -0
- package/dist/socket/index.js.map +1 -1
- package/dist/sql/Migrator.d.ts.map +1 -1
- package/dist/sql/Migrator.js +7 -2
- package/dist/sql/Migrator.js.map +1 -1
- package/dist/sql/SqlClient.d.ts +7 -1
- package/dist/sql/SqlClient.d.ts.map +1 -1
- package/dist/sql/SqlClient.js +31 -6
- package/dist/sql/SqlClient.js.map +1 -1
- package/dist/testing/TestCrypto.d.ts +73 -0
- package/dist/testing/TestCrypto.d.ts.map +1 -0
- package/dist/testing/TestCrypto.js +87 -0
- package/dist/testing/TestCrypto.js.map +1 -0
- package/dist/testing/index.d.ts +5 -0
- package/dist/testing/index.d.ts.map +1 -1
- package/dist/testing/index.js +5 -0
- package/dist/testing/index.js.map +1 -1
- package/dist/workflow/DurableDeferred.d.ts +54 -0
- package/dist/workflow/DurableDeferred.d.ts.map +1 -1
- package/dist/workflow/DurableDeferred.js +108 -50
- package/dist/workflow/DurableDeferred.js.map +1 -1
- package/dist/workflow/WorkflowEngine.d.ts +24 -2
- package/dist/workflow/WorkflowEngine.d.ts.map +1 -1
- package/dist/workflow/WorkflowEngine.js +13 -3
- package/dist/workflow/WorkflowEngine.js.map +1 -1
- package/package.json +6 -3
- package/src/Context.ts +12 -3
- package/src/Crypto.ts +43 -9
- package/src/DateTime.ts +9 -6
- package/src/Duration.ts +36 -16
- package/src/FileSystem.ts +48 -2
- package/src/RequestResolver.ts +4 -10
- package/src/Scheduler.ts +9 -0
- package/src/Schema.ts +677 -5
- package/src/SchemaGetter.ts +47 -35
- package/src/Sink.ts +45 -18
- package/src/Stdio.ts +9 -2
- package/src/Stream.ts +44 -15
- package/src/Utils.ts +8 -5
- package/src/ai/AnthropicStructuredOutput.ts +2 -2
- package/src/ai/Chat.ts +2 -2
- package/src/ai/LanguageModel.ts +96 -24
- package/src/ai/McpServer.ts +288 -94
- package/src/ai/Prompt.ts +24 -10
- package/src/ai/Response.ts +6 -3
- package/src/ai/internal/mcpProtocol/v2026_07_28.ts +5 -0
- package/src/ai/internal/mcpProtocol.ts +3 -0
- package/src/ai/internal/mcpRuntime.ts +2 -0
- package/src/cli/CliOutput.ts +33 -16
- package/src/cli/Command.ts +4 -3
- package/src/cli/Completions.ts +9 -6
- package/src/cli/GlobalFlag.ts +19 -11
- package/src/cli/Prompt.ts +159 -149
- package/src/cli/internal/ansi.ts +26 -3
- package/src/cli/internal/completions/powershell.ts +322 -0
- package/src/cli/internal/completions/zsh.ts +4 -2
- package/src/cli/internal/wizard.ts +34 -26
- package/src/cluster/ClusterSchema.ts +32 -0
- package/src/cluster/ClusterWorkflowEngine.ts +64 -36
- package/src/cluster/MessageStorage.ts +45 -14
- package/src/cluster/Runners.ts +8 -2
- package/src/cluster/Sharding.ts +219 -58
- package/src/cluster/SqlMessageStorage.ts +118 -84
- package/src/cluster/SqlRunnerStorage.ts +224 -128
- package/src/cluster/internal/entityManager.ts +151 -55
- package/src/encoding/Sse.ts +69 -79
- package/src/graphql/GraphQL.ts +494 -0
- package/src/graphql/GraphQLClient.ts +912 -0
- package/src/graphql/GraphQLClientError.ts +542 -0
- package/src/graphql/GraphQLGroup.ts +167 -0
- package/src/graphql/GraphQLMiddleware.ts +253 -0
- package/src/graphql/GraphQLProtocol.ts +1286 -0
- package/src/graphql/GraphQLResolver.ts +1010 -0
- package/src/graphql/index.ts +48 -0
- package/src/http/FindMyWay/internal/router.ts +4 -7
- package/src/http/HttpClient.ts +110 -12
- package/src/http/HttpEffect.ts +11 -5
- package/src/http/HttpMiddleware.ts +33 -34
- package/src/http/HttpRouter.ts +35 -0
- package/src/http/HttpServerResponse.ts +28 -16
- package/src/http/HttpTraceContext.ts +8 -2
- package/src/http/Multipart.ts +17 -1
- package/src/http-api/HttpApiScalar.ts +5 -0
- package/src/internal/dateTime.ts +13 -10
- package/src/internal/effect.ts +22 -15
- package/src/internal/graphql.ts +263 -0
- package/src/internal/graphqlHeaders.ts +11 -0
- package/src/internal/request.ts +31 -3
- package/src/internal/schema/toCodeDocument.ts +7 -1
- package/src/internal/schema/toJsonSchemaDocument.ts +34 -2
- package/src/internal/version.ts +1 -1
- package/src/net/AddressResolver.ts +222 -0
- package/src/net/Dns.ts +1035 -0
- package/src/net/Host.ts +544 -0
- package/src/net/IpInterface.ts +117 -7
- package/src/net/IpNetwork.ts +116 -17
- package/src/net/NetAddress.ts +620 -76
- package/src/net/index.ts +18 -0
- package/src/observability/OtlpMetrics.ts +12 -23
- package/src/observability/PrometheusMetrics.ts +1 -0
- package/src/persistence/PersistedQueue.ts +146 -53
- package/src/rpc/RpcClient.ts +41 -19
- package/src/rpc/RpcServer.ts +115 -40
- package/src/socket/DatagramSocket.ts +919 -0
- package/src/socket/Socket.ts +28 -2
- package/src/socket/index.ts +6 -0
- package/src/sql/Migrator.ts +21 -5
- package/src/sql/SqlClient.ts +39 -15
- package/src/testing/TestCrypto.ts +94 -0
- package/src/testing/index.ts +6 -0
- package/src/workflow/DurableDeferred.ts +160 -47
- package/src/workflow/WorkflowEngine.ts +50 -5
|
@@ -0,0 +1,253 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* GraphQL-level middleware provided as `Context.Service` implementations.
|
|
3
|
+
* Middleware can rewrite requests and inspect raw results, including response
|
|
4
|
+
* extensions and HTTP headers. Provide implementations with `Layer.succeed`
|
|
5
|
+
* or `Layer.effect`.
|
|
6
|
+
*
|
|
7
|
+
* @stability experimental
|
|
8
|
+
* @since 4.0.0
|
|
9
|
+
*/
|
|
10
|
+
import * as Context from "../Context.ts"
|
|
11
|
+
import * as Effect from "../Effect.ts"
|
|
12
|
+
import * as Stream from "../Stream.ts"
|
|
13
|
+
import type * as Types from "../Types.ts"
|
|
14
|
+
import type * as GraphQL from "./GraphQL.ts"
|
|
15
|
+
import type { GraphQLClientError } from "./GraphQLClientError.ts"
|
|
16
|
+
import type { ExecutionResult, GraphQLRequest } from "./GraphQLProtocol.ts"
|
|
17
|
+
|
|
18
|
+
const TypeId = "~effect/graphql/GraphQLMiddleware"
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Middleware handlers for queries, mutations and subscriptions. Both
|
|
22
|
+
* `execute` and `subscribe` are required.
|
|
23
|
+
*
|
|
24
|
+
* **Details**
|
|
25
|
+
*
|
|
26
|
+
* `execute` runs once per query or mutation. `subscribe` runs once per
|
|
27
|
+
* subscription attempt (the client re-runs the whole chain when it retries a
|
|
28
|
+
* subscription) and can map or tap the event stream.
|
|
29
|
+
*
|
|
30
|
+
* Both receive the operation being run, the request so far, and `next`, which
|
|
31
|
+
* runs the rest of the chain with the given request and returns the raw
|
|
32
|
+
* `ExecutionResult` (or, for `subscribe`, the stream of them). Its `status`
|
|
33
|
+
* and `headers` are the HTTP status and response headers the transport
|
|
34
|
+
* reported, if any; a middleware may rewrite them, and a failed call's
|
|
35
|
+
* `ResponseError` carries the final values. Request and response headers
|
|
36
|
+
* are `Headers.Headers` values; use `Headers.set` to rewrite them.
|
|
37
|
+
*
|
|
38
|
+
* `next` fails with `GraphQLClientError`, or with the error of a middleware
|
|
39
|
+
* further in, which is typed as `Types.unhandled`, as in `RpcMiddleware`.
|
|
40
|
+
* Narrow it, for example with `Effect.catchTag("GraphQLClientError", ...)`,
|
|
41
|
+
* before reading `reason`.
|
|
42
|
+
*
|
|
43
|
+
* Use {@link mapRequest} when the middleware only needs to transform the
|
|
44
|
+
* request.
|
|
45
|
+
*
|
|
46
|
+
* **Example** (Reading a rate-limit header)
|
|
47
|
+
*
|
|
48
|
+
* ```ts import.meta.vitest
|
|
49
|
+
* import { Effect } from "effect"
|
|
50
|
+
* import { GraphQLMiddleware } from "effect/graphql"
|
|
51
|
+
* import { Headers } from "effect/http"
|
|
52
|
+
*
|
|
53
|
+
* const rateLimit: GraphQLMiddleware.Implementation = {
|
|
54
|
+
* execute: ({ next, request }) =>
|
|
55
|
+
* Effect.tap(next(request), (result) =>
|
|
56
|
+
* Effect.log(result.headers === undefined ? undefined : Headers.get(result.headers, "x-ratelimit-remaining"))
|
|
57
|
+
* ),
|
|
58
|
+
* subscribe: ({ next, request }) => next(request)
|
|
59
|
+
* }
|
|
60
|
+
* ```
|
|
61
|
+
*
|
|
62
|
+
* @stability experimental
|
|
63
|
+
* @category models
|
|
64
|
+
* @since 4.0.0
|
|
65
|
+
*/
|
|
66
|
+
export interface Implementation<E = never, R = never> {
|
|
67
|
+
readonly execute: (options: {
|
|
68
|
+
readonly operation: GraphQL.Any
|
|
69
|
+
readonly request: GraphQLRequest
|
|
70
|
+
readonly next: (request: GraphQLRequest) => Effect.Effect<ExecutionResult, GraphQLClientError | Types.unhandled>
|
|
71
|
+
}) => Effect.Effect<ExecutionResult, GraphQLClientError | Types.unhandled | E, R>
|
|
72
|
+
readonly subscribe: (options: {
|
|
73
|
+
readonly operation: GraphQL.Any
|
|
74
|
+
readonly request: GraphQLRequest
|
|
75
|
+
readonly next: (request: GraphQLRequest) => Stream.Stream<ExecutionResult, GraphQLClientError | Types.unhandled>
|
|
76
|
+
}) => Stream.Stream<ExecutionResult, GraphQLClientError | Types.unhandled | E, R>
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* The class returned by {@link Service}: a `Context.Service` for an
|
|
81
|
+
* {@link Implementation}, carrying the declared `error` and `requires` types.
|
|
82
|
+
*
|
|
83
|
+
* @stability experimental
|
|
84
|
+
* @category services
|
|
85
|
+
* @since 4.0.0
|
|
86
|
+
*/
|
|
87
|
+
export interface ServiceClass<Self, Key extends string, E, R> extends Context.Service<Self, Implementation<E, R>> {
|
|
88
|
+
new(_: never): Context.ServiceClass.Shape<Key, Implementation<E, R>> & {
|
|
89
|
+
readonly [TypeId]: { readonly error: E; readonly requires: R }
|
|
90
|
+
}
|
|
91
|
+
readonly [TypeId]: { readonly error: E; readonly requires: R }
|
|
92
|
+
readonly key: Key
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Any middleware service class, with its error and requirement types erased.
|
|
97
|
+
*
|
|
98
|
+
* @stability experimental
|
|
99
|
+
* @category utility types
|
|
100
|
+
* @since 4.0.0
|
|
101
|
+
*/
|
|
102
|
+
export interface AnyService extends Context.Service<any, Implementation<any, any>> {
|
|
103
|
+
readonly [TypeId]: { readonly error: any; readonly requires: any }
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Extracts the service identifier of a middleware, which is what
|
|
108
|
+
* `GraphQLClient.make` requires.
|
|
109
|
+
*
|
|
110
|
+
* @stability experimental
|
|
111
|
+
* @category utility types
|
|
112
|
+
* @since 4.0.0
|
|
113
|
+
*/
|
|
114
|
+
export type Identifier<M> = M extends Context.Service<infer I, any> ? I : never
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* Extracts the error type a middleware declared.
|
|
118
|
+
*
|
|
119
|
+
* @stability experimental
|
|
120
|
+
* @category utility types
|
|
121
|
+
* @since 4.0.0
|
|
122
|
+
*/
|
|
123
|
+
export type Error<M> = M extends { readonly [TypeId]: { readonly error: infer E } } ? E : never
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* Extracts the services a middleware declared it requires per call.
|
|
127
|
+
*
|
|
128
|
+
* @stability experimental
|
|
129
|
+
* @category utility types
|
|
130
|
+
* @since 4.0.0
|
|
131
|
+
*/
|
|
132
|
+
export type Requires<M> = M extends { readonly [TypeId]: { readonly requires: infer R } } ? R : never
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* Declares a middleware tag. `requires` are services the implementation needs
|
|
136
|
+
* on every call, and `error` is what it can fail with; both show up on the
|
|
137
|
+
* requirements and error type of every client method the middleware is
|
|
138
|
+
* attached to.
|
|
139
|
+
*
|
|
140
|
+
* **Example** (An auth middleware that needs the current user)
|
|
141
|
+
*
|
|
142
|
+
* ```ts import.meta.vitest
|
|
143
|
+
* import { Context, Effect, Layer, Schema, Stream } from "effect"
|
|
144
|
+
* import { GraphQL, GraphQLClient, GraphQLGroup, GraphQLMiddleware, GraphQLProtocol } from "effect/graphql"
|
|
145
|
+
* import { Headers } from "effect/http"
|
|
146
|
+
*
|
|
147
|
+
* class CurrentUser extends Context.Service<CurrentUser, { readonly token: string }>()("CurrentUser") {}
|
|
148
|
+
* class TokenExpired extends Schema.TaggedError<TokenExpired>()("TokenExpired", {}) {}
|
|
149
|
+
*
|
|
150
|
+
* class Auth extends GraphQLMiddleware.Service<Auth, {
|
|
151
|
+
* requires: CurrentUser
|
|
152
|
+
* error: TokenExpired
|
|
153
|
+
* }>()("app/Auth") {}
|
|
154
|
+
*
|
|
155
|
+
* const AuthLive = Layer.succeed(
|
|
156
|
+
* Auth,
|
|
157
|
+
* GraphQLMiddleware.mapRequest(Effect.fnUntraced(function*(request) {
|
|
158
|
+
* const user = yield* CurrentUser
|
|
159
|
+
* if (user.token === "") return yield* new TokenExpired()
|
|
160
|
+
* return { ...request, headers: Headers.set(request.headers, "authorization", `Bearer ${user.token}`) }
|
|
161
|
+
* }))
|
|
162
|
+
* )
|
|
163
|
+
*
|
|
164
|
+
* const Viewer = GraphQL.query("Viewer", {
|
|
165
|
+
* document: "query Viewer{viewer{login}}",
|
|
166
|
+
* result: Schema.Struct({ viewer: Schema.Struct({ login: Schema.String }) })
|
|
167
|
+
* })
|
|
168
|
+
*
|
|
169
|
+
* const Scripted = Layer.succeed(GraphQLProtocol.GraphQLProtocol, {
|
|
170
|
+
* execute: (request) => Effect.succeed({ body: { data: { viewer: { login: request.headers.authorization } } } }),
|
|
171
|
+
* subscribe: () => Stream.empty
|
|
172
|
+
* })
|
|
173
|
+
*
|
|
174
|
+
* const program = Effect.gen(function*() {
|
|
175
|
+
* const client = yield* GraphQLClient.make(GraphQLGroup.make(Viewer).middleware(Auth))
|
|
176
|
+
* // CurrentUser is required by the method because Auth is attached
|
|
177
|
+
* return yield* client.Viewer().pipe(Effect.provideService(CurrentUser, { token: "abc" }))
|
|
178
|
+
* })
|
|
179
|
+
*
|
|
180
|
+
* await Effect.runPromise(program.pipe(Effect.provide([Scripted, AuthLive]))) // => { viewer: { login: "Bearer abc" } }
|
|
181
|
+
* ```
|
|
182
|
+
*
|
|
183
|
+
* @stability experimental
|
|
184
|
+
* @category constructors
|
|
185
|
+
* @since 4.0.0
|
|
186
|
+
*/
|
|
187
|
+
export const Service = <
|
|
188
|
+
Self,
|
|
189
|
+
Config extends {
|
|
190
|
+
readonly requires?: any
|
|
191
|
+
readonly error?: any
|
|
192
|
+
} = {}
|
|
193
|
+
>(): <const Key extends string>(key: Key) => ServiceClass<
|
|
194
|
+
Self,
|
|
195
|
+
Key,
|
|
196
|
+
Config extends { readonly error: infer E } ? E : never,
|
|
197
|
+
Config extends { readonly requires: infer R } ? R : never
|
|
198
|
+
> =>
|
|
199
|
+
(key: string) => {
|
|
200
|
+
function ServiceClass() {}
|
|
201
|
+
Object.setPrototypeOf(ServiceClass, Object.getPrototypeOf(Context.Service<Self, any>(key)))
|
|
202
|
+
return Object.assign(ServiceClass, { key, [TypeId]: TypeId }) as any
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
/**
|
|
206
|
+
* Builds an {@link Implementation} that transforms requests for queries,
|
|
207
|
+
* mutations and subscriptions. Use it to set headers, auth or extensions.
|
|
208
|
+
*
|
|
209
|
+
* **Example** (Adding a header to every request)
|
|
210
|
+
*
|
|
211
|
+
* ```ts import.meta.vitest
|
|
212
|
+
* import { Effect, Layer, Schema, Stream } from "effect"
|
|
213
|
+
* import { GraphQL, GraphQLClient, GraphQLGroup, GraphQLMiddleware, GraphQLProtocol } from "effect/graphql"
|
|
214
|
+
* import { Headers } from "effect/http"
|
|
215
|
+
*
|
|
216
|
+
* class RequestId extends GraphQLMiddleware.Service<RequestId>()("app/RequestId") {}
|
|
217
|
+
*
|
|
218
|
+
* const RequestIdLive = Layer.succeed(
|
|
219
|
+
* RequestId,
|
|
220
|
+
* GraphQLMiddleware.mapRequest((request) =>
|
|
221
|
+
* Effect.succeed({ ...request, headers: Headers.set(request.headers, "x-request-id", "abc") })
|
|
222
|
+
* )
|
|
223
|
+
* )
|
|
224
|
+
*
|
|
225
|
+
* const Viewer = GraphQL.query("Viewer", {
|
|
226
|
+
* document: "query Viewer{viewer{login}}",
|
|
227
|
+
* result: Schema.Struct({ viewer: Schema.Struct({ login: Schema.String }) })
|
|
228
|
+
* })
|
|
229
|
+
*
|
|
230
|
+
* const Scripted = Layer.succeed(GraphQLProtocol.GraphQLProtocol, {
|
|
231
|
+
* execute: (request) =>
|
|
232
|
+
* Effect.succeed({ body: { data: { viewer: { login: request.headers["x-request-id"] } } } }),
|
|
233
|
+
* subscribe: () => Stream.empty
|
|
234
|
+
* })
|
|
235
|
+
*
|
|
236
|
+
* const program = Effect.gen(function*() {
|
|
237
|
+
* const client = yield* GraphQLClient.make(GraphQLGroup.make(Viewer).middleware(RequestId))
|
|
238
|
+
* return yield* client.Viewer()
|
|
239
|
+
* })
|
|
240
|
+
*
|
|
241
|
+
* await Effect.runPromise(program.pipe(Effect.provide([Scripted, RequestIdLive]))) // => { viewer: { login: "abc" } }
|
|
242
|
+
* ```
|
|
243
|
+
*
|
|
244
|
+
* @stability experimental
|
|
245
|
+
* @category constructors
|
|
246
|
+
* @since 4.0.0
|
|
247
|
+
*/
|
|
248
|
+
export const mapRequest = <E = never, R = never>(
|
|
249
|
+
f: (request: GraphQLRequest, operation: GraphQL.Any) => Effect.Effect<GraphQLRequest, E, R>
|
|
250
|
+
): Implementation<E, R> => ({
|
|
251
|
+
execute: ({ next, operation, request }) => Effect.flatMap(f(request, operation), next),
|
|
252
|
+
subscribe: ({ next, operation, request }) => Stream.unwrap(Effect.map(f(request, operation), next))
|
|
253
|
+
})
|