@supacloud/compiler 0.12.0 → 0.13.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/README.md CHANGED
@@ -9,6 +9,122 @@ SupaCloud 应用静态编译器:读取 `@supacloud/app` 装饰器元数据的
9
9
 
10
10
  本包不依赖 `@supacloud/app`:AST 只按装饰器名匹配(`Module`/`Injectable`/`Inject`/`Command`/`Query`/`Controller`/`Get`/`Post`/`Put`/`Patch`/`Delete`/`defineModule`/`InjectionToken`),不校验 import 来源。
11
11
 
12
+ ## Recommended GraphQL Query Contracts
13
+
14
+ **Database First is the only server-schema model.** Drizzle and SQL declarations
15
+ are migrated to PostgreSQL; `pg_graphql` reflects the actual database, and
16
+ `graphql-schema` exports the intended role's snapshot. Author queries/fragments,
17
+ not GraphQL resolver classes or a separate server SDL. `graphql.schema` accepts
18
+ only local `.graphql`, `.gql` or `.json` snapshots; executable sources, URLs and
19
+ schema-authoring options such as `autoSchemaFile`, `typePaths`, `resolvers` or
20
+ `mode` are rejected. Snapshot formats and TypedDocumentNode output are not
21
+ alternative authoring modes.
22
+
23
+ Do not edit exported snapshots. Change database declarations, apply migrations,
24
+ export again and compile. Offline compilation cannot attest a snapshot's origin;
25
+ `graphql-schema --check` against the intended database detects local/remote drift.
26
+ The starter's synthetic SDL is solely an offline test fixture, not a deployed
27
+ schema. Replace it with a database export before integration.
28
+
29
+ New `supacloud app init` projects preconfigure GraphQL query contracts and include
30
+ an offline example. Existing REST, Command-only and background-task projects
31
+ remain unchanged: general App/CLI configuration and `compileProject(options)`
32
+ only enable this pipeline when `graphql` is explicitly configured. Enabled
33
+ contracts always reject invalid queries and missing schemas, even with
34
+ `strict: false` or `--no-strict`. Choosing `graphql: false` (or `--no-graphql` for
35
+ one compiler run) disables adoption, not just validation of individual queries.
36
+ The platform extension is still opt-in; compilation changes no database grants,
37
+ extensions or production introspection settings.
38
+
39
+ ```ts
40
+ export default defineSupacloudConfig({
41
+ root: "src",
42
+ graphql: { schema: "graphql/schema.graphql" },
43
+ });
44
+ ```
45
+
46
+ The schema path is configuration-relative, query globs are source-root-relative.
47
+ Normal `compile`, `check`, `dev` and `context` handle the local snapshot and
48
+ queries offline. Invalid fields/variables, unnamed operations, mutations and
49
+ subscriptions produce structured diagnostics; failures preserve working output.
50
+ GraphQL.js and GraphQL Code Generator own parsing, validation and operation types.
51
+ Unknown custom scalars stay `unknown` unless explicitly mapped via `graphql.scalars`.
52
+
53
+ ```sh
54
+ supacloud-compiler graphql-schema --url https://your-project.example \
55
+ --key-env SUPACLOUD_PUBLISHABLE_KEY --token-env APP_USER_ACCESS_TOKEN
56
+ supacloud-compiler compile
57
+ supacloud-compiler check --json
58
+ ```
59
+
60
+ Export is explicit and requires introspection already enabled in the selected
61
+ development project. Flags name environment variables, not secrets. Use the
62
+ intended caller role, never a privileged service-role schema for browser queries.
63
+ After migrations, use the same export command with `--check --json` to detect
64
+ remote schema drift without overwriting the local snapshot (exit 1 on drift).
65
+ Refresh intentionally, then run compile/check and the application's typecheck
66
+ and role/RLS tests. This remote gate requires development introspection; offline
67
+ compilation does not require production introspection.
68
+
69
+ Set `graphql.typedDocuments: true` to additionally generate
70
+ `graphql.documents.ts` using the standard TypedDocumentNode Codegen plugin.
71
+ Consumers of that optional file must install `@graphql-typed-document-node/core`;
72
+ it provides `ResultOf` and `VariablesOf` for operation-level inference. The default
73
+ fetch client still needs no GraphQL runtime dependency.
74
+
75
+ Both outputs use the operation plugin as the single owner of referenced enums
76
+ and input objects. Regenerate both files after upgrading the compiler; do not
77
+ deduplicate declarations by editing generated files. Scalar mappings are applied
78
+ directly to operation/input fields, and types unused by queries are not emitted.
79
+
80
+ Consumer acceptance tests cover both SDK and TypedDocumentNode output:
81
+
82
+ ```gherkin
83
+ Scenario: Shared enum types
84
+ Given multiple queries share an enum in variables and selected fields
85
+ When the compiler generates the client artifacts
86
+ Then each artifact declares the enum once and passes strict TypeScript checks
87
+
88
+ Scenario: Nested input objects
89
+ Given recursive input objects contain enum lists, defaults and custom scalars
90
+ When the compiler generates the client artifacts
91
+ Then input declarations are unique and valid variables retain their types
92
+
93
+ Scenario: Invalid consumer code
94
+ Given generated query contracts
95
+ When a consumer supplies invalid variables or reads an unselected field
96
+ Then TypeScript rejects the consumer code
97
+
98
+ Scenario: Release package acceptance
99
+ Given an installed compiler package
100
+ When its CLI runs the same consumer acceptance suite
101
+ Then both artifact formats pass without editing generated files
102
+ ```
103
+
104
+ Run `bun test src/graphql-package.test.ts` from this package. To test an installed
105
+ tarball or registry release with the same suite, set
106
+ `SUPACLOUD_COMPILER_TEST_CLI` to its absolute `dist/cli.js` path.
107
+ `bun run test:package` checks the built CLI and runs automatically after the build
108
+ in `prepublishOnly`, blocking publication when generated consumer types fail.
109
+
110
+ ```ts
111
+ import { createGraphqlClient } from "./generated/graphql";
112
+ const queries = createGraphqlClient({
113
+ url: projectUrl,
114
+ publishableKey,
115
+ getAccessToken: readCurrentUserToken,
116
+ });
117
+ const result = await queries.ReviewList({ first: 20 });
118
+ ```
119
+
120
+ Method names and types come from named operations. The generated client is
121
+ dependency-free and refreshes identity per request; `getSdk(requester)` integrates
122
+ an existing transport. It rejects HTTP errors, GraphQL errors and malformed
123
+ response envelopes, but does not runtime-decode selected field values.
124
+ `graphql.manifest.json` records query locations and the schema hash; context packs
125
+ include colocated queries. RLS/grants, real database acceptance and query resource
126
+ limits remain deployment responsibilities. Business writes stay in Commands.
127
+
12
128
  ## 安装
