@stackables/bridge-graphql 0.0.1

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Stackables
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,58 @@
1
+ import { type GraphQLSchema } from "graphql";
2
+ import { type Logger, type ToolTrace, type TraceLevel } from "@stackables/bridge-core";
3
+ import type { Instruction, ToolMap } from "@stackables/bridge-core";
4
+ export type { Logger };
5
+ export type BridgeOptions = {
6
+ /** Tool functions available to the engine.
7
+ * Supports namespaced nesting: `{ myNamespace: { myTool } }`.
8
+ * The built-in `std` namespace and `httpCall` are always included;
9
+ * user tools are merged on top (shallow). */
10
+ tools?: ToolMap;
11
+ /** Optional function to reshape/restrict the GQL context before it reaches bridge files.
12
+ * By default the full context is exposed via `with context`. */
13
+ contextMapper?: (context: any) => Record<string, any>;
14
+ /** Enable tool-call tracing.
15
+ * - `"off"` (default) — no collection, zero overhead
16
+ * - `"basic"` — tool, fn, timing, errors; no input/output
17
+ * - `"full"` — everything including input and output */
18
+ trace?: TraceLevel;
19
+ /**
20
+ * Structured logger for engine-level events (tool errors, warnings, debug).
21
+ * Accepts any logger with `debug`, `info`, `warn`, and `error` methods —
22
+ * pino, winston, `console`, or any compatible interface.
23
+ * Defaults to silent no-ops so there is zero output unless you opt in.
24
+ */
25
+ logger?: Logger;
26
+ };
27
+ /** Instructions can be a static array or a function that selects per-request */
28
+ export type InstructionSource = Instruction[] | ((context: any) => Instruction[]);
29
+ export declare function bridgeTransform(schema: GraphQLSchema, instructions: InstructionSource, options?: BridgeOptions): GraphQLSchema;
30
+ /**
31
+ * Read traces that were collected during the current request.
32
+ * Pass the GraphQL context object; returns an empty array when tracing is
33
+ * disabled or no traces were recorded.
34
+ */
35
+ export declare function getBridgeTraces(context: any): ToolTrace[];
36
+ /**
37
+ * Envelop-compatible plugin for GraphQL Yoga (or any Envelop-based server).
38
+ * When bridge tracing is enabled, this plugin copies the recorded traces into
39
+ * the GraphQL response `extensions.traces` field.
40
+ *
41
+ * Usage:
42
+ * ```ts
43
+ * createYoga({ schema, plugins: [useBridgeTracing()] })
44
+ * ```
45
+ */
46
+ export declare function useBridgeTracing(): {
47
+ onExecute({ args }: {
48
+ args: {
49
+ contextValue: any;
50
+ };
51
+ }): {
52
+ onExecuteDone({ result, setResult, }: {
53
+ result: any;
54
+ setResult: (r: any) => void;
55
+ }): void;
56
+ };
57
+ };
58
+ //# sourceMappingURL=bridge-transform.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"bridge-transform.d.ts","sourceRoot":"","sources":["../src/bridge-transform.ts"],"names":[],"mappings":"AACA,OAAO,EAGL,KAAK,aAAa,EAEnB,MAAM,SAAS,CAAC;AACjB,OAAO,EAGL,KAAK,MAAM,EACX,KAAK,SAAS,EACd,KAAK,UAAU,EAChB,MAAM,yBAAyB,CAAC;AAEjC,OAAO,KAAK,EAAE,WAAW,EAAE,OAAO,EAAE,MAAM,yBAAyB,CAAC;AAGpE,YAAY,EAAE,MAAM,EAAE,CAAC;AAUvB,MAAM,MAAM,aAAa,GAAG;IAC1B;;;kDAG8C;IAC9C,KAAK,CAAC,EAAE,OAAO,CAAC;IAChB;qEACiE;IACjE,aAAa,CAAC,EAAE,CAAC,OAAO,EAAE,GAAG,KAAK,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;IACtD;;;6DAGyD;IACzD,KAAK,CAAC,EAAE,UAAU,CAAC;IACnB;;;;;OAKG;IACH,MAAM,CAAC,EAAE,MAAM,CAAC;CACjB,CAAC;AAEF,gFAAgF;AAChF,MAAM,MAAM,iBAAiB,GACzB,WAAW,EAAE,GACb,CAAC,CAAC,OAAO,EAAE,GAAG,KAAK,WAAW,EAAE,CAAC,CAAC;AAEtC,wBAAgB,eAAe,CAC7B,MAAM,EAAE,aAAa,EACrB,YAAY,EAAE,iBAAiB,EAC/B,OAAO,CAAC,EAAE,aAAa,GACtB,aAAa,CAqHf;AAED;;;;GAIG;AACH,wBAAgB,eAAe,CAAC,OAAO,EAAE,GAAG,GAAG,SAAS,EAAE,CAEzD;AAED;;;;;;;;;GASG;AACH,wBAAgB,gBAAgB;wBAER;QAAE,IAAI,EAAE;YAAE,YAAY,EAAE,GAAG,CAAA;SAAE,CAAA;KAAE;8CAK5C;YACD,MAAM,EAAE,GAAG,CAAC;YACZ,SAAS,EAAE,CAAC,CAAC,EAAE,GAAG,KAAK,IAAI,CAAC;SAC7B;;EAeR"}
@@ -0,0 +1,135 @@
1
+ import { MapperKind, mapSchema } from "@graphql-tools/utils";
2
+ import { GraphQLList, GraphQLNonNull, defaultFieldResolver, } from "graphql";
3
+ import { ExecutionTree, TraceCollector, } from "@stackables/bridge-core";
4
+ import { std } from "@stackables/bridge-stdlib";
5
+ import { SELF_MODULE } from "@stackables/bridge-core";
6
+ const noop = () => { };
7
+ const defaultLogger = {
8
+ debug: noop,
9
+ info: noop,
10
+ warn: noop,
11
+ error: noop,
12
+ };
13
+ export function bridgeTransform(schema, instructions, options) {
14
+ const userTools = options?.tools;
15
+ const contextMapper = options?.contextMapper;
16
+ const traceLevel = options?.trace ?? "off";
17
+ const logger = options?.logger ?? defaultLogger;
18
+ return mapSchema(schema, {
19
+ [MapperKind.OBJECT_FIELD]: (fieldConfig, fieldName, typeName) => {
20
+ let array = false;
21
+ if (fieldConfig.type instanceof GraphQLNonNull) {
22
+ if (fieldConfig.type.ofType instanceof GraphQLList) {
23
+ array = true;
24
+ }
25
+ }
26
+ if (fieldConfig.type instanceof GraphQLList) {
27
+ array = true;
28
+ }
29
+ const trunk = { module: SELF_MODULE, type: typeName, field: fieldName };
30
+ const { resolve = defaultFieldResolver } = fieldConfig;
31
+ return {
32
+ ...fieldConfig,
33
+ resolve: async function (source, args, context, info) {
34
+ // Start execution tree at query/mutation root
35
+ if (!source && !info.path.prev) {
36
+ const activeInstructions = typeof instructions === "function"
37
+ ? instructions(context)
38
+ : instructions;
39
+ // Only intercept fields that have a matching bridge instruction.
40
+ // Fields without one fall through to their original resolver,
41
+ // allowing hand-coded resolvers to coexist with bridge-powered ones.
42
+ const hasBridge = activeInstructions.some((i) => i.kind === "bridge" &&
43
+ i.type === typeName &&
44
+ i.field === fieldName);
45
+ if (!hasBridge) {
46
+ return resolve(source, args, context, info);
47
+ }
48
+ const bridgeContext = contextMapper
49
+ ? contextMapper(context)
50
+ : (context ?? {});
51
+ // std is always included; user tools merge on top (shallow)
52
+ // internal tools are injected automatically by ExecutionTree
53
+ const allTools = {
54
+ std,
55
+ ...(userTools ?? {}),
56
+ };
57
+ source = new ExecutionTree(trunk, activeInstructions, allTools, bridgeContext);
58
+ source.logger = logger;
59
+ if (traceLevel !== "off") {
60
+ source.tracer = new TraceCollector(traceLevel);
61
+ // Stash tracer on GQL context so the tracing plugin can read it
62
+ context.__bridgeTracer = source.tracer;
63
+ }
64
+ }
65
+ if (source instanceof ExecutionTree &&
66
+ args &&
67
+ Object.keys(args).length > 0) {
68
+ source.push(args);
69
+ }
70
+ // Kick off forced handles (force <handle>) at the root entry point
71
+ if (source instanceof ExecutionTree && !info.path.prev) {
72
+ // Ensure input state exists even with no args (prevents
73
+ // recursive scheduling of the input trunk → stack overflow).
74
+ if (!args || Object.keys(args).length === 0) {
75
+ source.push({});
76
+ }
77
+ const criticalForces = source.executeForced();
78
+ if (criticalForces.length > 0) {
79
+ source.setForcedExecution(Promise.all(criticalForces).then(() => { }));
80
+ }
81
+ }
82
+ if (source instanceof ExecutionTree) {
83
+ const result = source.response(info.path, array);
84
+ // At the leaf level (not root), race data pull with critical
85
+ // force promises so errors propagate into GraphQL `errors[]`
86
+ // while still allowing parallel execution.
87
+ if (info.path.prev && source.getForcedExecution()) {
88
+ return Promise.all([result, source.getForcedExecution()]).then(([data]) => data);
89
+ }
90
+ return result;
91
+ }
92
+ return resolve(source, args, context, info);
93
+ },
94
+ };
95
+ },
96
+ });
97
+ }
98
+ /**
99
+ * Read traces that were collected during the current request.
100
+ * Pass the GraphQL context object; returns an empty array when tracing is
101
+ * disabled or no traces were recorded.
102
+ */
103
+ export function getBridgeTraces(context) {
104
+ return context?.__bridgeTracer?.traces ?? [];
105
+ }
106
+ /**
107
+ * Envelop-compatible plugin for GraphQL Yoga (or any Envelop-based server).
108
+ * When bridge tracing is enabled, this plugin copies the recorded traces into
109
+ * the GraphQL response `extensions.traces` field.
110
+ *
111
+ * Usage:
112
+ * ```ts
113
+ * createYoga({ schema, plugins: [useBridgeTracing()] })
114
+ * ```
115
+ */
116
+ export function useBridgeTracing() {
117
+ return {
118
+ onExecute({ args }) {
119
+ return {
120
+ onExecuteDone({ result, setResult, }) {
121
+ const traces = getBridgeTraces(args.contextValue);
122
+ if (traces.length > 0 && result && "data" in result) {
123
+ setResult({
124
+ ...result,
125
+ extensions: {
126
+ ...(result.extensions ?? {}),
127
+ traces,
128
+ },
129
+ });
130
+ }
131
+ },
132
+ };
133
+ },
134
+ };
135
+ }
@@ -0,0 +1,11 @@
1
+ /**
2
+ * @stackables/bridge-graphql — Wire bridges into a GraphQL schema.
3
+ *
4
+ * Provides `bridgeTransform()` to map bridge instructions onto GraphQL
5
+ * resolvers, plus tracing utilities for request-level tool-call visibility.
6
+ *
7
+ * Peer dependencies: `graphql`, `@graphql-tools/utils`.
8
+ */
9
+ export { bridgeTransform, getBridgeTraces, useBridgeTracing, } from "./bridge-transform.ts";
10
+ export type { BridgeOptions, InstructionSource } from "./bridge-transform.ts";
11
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,EACL,eAAe,EACf,eAAe,EACf,gBAAgB,GACjB,MAAM,uBAAuB,CAAC;AAC/B,YAAY,EAAE,aAAa,EAAE,iBAAiB,EAAE,MAAM,uBAAuB,CAAC"}
package/build/index.js ADDED
@@ -0,0 +1,9 @@
1
+ /**
2
+ * @stackables/bridge-graphql — Wire bridges into a GraphQL schema.
3
+ *
4
+ * Provides `bridgeTransform()` to map bridge instructions onto GraphQL
5
+ * resolvers, plus tracing utilities for request-level tool-call visibility.
6
+ *
7
+ * Peer dependencies: `graphql`, `@graphql-tools/utils`.
8
+ */
9
+ export { bridgeTransform, getBridgeTraces, useBridgeTracing, } from "./bridge-transform.js";
package/package.json ADDED
@@ -0,0 +1,41 @@
1
+ {
2
+ "name": "@stackables/bridge-graphql",
3
+ "version": "0.0.1",
4
+ "description": "Bridge GraphQL adapter — wire bridges into a GraphQL schema",
5
+ "main": "./build/index.js",
6
+ "type": "module",
7
+ "types": "./build/index.d.ts",
8
+ "exports": {
9
+ ".": {
10
+ "source": "./src/index.ts",
11
+ "import": "./build/index.js",
12
+ "types": "./build/index.d.ts"
13
+ }
14
+ },
15
+ "files": [
16
+ "build",
17
+ "README.md"
18
+ ],
19
+ "repository": {
20
+ "type": "git",
21
+ "url": "git+https://github.com/stackables/bridge.git"
22
+ },
23
+ "license": "MIT",
24
+ "dependencies": {
25
+ "@stackables/bridge-stdlib": "0.0.1",
26
+ "@stackables/bridge-core": "0.0.1"
27
+ },
28
+ "devDependencies": {
29
+ "typescript": "^5.9.3"
30
+ },
31
+ "peerDependencies": {
32
+ "graphql": "^16",
33
+ "@graphql-tools/utils": "^11"
34
+ },
35
+ "publishConfig": {
36
+ "access": "public"
37
+ },
38
+ "scripts": {
39
+ "build": "tsc -p tsconfig.json"
40
+ }
41
+ }