polen 0.11.0-next.1 → 0.11.0-next.10

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 (123) hide show
  1. package/build/api/config/configurator.d.ts +35 -23
  2. package/build/api/config/configurator.d.ts.map +1 -1
  3. package/build/api/config/configurator.js +0 -6
  4. package/build/api/config/configurator.js.map +1 -1
  5. package/build/api/config/merge.d.ts.map +1 -1
  6. package/build/api/config/merge.js +0 -11
  7. package/build/api/config/merge.js.map +1 -1
  8. package/build/api/content/metadata.d.ts +8 -2
  9. package/build/api/content/metadata.d.ts.map +1 -1
  10. package/build/api/content/metadata.js +1 -1
  11. package/build/api/content/metadata.js.map +1 -1
  12. package/build/api/schema/data-sources/data-sources.d.ts +1 -0
  13. package/build/api/schema/data-sources/data-sources.d.ts.map +1 -1
  14. package/build/api/schema/data-sources/data-sources.js +1 -0
  15. package/build/api/schema/data-sources/data-sources.js.map +1 -1
  16. package/build/api/schema/data-sources/introspection/introspection.d.ts +83 -0
  17. package/build/api/schema/data-sources/introspection/introspection.d.ts.map +1 -0
  18. package/build/api/schema/data-sources/introspection/introspection.js +110 -0
  19. package/build/api/schema/data-sources/introspection/introspection.js.map +1 -0
  20. package/build/api/schema/metadata.d.ts +11 -3
  21. package/build/api/schema/metadata.d.ts.map +1 -1
  22. package/build/api/schema/metadata.js +1 -1
  23. package/build/api/schema/metadata.js.map +1 -1
  24. package/build/api/schema/read.d.ts +83 -9
  25. package/build/api/schema/read.d.ts.map +1 -1
  26. package/build/api/schema/read.js +15 -6
  27. package/build/api/schema/read.js.map +1 -1
  28. package/build/api/schema-source/schema-source.d.ts +1 -1
  29. package/build/api/schema-source/schema-source.d.ts.map +1 -1
  30. package/build/api/schema-source/schema-source.js.map +1 -1
  31. package/build/api/static/manifest.d.ts +1 -2
  32. package/build/api/static/manifest.d.ts.map +1 -1
  33. package/build/api/static/manifest.js +1 -1
  34. package/build/api/static/manifest.js.map +1 -1
  35. package/build/api/vite/plugins/build.d.ts.map +1 -1
  36. package/build/api/vite/plugins/build.js +3 -0
  37. package/build/api/vite/plugins/build.js.map +1 -1
  38. package/build/api/vite/plugins/core.js +8 -8
  39. package/build/api/vite/plugins/core.js.map +1 -1
  40. package/build/api/vite/plugins/main.d.ts.map +1 -1
  41. package/build/api/vite/plugins/main.js +1 -8
  42. package/build/api/vite/plugins/main.js.map +1 -1
  43. package/build/api/vite/plugins/schema-assets.d.ts.map +1 -1
  44. package/build/api/vite/plugins/schema-assets.js +52 -11
  45. package/build/api/vite/plugins/schema-assets.js.map +1 -1
  46. package/build/cli/commands/open.js +1 -1
  47. package/build/cli/commands/open.js.map +1 -1
  48. package/build/lib/grafaid/schema/schema.d.ts +1 -1
  49. package/build/lib/grafaid/schema/schema.d.ts.map +1 -1
  50. package/build/lib/grafaid/schema/schema.js +1 -1
  51. package/build/lib/grafaid/schema/schema.js.map +1 -1
  52. package/build/lib/kit-temp.js +2 -2
  53. package/build/lib/kit-temp.js.map +1 -1
  54. package/build/lib/react-router-aid/react-router-aid.d.ts +5 -3
  55. package/build/lib/react-router-aid/react-router-aid.d.ts.map +1 -1
  56. package/build/lib/react-router-aid/react-router-aid.js +7 -4
  57. package/build/lib/react-router-aid/react-router-aid.js.map +1 -1
  58. package/build/template/routes/changelog.js +2 -2
  59. package/build/template/routes/changelog.js.map +1 -1
  60. package/build/template/routes/index.js +2 -2
  61. package/build/template/routes/index.js.map +1 -1
  62. package/build/template/routes/pages.js +2 -2
  63. package/build/template/routes/pages.js.map +1 -1
  64. package/build/template/routes/reference.d.ts +27 -4
  65. package/build/template/routes/reference.d.ts.map +1 -1
  66. package/build/template/routes/reference.js +81 -20
  67. package/build/template/routes/reference.js.map +1 -1
  68. package/build/template/routes/root.js +3 -3
  69. package/build/template/routes/root.js.map +1 -1
  70. package/build/template/server/ssg/get-route-paths.d.ts.map +1 -1
  71. package/build/template/server/ssg/get-route-paths.js +42 -3
  72. package/build/template/server/ssg/get-route-paths.js.map +1 -1
  73. package/build/template/sources/schema-source.d.ts +1 -1
  74. package/build/template/sources/schema-source.d.ts.map +1 -1
  75. package/build/template/sources/schema-source.js +2 -1
  76. package/build/template/sources/schema-source.js.map +1 -1
  77. package/package.json +16 -17
  78. package/src/api/config/configurator.ts +35 -30
  79. package/src/api/config/merge.ts +0 -16
  80. package/src/api/content/metadata.ts +1 -1
  81. package/src/api/schema/data-sources/data-sources.ts +1 -0
  82. package/src/api/schema/data-sources/introspection/introspection.ts +213 -0
  83. package/src/api/schema/metadata.ts +1 -1
  84. package/src/api/schema/read.ts +107 -16
  85. package/src/api/schema-source/schema-source.ts +3 -3
  86. package/src/api/static/manifest.ts +1 -1
  87. package/src/api/vite/plugins/build.ts +3 -0
  88. package/src/api/vite/plugins/core.ts +8 -8
  89. package/src/api/vite/plugins/main.ts +1 -9
  90. package/src/api/vite/plugins/schema-assets.ts +59 -12
  91. package/src/cli/commands/open.ts +1 -1
  92. package/src/lib/grafaid/schema/schema.ts +1 -0
  93. package/src/lib/kit-temp.ts +2 -2
  94. package/src/lib/mask/$.test.ts +3 -3
  95. package/src/lib/react-router-aid/react-router-aid.ts +12 -6
  96. package/src/template/routes/changelog.tsx +2 -2
  97. package/src/template/routes/index.tsx +2 -2
  98. package/src/template/routes/pages.tsx +2 -2
  99. package/src/template/routes/reference.tsx +88 -20
  100. package/src/template/routes/root.tsx +3 -3
  101. package/src/template/server/ssg/get-route-paths.test.ts +132 -0
  102. package/src/template/server/ssg/get-route-paths.ts +41 -3
  103. package/src/template/sources/schema-source.ts +2 -1
  104. package/build/template/routes/reference.$type.$field.d.ts +0 -5
  105. package/build/template/routes/reference.$type.$field.d.ts.map +0 -1
  106. package/build/template/routes/reference.$type.$field.js +0 -31
  107. package/build/template/routes/reference.$type.$field.js.map +0 -1
  108. package/build/template/routes/reference.$type.d.ts +0 -9
  109. package/build/template/routes/reference.$type.d.ts.map +0 -1
  110. package/build/template/routes/reference.$type.js +0 -25
  111. package/build/template/routes/reference.$type.js.map +0 -1
  112. package/build/template/routes/reference.version.$version.$type.$field.d.ts +0 -6
  113. package/build/template/routes/reference.version.$version.$type.$field.d.ts.map +0 -1
  114. package/build/template/routes/reference.version.$version.$type.$field.js +0 -32
  115. package/build/template/routes/reference.version.$version.$type.$field.js.map +0 -1
  116. package/build/template/routes/reference.version.$version.$type.d.ts +0 -11
  117. package/build/template/routes/reference.version.$version.$type.d.ts.map +0 -1
  118. package/build/template/routes/reference.version.$version.$type.js +0 -26
  119. package/build/template/routes/reference.version.$version.$type.js.map +0 -1
  120. package/src/template/routes/reference.$type.$field.tsx +0 -34
  121. package/src/template/routes/reference.$type.tsx +0 -29
  122. package/src/template/routes/reference.version.$version.$type.$field.tsx +0 -35
  123. package/src/template/routes/reference.version.$version.$type.tsx +0 -30
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "polen",
3
- "version": "0.11.0-next.1",
3
+ "version": "0.11.0-next.10",
4
4
  "type": "module",
