@silkline/hasura-sim 0.1.1 → 0.1.3

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.
Files changed (2) hide show
  1. package/package.json +6 -2
  2. package/src/index.d.ts +130 -0
package/package.json CHANGED
@@ -1,11 +1,15 @@
1
1
  {
2
2
  "name": "@silkline/hasura-sim",
3
- "version": "0.1.1",
3
+ "version": "0.1.3",
4
4
  "description": "In-memory Hasura GraphQL Engine (compiled to WebAssembly) + pglite — runs real GraphQL queries/mutations offline for tests and codegen, with no Docker/Postgres/network.",
5
5
  "type": "module",
6
6
  "main": "src/index.mjs",
7
+ "types": "src/index.d.ts",
7
8
  "exports": {
8
- ".": "./src/index.mjs"
9
+ ".": {
10
+ "types": "./src/index.d.ts",
11
+ "default": "./src/index.mjs"
12
+ }
9
13
  },
10
14
  "files": [
11
15
  "src",
package/src/index.d.ts ADDED
@@ -0,0 +1,130 @@
1
+ import type { PGliteInterface } from '@electric-sql/pglite';
2
+ import type {
3
+ GraphQLFieldResolver,
4
+ GraphQLFormattedError,
5
+ IntrospectionQuery,
6
+ } from 'graphql';
7
+
8
+ export { PGlite } from '@electric-sql/pglite';
9
+
10
+ /** Arguments to a single GraphQL execution. */
11
+ export interface ExecuteArgs {
12
+ query: string;
13
+ variables?: Record<string, unknown>;
14
+ /** x-hasura-role; defaults to `admin` (bypasses permissions). */
15
+ role?: string;
16
+ /** x-hasura-* session variables, e.g. { 'x-hasura-org-id': '…' }. */
17
+ sessionVariables?: Record<string, string>;
18
+ }
19
+
20
+ /** Result of a GraphQL execution: the engine's `{ data, errors }` payload. */
21
+ export interface ExecuteResult<TData = Record<string, unknown>> {
22
+ data?: TData | null;
23
+ errors?: readonly GraphQLFormattedError[];
24
+ }
25
+
26
+ /** A graphql-js resolver map: `{ [typeName]: { [fieldName]: resolver } }`. */
27
+ export type RemoteResolvers = Record<
28
+ string,
29
+ Record<string, GraphQLFieldResolver<unknown, unknown>>
30
+ >;
31
+
32
+ /**
33
+ * A remote schema, supplied as a static SDL string or as an object carrying the
34
+ * SDL (for schema-cache build) plus optional mock execution — either field
35
+ * `resolvers` (run with graphql-js over the SDL) or a whole-operation
36
+ * `handler`. `{ introspection }` accepts a pre-computed introspection response.
37
+ */
38
+ export type RemoteSchemaDef =
39
+ | string
40
+ | {
41
+ sdl: string;
42
+ resolvers?: RemoteResolvers;
43
+ handler?: (op: {
44
+ query: string;
45
+ variables?: Record<string, unknown>;
46
+ }) => ExecuteResult | Promise<ExecuteResult>;
47
+ }
48
+ | { introspection: unknown };
49
+
50
+ /** Context passed to an action handler alongside its arguments. */
51
+ export interface ActionHandlerContext {
52
+ /** x-hasura-* session variables from the request. */
53
+ sessionVariables: Record<string, string>;
54
+ /** The raw Hasura action webhook payload. */
55
+ payload: unknown;
56
+ }
57
+
58
+ /**
59
+ * Handler for a Hasura action. `args` is the action's arguments object (keyed
60
+ * by your argument names, e.g. `{ input }`).
61
+ */
62
+ export type ActionHandler<
63
+ TArgs = Record<string, unknown>,
64
+ TResult = unknown,
65
+ > = (args: TArgs, context: ActionHandlerContext) => TResult | Promise<TResult>;
66
+
67
+ export interface CreateSimulatorArgs {
68
+ /** Consolidated Hasura metadata ({ version, sources, … }). */
69
+ metadataJson?: unknown;
70
+ /** A pglite instance to run SQL against; one is created if omitted. */
71
+ db?: PGliteInterface;
72
+ /** Remote schemas, keyed by remote-schema name. */
73
+ remoteSchemas?: Record<string, RemoteSchemaDef>;
74
+ /** Action handlers, keyed by action name. */
75
+ actions?: Record<string, ActionHandler>;
76
+ }
77
+
78
+ /** Result of `sim_init`; `error` is set if metadata/schema-cache build failed. */
79
+ export interface InitResult {
80
+ error?: string;
81
+ }
82
+
83
+ export interface RoleArgs {
84
+ /** x-hasura-role to scope the schema/operation to. */
85
+ role?: string;
86
+ /** x-hasura-* session variables the role expects. */
87
+ sessionVariables?: Record<string, string>;
88
+ }
89
+
90
+ /** A running simulator: a wasm Hasura engine bound to a pglite database. */
91
+ export interface Simulator {
92
+ /** The pglite database the engine executes SQL against. */
93
+ db: PGliteInterface;
94
+ /** (Re)build the schema cache from metadata + static remote introspections. */
95
+ init(
96
+ metadata: unknown,
97
+ remoteSchemas?: Record<string, RemoteSchemaDef>,
98
+ ): Promise<InitResult>;
99
+ /** The engine's version string. */
100
+ version(): Promise<string>;
101
+ /** Execute a GraphQL operation and return `{ data, errors }`. */
102
+ execute<TData = Record<string, unknown>>(
103
+ args: ExecuteArgs,
104
+ ): Promise<ExecuteResult<TData>>;
105
+ /** Run standard GraphQL introspection for a role. */
106
+ introspect(args?: RoleArgs): Promise<IntrospectionQuery>;
107
+ /** Role-scoped schema as SDL — a static codegen base, generated offline. */
108
+ schemaSDL(args?: RoleArgs): Promise<string>;
109
+ }
110
+
111
+ /**
112
+ * Create an in-memory Hasura simulator: the real GraphQL Engine v2 compiled to
113
+ * WebAssembly, executing SQL against pglite. If `metadataJson` is provided the
114
+ * schema cache is built eagerly; otherwise call `init()` yourself.
115
+ */
116
+ export function createSimulator(args?: CreateSimulatorArgs): Promise<Simulator>;
117
+
118
+ /**
119
+ * Run one Haskell-bridge request against a pglite connection via the raw wire
120
+ * protocol. Exported for tests; normally reached through the wasm bridge.
121
+ */
122
+ export function execOnPglite(db: PGliteInterface, requestJson: string): Promise<string>;
123
+
124
+ /**
125
+ * Normalise a remote-schema definition to the introspection *response* shape
126
+ * the engine expects (`{ data: { __schema: … } }`).
127
+ */
128
+ export function remoteSchemaToIntrospection(def: RemoteSchemaDef): {
129
+ data: { __schema: IntrospectionQuery['__schema'] };
130
+ };