13
129
 
14
130
  ```bash
@@ -217,7 +333,7 @@ supacloud-compiler check --root ./app --out ./app/generated --strict
217
333
  supacloud-compiler dev --root . --out ./generated
218
334
  ```
219
335
 
220
- 开发模式默认对源码变化做 100ms 防抖,并只监听 TypeScript 文件。编译器会复用进程内的源码快照:相同输入直接命中缓存;只改动普通实现文件时复用既有依赖图和生成物;只有 SupaCloud 元数据、模块声明或依赖相关文件变化时才重建图。编译失败时不会覆盖最后一次成功的 `application.ts` 和 `app.manifest.json`;修复错误后会自动生成新产物。`--debounce <ms>` 可调整防抖时间。
336
+ 开发模式默认对源码变化做 100ms 防抖,监听 TypeScript 文件;启用 GraphQL 后,还监听查询文件和配置的 Schema 快照。编译器会复用进程内的源码快照:相同输入直接命中缓存;只改动普通实现文件时复用既有依赖图和生成物;只有 SupaCloud 元数据、模块声明或依赖相关文件变化时才重建图。编译失败时不会覆盖最后一次成功的 `application.ts` 和 `app.manifest.json`;修复错误后会自动生成新产物。`--debounce <ms>` 可调整防抖时间。
221
337
 
222
338
  ## 图谱与诊断
223
339