5
5
  "description": "A framework for delightful GraphQL developer portals",
6
6
  "author": {
@@ -87,18 +87,18 @@
87
87
  "@graphql-inspector/core": "^6.2.1",
88
88
  "@graphql-tools/load": "^8.1.0",
89
89
  "@hiogawa/vite-plugin-ssr-css": "^0.0.1",
90
- "@hono/node-server": "^1.15.0",
90
+ "@hono/node-server": "^1.16.0",
91
91
  "@mdx-js/mdx": "^3.1.0",
92
92
  "@mdx-js/react": "^3.1.0",
93
93
  "@mdx-js/rollup": "^3.1.0",
94
94
  "@radix-ui/react-icons": "^1.3.2",
95
95
  "@radix-ui/themes": "^3.2.1",
96
- "@rolldown/pluginutils": "1.0.0-beta.24",
96
+ "@rolldown/pluginutils": "1.0.0-beta.27",
97
97
  "@rollup/pluginutils": "^5.2.0",
98
98
  "@types/jsesc": "^3.0.3",
99
99
  "@vitejs/plugin-react": "^4.6.0",
100
100
  "@vitejs/plugin-react-oxc": "^0.2.3",
101
- "@vltpkg/semver": "0.0.0-16",
101
+ "@vltpkg/semver": "0.0.0-17",
102
102
  "@vue/reactivity": "^3.5.17",
103
103
  "@wollybeard/kit": "^0.41.0",
104
104
  "@wollybeard/projector": "^0.3.0",
@@ -107,12 +107,12 @@
107
107
  "codehike": "^1.0.7",
108
108
  "consola": "^3.4.2",
109
109
  "defu": "^6.1.4",
110
- "es-toolkit": "^1.39.6",
110
+ "es-toolkit": "^1.39.7",
111
111
  "fuse.js": "^7.1.0",
112
112
  "graffle": "8.0.0-next.163",
113
113
  "graphql": "^16.11.0",
114
114
  "gray-matter": "^4.0.3",
115
- "hono": "^4.8.4",
115
+ "hono": "^4.8.5",
116
116
  "jsesc": "^3.1.0",
117
117
  "jsonc-parser": "^3.3.1",
118
118
  "react": "^19.1.0",
@@ -126,22 +126,21 @@
126
126
  "remark-parse": "^11.0.0",
127
127
  "remark-rehype": "^11.1.2",
128
128
  "resolve.imports": "^2.0.3",
129
- "rolldown": "1.0.0-beta.24",
129
+ "rolldown": "1.0.0-beta.27",
130
130
  "simple-git": "^3.28.0",
131
131
  "source-map": "^0.7.4",
132
132
  "superjson": "^2.2.2",
133
133
  "tinyglobby": "^0.2.14",
134
- "tree-sitter-graphql-grammar-wasm": "^1.0.0",
134
+ "tree-sitter-graphql-grammar-wasm": "^1.1.0",
135
135
  "tsx": "^4.20.3",
136
136
  "typescript": "^5.8.3",
137
137
  "unified": "^11.0.5",
138
138
  "vfile": "^6.0.3",
139
- "vite": "npm:rolldown-vite@^7.0.4",
139
+ "vite": "npm:rolldown-vite@^7.0.9",
140
140
  "vite-plugin-inspect": "^11.3.0",
141
- "vite-plugin-restart": "^0.4.2",
142
- "web-tree-sitter": "^0.25.6",
141
+ "web-tree-sitter": "^0.25.8",
143
142
  "youch-core": "^0.3.3",
144
- "zx": "8.6.1"
143
+ "zx": "8.7.1"
145
144
  },
