@tangen/graphql 0.0.0-stage → 0.1.25

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.
@@ -0,0 +1,30 @@
1
+ /**
2
+ * File Name: loaders.ts
3
+ * Purpose: A tiny per-request batch loader — the N+1 killer for relation
4
+ * fields. Resolvers enqueue (targetType, id) lookups; the loader
5
+ * coalesces every id requested for a type within a tick into ONE
6
+ * `where({ id: { in: [...] } })` query, then fans the rows back
7
+ * out by id. Hand-rolled (≈40 lines) to keep the package
8
+ * dependency-light — no `dataloader`.
9
+ *
10
+ * Author: Raghunath J
11
+ * License: MIT
12
+ */
13
+ type Row = Record<string, unknown>;
14
+ interface QueryClient {
15
+ objects: (type: string) => {
16
+ where: (filter: unknown) => any;
17
+ };
18
+ }
19
+ export interface BatchLoaders {
20
+ /** Load one target object by id (batched with sibling loads of the same type). */
21
+ load: (targetType: string, id: string) => Promise<Row | null>;
22
+ /** Load many by ids, preserving order (missing ids drop out). */
23
+ loadMany: (targetType: string, ids: string[]) => Promise<Row[]>;
24
+ }
25
+ /**
26
+ * Creates the per-request loaders. `canRead` gates each target type — an
27
+ * ungranted type yields null rows rather than leaking data.
28
+ */
29
+ export declare function createBatchLoaders(client: QueryClient, canRead: (type: string) => boolean): BatchLoaders;
30
+ export {};
@@ -0,0 +1,11 @@
1
+ /**
2
+ * File Name: naming.ts
3
+ * Purpose: GraphQL name derivation for the builder and resolvers. The rules now
4
+ * live in @tangen/types/graphql-naming so push-time collision checks
5
+ * (@tangen/schema) and the actual schema build agree on every name;
6
+ * this module just re-exports them for the graphql package's callers.
7
+ *
8
+ * Author: Raghunath J
9
+ * License: MIT
10
+ */
11
+ export { pascal, pluralize, singularize, typeName, singleQueryName, listQueryName, listQueryNameFor, connectionName, whereInputName, orderByName, } from '@tangen/types';
@@ -0,0 +1,21 @@
1
+ /**
2
+ * File Name: permissions.ts
3
+ * Purpose: The object-access gate, re-exported for GraphQL's consumers.
4
+ *
5
+ * The rule itself moved to `@tangen/types`, because it never was
6
+ * GraphQL's: this file's own comment used to say it "mirrors the
7
+ * object-read logic REST/MCP use", and a rule three surfaces must
8
+ * agree on belongs to none of them. It stayed here long enough for
9
+ * `realtime/authorize.ts` to import it, which resolved this whole
10
+ * package at boot in every process and quietly undid the lazy
11
+ * import `routes/graphql.ts` arranges on purpose.
12
+ *
13
+ * Author: Raghunath J
14
+ * License: MIT
15
+ */
16
+ import type { ApiKeyPermissions } from '@tangen/types';
17
+ export { canReadType, canReadDraftsType, canPublishTypeObjects } from '@tangen/types';
18
+ /** Can this caller WRITE the given content type? (objects:write) */
19
+ export declare function canWriteType(permissions: ApiKeyPermissions | {
20
+ admin: true;
21
+ } | undefined, typeName: string): boolean;
@@ -0,0 +1,87 @@
1
+ /**
2
+ * File Name: resolvers.ts
3
+ * Purpose: The resolver-hook contract the schema-builder wires in, plus the
4
+ * implementations (G2): list/single queries and relation fields
5
+ * over an injected local client. Relations go through a per-request
6
+ * batch loader (no N+1); every content-type access is gated by an
7
+ * injected canRead so GraphQL matches REST's permission model.
8
+ *
9
+ * Author: Raghunath J
10
+ * License: MIT
11
+ *
12
+ * Dependencies/Notes: Resolvers read the client / canRead / loaders from the
13
+ * GraphQL contextValue (createRequestContext), so the hooks are
14
+ * static and the builder imports no I/O.
15
+ */
16
+ import { type GraphQLFieldResolver } from 'graphql';
17
+ import { type BatchLoaders } from './loaders';
18
+ import type { RealtimeSubscriber } from './subscriptions';
19
+ type Row = Record<string, unknown>;
20
+ /** What resolvers read off the GraphQL contextValue. */
21
+ export interface GraphqlRequestContext {
22
+ client: {
23
+ objects: (type: string) => any;
24
+ table?: (name: string) => any;
25
+ globals?: (name: string) => any;
26
+ };
27
+ canRead: (typeName: string) => boolean;
28
+ canWrite: (typeName: string) => boolean;
29
+ /** K0: publish, unpublish and preflight need objects:publish, not a write grant. */
30
+ canPublish?: (typeName: string) => boolean;
31
+ canReadDrafts?: (typeName: string) => boolean;
32
+ loaders: BatchLoaders;
33
+ /** Realtime R5: present only on the WebSocket adapter; subscription fields refuse without it. */
34
+ realtime?: RealtimeSubscriber;
35
+ }
36
+ /** Builds the per-request context (call once per request in the mount). */
37
+ export declare function createRequestContext(input: {
38
+ client: GraphqlRequestContext['client'];
39
+ canRead: (typeName: string) => boolean;
40
+ /** Absent ⇒ writes denied (read-only mount). */
41
+ canWrite?: (typeName: string) => boolean;
42
+ /** Absent ⇒ draft reads denied (objects:read_draft — preview keys). */
43
+ canReadDrafts?: (typeName: string) => boolean;
44
+ /** Absent ⇒ publish, unpublish and preflight denied (objects:publish). */
45
+ canPublish?: (typeName: string) => boolean;
46
+ /** Realtime R5: the per-connection subscriber the WebSocket adapter installs; absent over POST. */
47
+ realtime?: RealtimeSubscriber;
48
+ }): GraphqlRequestContext;
49
+ /**
50
+ * The resolver hooks. Each factory returns a static resolver that reads the
51
+ * request context — so nothing here is per-request state.
52
+ */
53
+ export declare function createResolvers(): {
54
+ list: (type: string) => GraphQLFieldResolver<unknown, GraphqlRequestContext>;
55
+ single: (type: string) => GraphQLFieldResolver<unknown, GraphqlRequestContext>;
56
+ relationOne: (_parent: string, fieldName: string, targetType: string) => GraphQLFieldResolver<Row, GraphqlRequestContext>;
57
+ relationMany: (_parent: string, fieldName: string, targetType: string) => GraphQLFieldResolver<Row, GraphqlRequestContext>;
58
+ objectRef: (_parent: string, fieldName: string) => GraphQLFieldResolver<Row, GraphqlRequestContext>;
59
+ table: (name: string) => GraphQLFieldResolver<unknown, GraphqlRequestContext>;
60
+ global: () => GraphQLFieldResolver<unknown, GraphqlRequestContext>;
61
+ create: (type: string) => GraphQLFieldResolver<unknown, GraphqlRequestContext>;
62
+ update: (type: string) => GraphQLFieldResolver<unknown, GraphqlRequestContext>;
63
+ publish: (type: string) => GraphQLFieldResolver<unknown, GraphqlRequestContext>;
64
+ preflight: (type: string) => GraphQLFieldResolver<unknown, GraphqlRequestContext>;
65
+ unpublish: (type: string) => GraphQLFieldResolver<unknown, GraphqlRequestContext>;
66
+ remove: (type: string) => GraphQLFieldResolver<unknown, GraphqlRequestContext>;
67
+ };
68
+ /**
69
+ * Factory hooks the schema-builder attaches. Each returns the actual GraphQL
70
+ * resolver for a given type (or relation field).
71
+ */
72
+ export interface ResolverHooks {
73
+ list?: (typeName: string) => GraphQLFieldResolver<unknown, unknown>;
74
+ single?: (typeName: string) => GraphQLFieldResolver<unknown, unknown>;
75
+ relationOne?: (parentType: string, fieldName: string, targetType: string) => GraphQLFieldResolver<unknown, unknown>;
76
+ relationMany?: (parentType: string, fieldName: string, targetType: string) => GraphQLFieldResolver<unknown, unknown>;
77
+ objectRef?: (parentType: string, fieldName: string) => GraphQLFieldResolver<unknown, unknown>;
78
+ table?: (name: string) => GraphQLFieldResolver<unknown, unknown>;
79
+ global?: () => GraphQLFieldResolver<unknown, unknown>;
80
+ create?: (typeName: string) => GraphQLFieldResolver<unknown, unknown>;
81
+ update?: (typeName: string) => GraphQLFieldResolver<unknown, unknown>;
82
+ publish?: (typeName: string) => GraphQLFieldResolver<unknown, unknown>;
83
+ preflight?: (typeName: string) => GraphQLFieldResolver<unknown, unknown>;
84
+ unpublish?: (typeName: string) => GraphQLFieldResolver<unknown, unknown>;
85
+ remove?: (typeName: string) => GraphQLFieldResolver<unknown, unknown>;
86
+ }
87
+ export {};
@@ -0,0 +1,15 @@
1
+ /**
2
+ * File Name: scalars.ts
3
+ * Purpose: Custom GraphQL scalars for content field types the built-ins
4
+ * don't cover — JSON (repeaters, components, groups, arbitrary
5
+ * blobs), DateTime (ISO strings), and Media (a thin object type is
6
+ * built in the schema-builder; the scalar here is the fallback).
7
+ *
8
+ * Author: Raghunath J
9
+ * License: MIT
10
+ */
11
+ import { GraphQLScalarType } from 'graphql';
12
+ /** Passes JSON-serializable values straight through (no transform). */
13
+ export declare const GraphQLJSON: GraphQLScalarType<unknown, unknown>;
14
+ /** ISO-8601 timestamps, carried as strings. */
15
+ export declare const GraphQLDateTime: GraphQLScalarType<unknown, unknown>;
@@ -0,0 +1,51 @@
1
+ /**
2
+ * File Name: schema-builder.ts
3
+ * Purpose: The heart of @tangen/graphql — turns a content SchemaDefinition
4
+ * into a GraphQLSchema. Each type becomes an object type (system
5
+ * fields + mapped content fields); relations wire to the target
6
+ * type (single) or its Connection (many); every type gets a list
7
+ * query (where/orderBy/limit/offset/after/locale → Connection) and
8
+ * a single query (id/slug). WhereInput + OrderBy inputs are
9
+ * generated per type. Field/type object references are thunked so
10
+ * self- and mutual-recursion build safely.
11
+ *
12
+ * Author: Raghunath J
13
+ * License: MIT
14
+ *
15
+ * Dependencies/Notes: Pure — no I/O. Resolvers are attached separately (G2)
16
+ * so this stays unit-testable. Unsupported field types degrade to
17
+ * the JSON scalar rather than crashing the schema.
18
+ */
19
+ import { GraphQLSchema } from 'graphql';
20
+ import type { SchemaDefinition } from '@tangen/types';
21
+ export interface CustomTableInput {
22
+ name: string;
23
+ columns: Record<string, {
24
+ type: string;
25
+ nullable?: boolean;
26
+ }>;
27
+ }
28
+ export interface BuildSchemaOptions {
29
+ /** Attach resolvers by wiring these hooks (G2 supplies them). */
30
+ resolvers?: import('./resolvers').ResolverHooks;
31
+ /** Custom tables → table_<name> queries over a generated row type. */
32
+ customTables?: CustomTableInput[];
33
+ /** Global names → a global(name) query (returns the JSON blob). */
34
+ globalNames?: string[];
35
+ /** Generate create/update/publish/unpublish/delete mutations per type. */
36
+ mutations?: boolean;
37
+ /** Reusable component registry — resolves string refs in component fields (NR-3 typed blocks). */
38
+ componentRegistry?: Array<{
39
+ name: string;
40
+ label?: string;
41
+ fields?: Array<Record<string, unknown>>;
42
+ }>;
43
+ /**
44
+ * Realtime R5: generate the Subscription type (`<type>Changed`,
45
+ * `objectsChanged`) over the realtime envelopes. The API mount sets it
46
+ * only when a realtime hub is running in the process, so introspection
47
+ * never advertises what the deployment cannot serve.
48
+ */
49
+ subscriptions?: boolean;
50
+ }
51
+ export declare function buildContentSchema(schema: SchemaDefinition, opts?: BuildSchemaOptions): GraphQLSchema;
@@ -0,0 +1,82 @@
1
+ /**
2
+ * File Name: subscriptions.ts
3
+ * Purpose: The generated Subscription type - one `<type>Changed(id)` field per
4
+ * content type plus the generic `objectsChanged(type, id)` - and the
5
+ * helpers a transport needs to run one: the topic a field maps to,
6
+ * and a parse-validate step that admits subscription operations
7
+ * only.
8
+ *
9
+ * Author: Raghunath J
10
+ * License: MIT
11
+ *
12
+ * Dependencies/Notes: Realtime R5 of tasks/plan-realtime.md, built to
13
+ * tasks/realtime-client-architecture.md sections 14 and 56.9:
14
+ * realtime signals, it does not replicate, and REST/GraphQL stay
15
+ * the canonical data API. A subscription therefore yields the SAME
16
+ * envelope the realtime client receives on `objects:<type>` or
17
+ * `object:<id>` - ids, the type slug, the changed field names, the
18
+ * actor - and never the document; the page refetches through a
19
+ * query. Pure: the builder knows nothing about sockets. The
20
+ * `subscribe` resolver reads a `RealtimeSubscriber` off the request
21
+ * context, which only the WebSocket adapter installs
22
+ * (packages/api/src/realtime/graphql-ws.ts); over POST the field
23
+ * answers an error naming the socket, so there is one realtime
24
+ * transport and GraphQL is an adapter over it. Authorization is the
25
+ * hub's (the subscriber refuses a topic the identity may not hold);
26
+ * the `canRead` check here only makes the refusal read like a
27
+ * query's.
28
+ */
29
+ import { GraphQLObjectType, GraphQLError, type GraphQLSchema, type DocumentNode, type GraphQLScalarType } from 'graphql';
30
+ /** The realtime envelope as a transport hands it over: what `{ op: 'event' }` carries. */
31
+ export interface RealtimeEventLike {
32
+ id: string;
33
+ ch: string;
34
+ topic: string;
35
+ t: number;
36
+ type: string;
37
+ data: Record<string, unknown>;
38
+ }
39
+ /**
40
+ * What a subscription resolver needs of the realtime layer: one topic in,
41
+ * the envelopes out, ending when the connection does. A refused topic
42
+ * rejects the first `next()` with an Error whose message is the reason.
43
+ */
44
+ export interface RealtimeSubscriber {
45
+ subscribe(topic: string): AsyncIterable<RealtimeEventLike>;
46
+ }
47
+ /**
48
+ * The topic a subscription field listens on: the one object when an id is
49
+ * given, else every object of the type. The same strings the REST client
50
+ * subscribes with, so both hear one publish.
51
+ */
52
+ export declare function subscriptionTopic(args: {
53
+ type: string;
54
+ id?: string | null;
55
+ }): string;
56
+ /**
57
+ * Builds the Subscription root for the given content types.
58
+ * @param types - The content types in the schema (disabled ones already dropped).
59
+ * @param scalars - The JSON and DateTime scalars the schema already registers.
60
+ */
61
+ export declare function buildSubscriptionType(types: Array<{
62
+ name: string;
63
+ }>, scalars: {
64
+ JSON: GraphQLScalarType;
65
+ DateTime: GraphQLScalarType;
66
+ }): GraphQLObjectType;
67
+ export type PreparedSubscription = {
68
+ ok: true;
69
+ document: DocumentNode;
70
+ } | {
71
+ ok: false;
72
+ errors: GraphQLError[];
73
+ };
74
+ /**
75
+ * Parses and validates one document for the socket: only a subscription
76
+ * operation is admitted there, because queries and mutations belong to the
77
+ * HTTP mount with its depth ceiling and access log. Never throws.
78
+ * @param schema - The generated schema with its Subscription type.
79
+ * @param source - The document text.
80
+ * @param operationName - Which operation, when the document holds several.
81
+ */
82
+ export declare function prepareSubscription(schema: GraphQLSchema, source: string, operationName?: string | null): PreparedSubscription;
package/package.json CHANGED
@@ -1,6 +1,50 @@
1
1
  {
2
2
  "name": "@tangen/graphql",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.1.25",
4
+ "description": "GraphQL API for Tangen CMS — a schema generated from your content model. Optional: install only when you enable GraphQL.",
5
+ "homepage": "https://github.com/raghunathj/tangen#readme",
6
+ "bugs": {
7
+ "url": "https://github.com/raghunathj/tangen/issues"
8
+ },
9
+ "license": "MIT",
10
+ "repository": {
11
+ "type": "git",
12
+ "url": "git+https://github.com/raghunathj/tangen.git",
13
+ "directory": "packages/graphql"
14
+ },
15
+ "type": "module",
16
+ "main": "./dist/index.js",
17
+ "types": "./dist/index.d.ts",
18
+ "files": [
19
+ "dist",
20
+ "README.md"
21
+ ],
22
+ "publishConfig": {
23
+ "access": "public",
24
+ "main": "./dist/index.js",
25
+ "types": "./dist/index.d.ts",
26
+ "exports": {
27
+ ".": {
28
+ "types": "./dist/index.d.ts",
29
+ "import": "./dist/index.js",
30
+ "default": "./dist/index.js"
31
+ }
32
+ }
33
+ },
34
+ "scripts": {
35
+ "build": "bun build ./src/index.ts --outdir dist --target node --format esm --packages external && tsc --emitDeclarationOnly -p tsconfig.build.json",
36
+ "typecheck": "tsc --noEmit",
37
+ "test": "bun test"
38
+ },
39
+ "dependencies": {
40
+ "@tangen/types": "^0.2.0",
41
+ "graphql": "^17.0.0"
42
+ },
43
+ "exports": {
44
+ ".": {
45
+ "types": "./dist/index.d.ts",
46
+ "import": "./dist/index.js",
47
+ "default": "./dist/index.js"
48
+ }
49
+ }
50
+ }
package/README.md DELETED
@@ -1,3 +0,0 @@
1
- # Temporary Holding Version
2
-
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.