146
145
  "devDependencies": {
147
146
  "@actions/core": "^1.11.1",
@@ -151,15 +150,15 @@
151
150
  "@actions/io": "^1.1.3",
152
151
  "@changesets/cli": "^2.29.5",
153
152
  "@molt/command": "^0.9.0",
154
- "@octokit/core": "^7.0.2",
153
+ "@octokit/core": "^7.0.3",
155
154
  "@octokit/types": "^14.1.0",
156
- "@playwright/browser-chromium": "^1.53.2",
155
+ "@playwright/browser-chromium": "^1.54.1",
157
156
  "@testing-library/react": "^16.3.0",
158
157
  "@tsconfig/node-lts": "^22.0.2",
159
158
  "@tsconfig/node22": "^22.0.2",
160
159
  "@tsconfig/strictest": "^2.0.5",
161
160
  "@types/jsdom": "^21.1.7",
162
- "@types/node": "^24.0.10",
161
+ "@types/node": "^24.0.14",
163
162
  "@types/react": "^19.1.8",
164
163
  "@types/react-dom": "^19.1.6",
165
164
  "@types/semver": "^7.7.0",
@@ -167,10 +166,10 @@
167
166
  "dripip": "^0.10.0",
168
167
  "fast-check": "^4.2.0",
169
168
  "fs-jetpack": "^5.1.0",
170
- "get-port-please": "^3.1.2",
169
+ "get-port-please": "^3.2.0",
171
170
  "globals": "^16.3.0",
172
171
  "jsdom": "^26.1.0",
173
- "playwright": "1.53.2",
172
+ "playwright": "1.54.1",
174
173
  "publint": "^0.3.12",
175
174
  "semver": "^7.7.2",
176
175
  "strip-ansi": "^7.1.0",
@@ -31,25 +31,56 @@ export interface ConfigInput {
31
31
  * - `file` - Load from a single SDL file (default: schema.graphql)
32
32
  * - `directory` - Load multiple SDL files with date prefixes (enables changelog)
33
33
  * - `memory` - Define schemas programmatically in configuration
34
+ * - `introspection` - Fetch schema from a GraphQL endpoint
35
+ * - `data` - Use pre-built schema objects
34
36
  *
35
37
  * @example
36
38
  * ```ts
37
- * // Single file
39
+ * // Default: looks for schema.graphql
40
+ * schema: {}
41
+ *
42
+ * // Load via introspection
38
43
  * schema: {
39
- * useDataSources: 'file',
40
44
  * dataSources: {
41
- * file: { path: './my-schema.graphql' }
45
+ * introspection: {
46
+ * url: 'https://api.example.com/graphql',
47
+ * headers: { 'Authorization': `Bearer ${process.env.API_TOKEN}` }
48
+ * }
42
49
  * }
43
50
  * }
44
51
  *
45
52
  * // Multiple versions for changelog
46
53
  * schema: {
47
- * useDataSources: 'directory',
48
54
  * dataSources: {
49
55
  * directory: { path: './schema' }
50
56
  * }
51
57
  * }
58
+ *
59
+ * // Custom source order
60
+ * schema: {
61
+ * useDataSources: ['introspection', 'file'],
62
+ * dataSources: {
63
+ * introspection: { url: 'https://api.example.com/graphql' },
64
+ * file: { path: './fallback.graphql' }
65
+ * }
66
+ * }
52
67
  * ```
68
+ *
69
+ * **Two introspection features**:
70
+ * 1. **File Convention**: Polen auto-detects `schema.introspection.json` files
71
+ * 2. **Config-driven**: Polen fetches and caches introspection for you
72
+ *
73
+ * **Interoperability**: The `schema.introspection.json` file uses the standard
74
+ * GraphQL introspection format, compatible with GraphQL Codegen, Apollo CLI, etc.
75
+ *
76
+ * **Lifecycle**:
77
+ * - First run: Fetches from endpoint, saves to `schema.introspection.json`
78
+ * - Subsequent runs: Loads from JSON file (no network request)
79
+ * - To refresh: Delete the JSON file
80
+ * - Runs during `polen dev` and `polen build`, never at runtime
81
+ *
82
+ * **Query details**: Uses the standard introspection query from the GraphQL spec
83
+ * @see https://spec.graphql.org/draft/#sec-Introspection
53
84
  */
54
85
  schema?: SchemaConfigInput
55
86
  /**
@@ -240,22 +271,6 @@ export interface ConfigInput {
240
271
  * @default false
241
272
  */
242
273
  isSelfContainedMode?: boolean
243
- /**
244
- * Tweak the watch behavior.
245
- */
246
- watch?: {
247
- /**
248
- * Restart the development server when some arbitrary files change.
249
- *
250
- * Use this to restart when files that are not already watched by vite change.
251
- *
252
- * @see https://github.com/antfu/vite-plugin-restart
253
- */
254
- /**
255
- * File paths to watch and restart the development server when they change.
256
- */
257
- also?: string[]
258
- }
259
274
  /**
260
275
  * Whether to enable debug mode.
261
276
  *
@@ -357,9 +372,6 @@ export interface Config {
357
372
  assets: string
358
373
  }
359
374
  }
360
- watch: {
361
- also: string[]
362
- }
363
375
  templateVariables: TemplateVariables
364
376
  schemaAugmentations: SchemaAugmentation.Augmentation[]
365
377
  schema: null | SchemaConfigInput
@@ -431,9 +443,6 @@ const configInputDefaults: Config = {
431
443
  title: `My Developer Portal`,
432
444
  },
433
445
  schemaAugmentations: [],
434
- watch: {
435
- also: [],
436
- },
437
446
  build: {
438
447
  architecture: BuildArchitecture.enum.ssg,
439
448
  base: `/`,
@@ -541,10 +550,6 @@ export const normalizeInput = async (
541
550
  config.advanced.explorer = configInput.advanced.explorer
542
551
  }
543
552
 
544
- if (configInput?.advanced?.watch?.also) {
545
- config.watch.also = configInput.advanced.watch.also
546
- }
547
-
548
553
  if (configInput?.server?.port !== undefined) {
549
554
  config.server.port = configInput.server.port
550
555
  }
@@ -48,22 +48,6 @@ export const mergeInputs = (
48
48
  if (base.advanced ?? overrides.advanced) {
49
49
  merged.advanced = spreadShallow(base.advanced, overrides.advanced)
50
50
 
51
- // Merge advanced.watch config
52
- if (base.advanced?.watch ?? overrides.advanced?.watch) {
53
- merged.advanced.watch = spreadShallow(
54
- base.advanced?.watch,
55
- overrides.advanced?.watch,
56
- )
57
-
58
- // Merge watch.also arrays
59
- if (base.advanced?.watch?.also ?? overrides.advanced?.watch?.also) {
60
- merged.advanced.watch.also = [
61
- ...(base.advanced?.watch?.also ?? []),
62
- ...(overrides.advanced?.watch?.also ?? []),
63
- ]
64
- }
65
- }
66
-
67
51
  // Merge Vite configs if present
68
52
  if (base.advanced?.vite ?? overrides.advanced?.vite) {
69
53
  merged.advanced.vite = Vite.mergeConfig(
@@ -1,4 +1,4 @@
1
- import { z } from 'zod/v4'
1
+ import { z } from 'zod'
2
2
 
3
3
  /**
4
4
  * Schema for validating front matter content
@@ -1,3 +1,4 @@
1
+ export * as Introspection from './introspection/introspection.js'
1
2
  export * as Memory from './memory/memory.js'
2
3
  export * as SchemaDirectory from './schema-directory/schema-directory.js'
3
4
  export * as SchemaFile from './schema-file/schema-file.js'
@@ -0,0 +1,213 @@
1
+ import { Grafaid } from '#lib/grafaid/index'
2
+ import { GraphqlChange } from '#lib/graphql-change/index'
3
+ import type { GraphqlChangeset } from '#lib/graphql-changeset/index'
4
+ import { GraphqlSchemaLoader } from '#lib/graphql-schema-loader/index'
5
+ import { Fs, Json, Path } from '@wollybeard/kit'
6
+ import type { NonEmptyChangeSets, SchemaReadResult } from '../../schema.js'
7
+
8
+ /**
9
+ * Configuration for loading schema via GraphQL introspection.
10
+ *
11
+ * Polen supports two introspection features:
12
+ * 1. **File Convention**: Automatically detects `schema.introspection.json` if present
13
+ * 2. **Automatic Introspection**: Fetches from your endpoint and creates the file
14
+ *
15
+ * When configured, Polen will:
16
+ * - Execute the standard GraphQL introspection query against your endpoint
17
+ * - Save the result to `schema.introspection.json` in your project root
18
+ * - Use this cached file for subsequent builds (no network requests)
19
+ *
20
+ * The saved file contains a standard GraphQL introspection query result as defined
21
+ * in the GraphQL specification, making it compatible with any tool that works with
22
+ * introspection data (GraphQL Codegen, Apollo CLI, etc.).
23
+ *
24
+ * To refresh the schema, delete `schema.introspection.json` and rebuild.
25
+ *
26
+ * **Technical details**:
27
+ * - Uses Graffle's introspection extension
28
+ * - Performs the full introspection query (all types, fields, descriptions, etc.)
29
+ * - No customization of the query is currently supported
30
+ *
31
+ * @see https://spec.graphql.org/draft/#sec-Introspection - GraphQL Introspection spec
32
+ * @see https://github.com/graphql/graphql-js/blob/main/src/utilities/getIntrospectionQuery.ts - Reference implementation
33
+ *
34
+ * @example
35
+ * ```ts
36
+ * // Basic introspection
37
+ * introspection: {
38
+ * url: 'https://api.example.com/graphql'
39
+ * }
40
+ *
41
+ * // With authentication
42
+ * introspection: {
43
+ * url: 'https://api.example.com/graphql',
44
+ * headers: {
45
+ * 'Authorization': `Bearer ${process.env.API_TOKEN}`
46
+ * }
47
+ * }
48
+ * ```
49
+ */
50
+ export interface ConfigInput {
51
+ /**
52
+ * The GraphQL endpoint URL to introspect.
53
+ *
54
+ * Must be a valid GraphQL endpoint that supports introspection queries.
55
+ *
56
+ * @example 'https://api.example.com/graphql'
57
+ */
58
+ url?: string
59
+ /**
60
+ * Optional headers to include in the introspection request.
61
+ *
62
+ * Use this for authentication, API keys, or any custom headers
63
+ * required by your GraphQL endpoint.
64
+ *
65
+ * @example
66
+ * ```ts
67
+ * headers: {
68
+ * 'Authorization': `Bearer ${process.env.API_TOKEN}`,
69
+ * 'X-API-Key': process.env.API_KEY
70
+ * }
71
+ * ```
72
+ */
73
+ headers?: Record<string, string>
74
+ projectRoot?: string
75
+ }
76
+
77
+ export interface Config {
78
+ url: string
79
+ headers?: Record<string, string>
80
+ projectRoot: string
81
+ }
82
+
83
+ export const normalizeConfig = (configInput: ConfigInput): Config => {
84
+ if (!configInput.url) {
85
+ throw new Error(`Introspection data source requires a URL`)
86
+ }
87
+
88
+ if (!configInput.projectRoot) {
89
+ throw new Error(`Introspection data source requires a projectRoot`)
90
+ }
91
+
92
+ const config: Config = {
93
+ url: configInput.url,
94
+ headers: configInput.headers,
95
+ projectRoot: configInput.projectRoot,
96
+ }
97
+
98
+ return config
99
+ }
100
+
101
+ const INTROSPECTION_FILE_NAME = `schema.introspection.json`
102
+
103
+ const getIntrospectionFilePath = (projectRoot: string) => {
104
+ return Path.join(projectRoot, INTROSPECTION_FILE_NAME)
105
+ }
106
+
107
+ export const readOrThrow = async (
108
+ configInput: ConfigInput,
109
+ ): Promise<SchemaReadResult> => {
110
+ const config = normalizeConfig(configInput)
111
+ const introspectionFilePath = getIntrospectionFilePath(config.projectRoot)
112
+
113
+ // Check if introspection file exists
114
+ const introspectionFileContent = await Fs.read(introspectionFilePath)
115
+ let schema: Grafaid.Schema.Schema
116
+
117
+ if (introspectionFileContent) {
118
+ // Load from existing file - no reCreate capability
119
+ try {
120
+ const introspectionData = Json.codec.decode(introspectionFileContent)
121
+
122
+ // Validate introspection data structure before passing to fromIntrospectionQuery
123
+ if (!introspectionData || typeof introspectionData !== 'object') {
124
+ throw new Error('Introspection data must be a valid JSON object')
125
+ }
126
+
127
+ // Allow fromIntrospectionQuery to handle validation of the introspection format
128
+ // It will provide more specific GraphQL-related error messages
129
+ if (!('data' in introspectionData)) {
130
+ throw new Error(
131
+ 'Introspection data missing required "data" property (expected GraphQL introspection result format)',
132
+ )
133
+ }
134
+
135
+ schema = Grafaid.Schema.fromIntrospectionQuery(introspectionData as any)
136
+ } catch (error) {
137
+ if (error instanceof SyntaxError) {
138
+ throw new Error(`Invalid JSON in ${introspectionFilePath}: ${error.message}`)
139
+ } else {
140
+ throw new Error(
141
+ `Invalid introspection data in ${introspectionFilePath}: ${
142
+ error instanceof Error ? error.message : String(error)
143
+ }. Delete this file to fetch fresh introspection data.`,
144
+ )
145
+ }
146
+ }
147
+
148
+ const schemaData = await createSingleSchemaChangeset(schema)
149
+ return {
150
+ data: schemaData,
151
+ source: { type: 'introspectionFile' },
152
+ }
153
+ } else {
154
+ // Fetch via introspection - can reCreate
155
+ const introspectionResult = await GraphqlSchemaLoader.load({
156
+ type: `introspect`,
157
+ url: config.url,
158
+ headers: config.headers,
159
+ })
160
+
161
+ schema = introspectionResult
162
+
163
+ // Get the raw introspection result for saving
164
+ const __schema = Grafaid.Schema.toIntrospectionQuery(schema)
165
+
166
+ // Wrap in GraphQL response format for consistency with API responses
167
+ const introspectionData = { data: { __schema } }
168
+
169
+ // Write to file
170
+ await Fs.write({
171
+ path: introspectionFilePath,
172
+ content: Json.codec.encode(introspectionData as any),
173
+ })
174
+
175
+ const schemaData = await createSingleSchemaChangeset(schema)
176
+ return {
177
+ data: schemaData,
178
+ source: {
179
+ type: 'introspectionAuto',
180
+ reCreate: async () => {
181
+ // Re-fetch using captured config - capture closure
182
+ const result = await readOrThrow(configInput)
183
+ return result.data
184
+ },
185
+ },
186
+ }
187
+ }
188
+ }
189
+
190
+ /**
191
+ * Create a single changeset from a schema object.
192
+ * This is the core logic for handling single (unversioned) schemas from introspection.
193
+ */
194
+ export const createSingleSchemaChangeset = async (schema: Grafaid.Schema.Schema): Promise<NonEmptyChangeSets> => {
195
+ const date = new Date() // Generate date here for unversioned schema
196
+ const after = schema
197
+ const before = Grafaid.Schema.empty
198
+ const changes = await GraphqlChange.calcChangeset({
199
+ before,
200
+ after,
201
+ })
202
+
203
+ const changeset: GraphqlChangeset.ChangeSet = {
204
+ date,
205
+ after,
206
+ before,
207
+ changes,
208
+ }
209
+
210
+ const result: NonEmptyChangeSets = [changeset]
211
+
212
+ return result
213
+ }
@@ -1,5 +1,5 @@
1
1
  import { Fs } from '@wollybeard/kit'
2
- import { z } from 'zod/v4'
2
+ import { z } from 'zod'
3
3
 
4
4
  export const SchemaMetadataSchema = z.object({
5
5
  /** Whether a schema is present in the project */
@@ -1,38 +1,104 @@
1
- import { Arr } from '@wollybeard/kit'
1
+ import { Arr, Fs, Path } from '@wollybeard/kit'
2
2
  import * as DataSources from './data-sources/data-sources.js'
3
3
  import type { NonEmptyChangeSets } from './schema.js'
4
4
 
5
- export type DataSourceType = `file` | `directory` | `memory` | `data`
5
+ export type DataSourceType =
6
+ | `file`
7
+ | `directory`
8
+ | `memory`
9
+ | `data`
10
+ | `introspection`
11
+ | `introspectionFile`
12
+ | `introspectionAuto`
13
+
14
+ /**
15
+ * Result of schema reading with provenance tracking for file watching and debugging.
16
+ */
17
+ export interface SchemaReadResult {
18
+ data: NonEmptyChangeSets | null
19
+ source: {
20
+ type: DataSourceType
21
+ /**
22
+ * Recreate the schema data and file after it has been deleted.
23
+ *
24
+ * This function re-fetches data from the original source and recreates
25
+ * the schema file on disk. Only called by file watchers after deletion.
26
+ *
27
+ * @returns Promise resolving to the recreated schema data, or null if recreation fails
28
+ */
29
+ reCreate?: () => Promise<NonEmptyChangeSets | null>
30
+ }
31
+ }
6
32
 
7
33
  /**
8
34
  * Schema configuration for Polen.
35
+ *
36
+ * Polen supports multiple ways to load your GraphQL schema, from simple files
37
+ * to dynamic introspection. Configure which sources to use and in what order.
38
+ *
39
+ * @example
40
+ * ```ts
41
+ * // Load from a file (default)
42
+ * schema: {} // Looks for schema.graphql
43
+ *
44
+ * // Load via introspection
45
+ * schema: {
46
+ * dataSources: {
47
+ * introspection: {
48
+ * url: 'https://api.example.com/graphql',
49
+ * headers: { 'Authorization': 'Bearer token' }
50
+ * }
51
+ * }
52
+ * }
53
+ *
54
+ * // Try multiple sources in order
55
+ * schema: {
56
+ * useDataSources: ['introspection', 'file'],
57
+ * dataSources: {
58
+ * introspection: { url: 'https://api.example.com/graphql' },
59
+ * file: { path: './fallback-schema.graphql' }
60
+ * }
61
+ * }
62
+ * ```
9
63
  */
10
64
  export interface Config {
11
65
  /**
12
66
  * Whether to enable schema loading.
13
67
  *
14
- * Set to `false` to disable schema features entirely.
68
+ * Set to `false` to disable schema features entirely. This removes
69
+ * the Reference and Changelog pages from your portal.
15
70
  *
16
- * @default `true`
71
+ * @default true
72
+ *
73
+ * @example
74
+ * ```ts
75
+ * // Disable schema features
76
+ * schema: { enabled: false }
77
+ * ```
17
78
  */
18
79
  enabled?: boolean
19
80
  /**
20
81
  * Which data sources to use for loading schemas.
21
82
  *
22
- * - `file` - Load from a single SDL file
23
- * - `directory` - Load multiple SDL files from a directory
83
+ * - `file` - Load from a single SDL file (default: `./schema.graphql`)
84
+ * - `directory` - Load multiple SDL files from a directory (default: `./schema/`)
24
85
  * - `memory` - Use schemas defined in configuration
25
86
  * - `data` - Use a pre-built schema object
87
+ * - `introspection` - Load schema via GraphQL introspection
26
88
  *
27
- * If not specified, Polen will try all sources in order until one succeeds.
89
+ * If not specified, Polen tries all sources in this order:
90
+ * 1. `data` 2. `directory` 3. `file` 4. `memory` 5. `introspection`
28
91
  *
29
92
  * @example
30
93
  * ```ts
31
94
  * // Use only file source
32
95
  * useDataSources: 'file'
33
96
  *
34
- * // Try multiple sources
35
- * useDataSources: ['directory', 'file']
97
+ * // Try multiple sources in custom order
98
+ * useDataSources: ['introspection', 'file']
99
+ *
100
+ * // Default behavior (try all sources)
101
+ * // useDataSources: undefined
36
102
  * ```
37
103
  */
38
104
  useDataSources?: Arr.Maybe<DataSourceType>
@@ -56,22 +122,38 @@ export interface Config {
56
122
  * Pre-built schema object to use directly.
57
123
  */
58
124
  data?: NonEmptyChangeSets
125
+ /**
126
+ * Configuration for loading schema via GraphQL introspection.
127
+ *
128
+ * Introspection fetches your schema directly from a running GraphQL endpoint.
129
+ * The schema is saved to `schema.introspection.json` in your project root.
130
+ * Delete this file to force a fresh introspection on the next build.
131
+ *
132
+ * @example
133
+ * ```ts
134
+ * introspection: {
135
+ * url: 'https://api.example.com/graphql',
136
+ * headers: { 'Authorization': 'Bearer token' }
137
+ * }
138
+ * ```
139
+ */
140
+ introspection?: DataSources.Introspection.ConfigInput
59
141
  }
60
142
  projectRoot: string
61
143
  }
62
144
 
63
145
  export const readOrThrow = async (
64
146
  config: Config,
65
- ): Promise<null | NonEmptyChangeSets> => {
147
+ ): Promise<SchemaReadResult> => {
66
148
  if (config.enabled === false) {
67
- return null
149
+ return { data: null, source: { type: 'data' } }
68
150
  }
69
151
 
70
152
  const useDataSources = config.useDataSources ? Arr.sure(config.useDataSources) : null
71
153
  const usingDataSource = (dataSource: DataSourceType) => useDataSources === null || useDataSources.includes(dataSource)
72
154
 
73
155
  if (usingDataSource(`data`) && config.dataSources?.data) {
74
- return config.dataSources.data
156
+ return { data: config.dataSources.data, source: { type: 'data' } }
75
157
  }
76
158
 
77
159
  if (usingDataSource(`directory`)) {
@@ -80,7 +162,7 @@ export const readOrThrow = async (
80
162
  ...config.dataSources?.directory,
81
163
  }
82
164
  const result = await DataSources.SchemaDirectory.readOrThrow(directoryConfig)
83
- if (result) return result
165
+ if (result) return { data: result, source: { type: 'directory' } }
84
166
  }
85
167
 
86
168
  if (usingDataSource(`file`)) {
@@ -89,7 +171,7 @@ export const readOrThrow = async (
89
171
  ...config.dataSources?.file,
90
172
  }
91
173
  const result = await DataSources.SchemaFile.readOrThrow(fileConfig)
92
- if (result) return result
174
+ if (result) return { data: result, source: { type: 'file' } }
93
175
  }
94
176
 
95
177
  if (usingDataSource(`memory`) && config.dataSources?.memory) {
@@ -98,8 +180,17 @@ export const readOrThrow = async (
98
180
  ...config.dataSources.memory,
99
181
  }
100
182
  const result = await DataSources.Memory.read(memoryConfig)
101
- if (result) return result
183
+ if (result) return { data: result, source: { type: 'memory' } }
184
+ }
185
+
186
+ if (usingDataSource(`introspection`) && config.dataSources?.introspection) {
187
+ const introspectionConfig = {
188
+ projectRoot: config.projectRoot,
189
+ ...config.dataSources.introspection,
190
+ }
191
+ const result = await DataSources.Introspection.readOrThrow(introspectionConfig)
192
+ if (result.data) return result
102
193
  }
103
194
 
104
- return null
195
+ return { data: null, source: { type: 'data' } }
105
196
  }
@@ -157,13 +157,13 @@ export const createSchemaSource = (config: SchemaSourceConfig) => {
157
157
  },
158
158
 
159
159
  writeAllAssets: async (
160
- schemaData: Awaited<ReturnType<typeof Schema.readOrThrow>>,
160
+ schemaData: Awaited<ReturnType<typeof Schema.readOrThrow>>['data'],
161
161
  metadata: Schema.SchemaMetadata,
162
162
  ) => {
163
163
  if (!schemaData) return
164
164
 
165
165
  // Write schema and changelog files
166
- for (const [index, version] of schemaData.entries()) {
166
+ for (const [index, version] of schemaData!.entries()) {
167
167
  const versionName = index === 0 ? Schema.VERSION_LATEST : Schema.dateToVersionString(version.date)
168
168
 
169
169
  // Write schema file
@@ -173,7 +173,7 @@ export const createSchemaSource = (config: SchemaSourceConfig) => {
173
173
  )
174
174
 
175
175
  // Write changelog file (except for the oldest/last version)
176
- if (Schema.shouldVersionHaveChangelog(index, schemaData.length)) {
176
+ if (Schema.shouldVersionHaveChangelog(index, schemaData!.length)) {
177
177
  const changelogData = {
178
178
  changes: version.changes,
179
179
  date: version.date.toISOString(),