polen 0.11.0-next.1 → 0.11.0-next.11

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 (215) hide show
  1. package/build/api/api.d.ts +2 -1
  2. package/build/api/api.d.ts.map +1 -1
  3. package/build/api/api.js +5 -1
  4. package/build/api/api.js.map +1 -1
  5. package/build/api/config/configurator.d.ts +35 -23
  6. package/build/api/config/configurator.d.ts.map +1 -1
  7. package/build/api/config/configurator.js +0 -6
  8. package/build/api/config/configurator.js.map +1 -1
  9. package/build/api/config/merge.d.ts.map +1 -1
  10. package/build/api/config/merge.js +0 -11
  11. package/build/api/config/merge.js.map +1 -1
  12. package/build/api/content/metadata.d.ts +8 -2
  13. package/build/api/content/metadata.d.ts.map +1 -1
  14. package/build/api/content/metadata.js +1 -1
  15. package/build/api/content/metadata.js.map +1 -1
  16. package/build/api/iso/$$.d.ts +2 -0
  17. package/build/api/iso/$$.d.ts.map +1 -0
  18. package/build/api/iso/$$.js +2 -0
  19. package/build/api/iso/$$.js.map +1 -0
  20. package/build/api/iso/$.d.ts +2 -0
  21. package/build/api/iso/$.d.ts.map +1 -0
  22. package/build/api/iso/$.js +2 -0
  23. package/build/api/iso/$.js.map +1 -0
  24. package/build/api/iso/schema/$$.d.ts +4 -0
  25. package/build/api/iso/schema/$$.d.ts.map +1 -0
  26. package/build/api/iso/schema/$$.js +3 -0
  27. package/build/api/iso/schema/$$.js.map +1 -0
  28. package/build/api/iso/schema/$.d.ts +2 -0
  29. package/build/api/iso/schema/$.d.ts.map +1 -0
  30. package/build/api/iso/schema/$.js +2 -0
  31. package/build/api/iso/schema/$.js.map +1 -0
  32. package/build/api/iso/schema/constants.d.ts +25 -0
  33. package/build/api/iso/schema/constants.d.ts.map +1 -0
  34. package/build/api/iso/schema/constants.js +42 -0
  35. package/build/api/iso/schema/constants.js.map +1 -0
  36. package/build/api/iso/schema/routing.d.ts +22 -0
  37. package/build/api/iso/schema/routing.d.ts.map +1 -0
  38. package/build/api/iso/schema/routing.js +39 -0
  39. package/build/api/iso/schema/routing.js.map +1 -0
  40. package/build/api/iso.d.ts +2 -0
  41. package/build/api/iso.d.ts.map +1 -0
  42. package/build/api/iso.js +2 -0
  43. package/build/api/iso.js.map +1 -0
  44. package/build/api/schema/data-sources/data-sources.d.ts +1 -0
  45. package/build/api/schema/data-sources/data-sources.d.ts.map +1 -1
  46. package/build/api/schema/data-sources/data-sources.js +1 -0
  47. package/build/api/schema/data-sources/data-sources.js.map +1 -1
  48. package/build/api/schema/data-sources/introspection/introspection.d.ts +83 -0
  49. package/build/api/schema/data-sources/introspection/introspection.d.ts.map +1 -0
  50. package/build/api/schema/data-sources/introspection/introspection.js +110 -0
  51. package/build/api/schema/data-sources/introspection/introspection.js.map +1 -0
  52. package/build/api/schema/metadata.d.ts +11 -3
  53. package/build/api/schema/metadata.d.ts.map +1 -1
  54. package/build/api/schema/metadata.js +1 -1
  55. package/build/api/schema/metadata.js.map +1 -1
  56. package/build/api/schema/read.d.ts +83 -9
  57. package/build/api/schema/read.d.ts.map +1 -1
  58. package/build/api/schema/read.js +15 -6
  59. package/build/api/schema/read.js.map +1 -1
  60. package/build/api/schema/schema.d.ts +3 -25
  61. package/build/api/schema/schema.d.ts.map +1 -1
  62. package/build/api/schema/schema.js +5 -42
  63. package/build/api/schema/schema.js.map +1 -1
  64. package/build/api/schema-source/schema-source.d.ts +1 -1
  65. package/build/api/schema-source/schema-source.d.ts.map +1 -1
  66. package/build/api/schema-source/schema-source.js.map +1 -1
  67. package/build/api/static/manifest.d.ts +1 -2
  68. package/build/api/static/manifest.d.ts.map +1 -1
  69. package/build/api/static/manifest.js +1 -1
  70. package/build/api/static/manifest.js.map +1 -1
  71. package/build/api/vite/plugins/build.d.ts.map +1 -1
  72. package/build/api/vite/plugins/build.js +3 -0
  73. package/build/api/vite/plugins/build.js.map +1 -1
  74. package/build/api/vite/plugins/core.d.ts.map +1 -1
  75. package/build/api/vite/plugins/core.js +11 -14
  76. package/build/api/vite/plugins/core.js.map +1 -1
  77. package/build/api/vite/plugins/main.d.ts.map +1 -1
  78. package/build/api/vite/plugins/main.js +1 -8
  79. package/build/api/vite/plugins/main.js.map +1 -1
  80. package/build/api/vite/plugins/schema-assets.d.ts.map +1 -1
  81. package/build/api/vite/plugins/schema-assets.js +52 -11
  82. package/build/api/vite/plugins/schema-assets.js.map +1 -1
  83. package/build/cli/commands/open.js +1 -1
  84. package/build/cli/commands/open.js.map +1 -1
  85. package/build/lib/grafaid/schema/schema.d.ts +1 -1
  86. package/build/lib/grafaid/schema/schema.d.ts.map +1 -1
  87. package/build/lib/grafaid/schema/schema.js +1 -1
  88. package/build/lib/grafaid/schema/schema.js.map +1 -1
  89. package/build/lib/kit-temp.js +2 -2
  90. package/build/lib/kit-temp.js.map +1 -1
  91. package/build/lib/react-router-aid/react-router-aid.d.ts +5 -3
  92. package/build/lib/react-router-aid/react-router-aid.d.ts.map +1 -1
  93. package/build/lib/react-router-aid/react-router-aid.js +7 -4
  94. package/build/lib/react-router-aid/react-router-aid.js.map +1 -1
  95. package/build/template/components/GraphQLInteractive/lib/parser.d.ts.map +1 -1
  96. package/build/template/components/GraphQLInteractive/lib/parser.js +32 -10
  97. package/build/template/components/GraphQLInteractive/lib/parser.js.map +1 -1
  98. package/build/template/components/HamburgerMenu.d.ts +1 -0
  99. package/build/template/components/HamburgerMenu.d.ts.map +1 -1
  100. package/build/template/components/HamburgerMenu.js +2 -2
  101. package/build/template/components/HamburgerMenu.js.map +1 -1
  102. package/build/template/components/ReferenceLink.d.ts.map +1 -1
  103. package/build/template/components/ReferenceLink.js +2 -4
  104. package/build/template/components/ReferenceLink.js.map +1 -1
  105. package/build/template/components/VersionSelector.d.ts +3 -2
  106. package/build/template/components/VersionSelector.d.ts.map +1 -1
  107. package/build/template/components/VersionSelector.js +7 -15
  108. package/build/template/components/VersionSelector.js.map +1 -1
  109. package/build/template/components/sidebar/Sidebar.d.ts +3 -2
  110. package/build/template/components/sidebar/Sidebar.d.ts.map +1 -1
  111. package/build/template/components/sidebar/Sidebar.js +7 -6
  112. package/build/template/components/sidebar/Sidebar.js.map +1 -1
  113. package/build/template/components/sidebar/SidebarContext.d.ts +6 -0
  114. package/build/template/components/sidebar/SidebarContext.d.ts.map +1 -0
  115. package/build/template/components/sidebar/SidebarContext.js +3 -0
  116. package/build/template/components/sidebar/SidebarContext.js.map +1 -0
  117. package/build/template/components/sidebar/SidebarItem.d.ts.map +1 -1
  118. package/build/template/components/sidebar/SidebarItem.js +11 -4
  119. package/build/template/components/sidebar/SidebarItem.js.map +1 -1
  120. package/build/template/hooks/useVersionPath.d.ts.map +1 -1
  121. package/build/template/hooks/useVersionPath.js +3 -1
  122. package/build/template/hooks/useVersionPath.js.map +1 -1
  123. package/build/template/layouts/SidebarLayout.d.ts +3 -2
  124. package/build/template/layouts/SidebarLayout.d.ts.map +1 -1
  125. package/build/template/layouts/SidebarLayout.js +2 -2
  126. package/build/template/layouts/SidebarLayout.js.map +1 -1
  127. package/build/template/routes/changelog.js +2 -2
  128. package/build/template/routes/changelog.js.map +1 -1
  129. package/build/template/routes/index.js +2 -2
  130. package/build/template/routes/index.js.map +1 -1
  131. package/build/template/routes/pages.js +2 -2
  132. package/build/template/routes/pages.js.map +1 -1
  133. package/build/template/routes/reference.d.ts +27 -4
  134. package/build/template/routes/reference.d.ts.map +1 -1
  135. package/build/template/routes/reference.js +82 -24
  136. package/build/template/routes/reference.js.map +1 -1
  137. package/build/template/routes/root.js +3 -3
  138. package/build/template/routes/root.js.map +1 -1
  139. package/build/template/server/ssg/get-route-paths.d.ts.map +1 -1
  140. package/build/template/server/ssg/get-route-paths.js +81 -9
  141. package/build/template/server/ssg/get-route-paths.js.map +1 -1
  142. package/build/template/sources/schema-source.d.ts +1 -1
  143. package/build/template/sources/schema-source.d.ts.map +1 -1
  144. package/build/template/sources/schema-source.js +2 -1
  145. package/build/template/sources/schema-source.js.map +1 -1
  146. package/package.json +19 -17
  147. package/src/api/api.ts +7 -1
  148. package/src/api/config/configurator.ts +35 -30
  149. package/src/api/config/merge.ts +0 -16
  150. package/src/api/content/metadata.ts +1 -1
  151. package/src/api/iso/$$.ts +1 -0
  152. package/src/api/iso/$.ts +1 -0
  153. package/src/api/iso/schema/$$.ts +5 -0
  154. package/src/api/iso/schema/$.ts +1 -0
  155. package/src/api/iso/schema/constants.ts +49 -0
  156. package/src/api/iso/schema/routing.ts +53 -0
  157. package/src/api/iso.ts +1 -0
  158. package/src/api/schema/data-sources/data-sources.ts +1 -0
  159. package/src/api/schema/data-sources/introspection/introspection.ts +213 -0
  160. package/src/api/schema/metadata.ts +1 -1
  161. package/src/api/schema/read.ts +107 -16
  162. package/src/api/schema/schema.ts +6 -53
  163. package/src/api/schema-source/schema-source.ts +3 -3
  164. package/src/api/static/manifest.ts +1 -1
  165. package/src/api/vite/plugins/build.ts +3 -0
  166. package/src/api/vite/plugins/core.ts +11 -14
  167. package/src/api/vite/plugins/main.ts +1 -9
  168. package/src/api/vite/plugins/schema-assets.ts +59 -12
  169. package/src/cli/commands/open.ts +1 -1
  170. package/src/lib/grafaid/schema/schema.ts +1 -0
  171. package/src/lib/kit-temp.ts +2 -2
  172. package/src/lib/mask/$.test.ts +3 -3
  173. package/src/lib/react-router-aid/react-router-aid.ts +12 -6
  174. package/src/template/components/GraphQLInteractive/lib/parser.ts +32 -16
  175. package/src/template/components/HamburgerMenu.tsx +3 -1
  176. package/src/template/components/ReferenceLink.tsx +7 -4
  177. package/src/template/components/VersionSelector.tsx +10 -18
  178. package/src/template/components/sidebar/Sidebar.tsx +20 -16
  179. package/src/template/components/sidebar/SidebarContext.tsx +7 -0
  180. package/src/template/components/sidebar/SidebarItem.tsx +11 -4
  181. package/src/template/hooks/useVersionPath.ts +3 -1
  182. package/src/template/layouts/SidebarLayout.tsx +5 -3
  183. package/src/template/routes/changelog.tsx +2 -2
  184. package/src/template/routes/index.tsx +2 -2
  185. package/src/template/routes/pages.tsx +2 -2
  186. package/src/template/routes/reference.tsx +90 -25
  187. package/src/template/routes/root.tsx +3 -3
  188. package/src/template/server/ssg/get-route-paths.test.ts +145 -0
  189. package/src/template/server/ssg/get-route-paths.ts +80 -9
  190. package/src/template/sources/schema-source.ts +2 -1
  191. package/build/template/lib/schema-utils/constants.d.ts +0 -5
  192. package/build/template/lib/schema-utils/constants.d.ts.map +0 -1
  193. package/build/template/lib/schema-utils/constants.js +0 -5
  194. package/build/template/lib/schema-utils/constants.js.map +0 -1
  195. package/build/template/routes/reference.$type.$field.d.ts +0 -5
  196. package/build/template/routes/reference.$type.$field.d.ts.map +0 -1
  197. package/build/template/routes/reference.$type.$field.js +0 -31
  198. package/build/template/routes/reference.$type.$field.js.map +0 -1
  199. package/build/template/routes/reference.$type.d.ts +0 -9
  200. package/build/template/routes/reference.$type.d.ts.map +0 -1
  201. package/build/template/routes/reference.$type.js +0 -25
  202. package/build/template/routes/reference.$type.js.map +0 -1
  203. package/build/template/routes/reference.version.$version.$type.$field.d.ts +0 -6
  204. package/build/template/routes/reference.version.$version.$type.$field.d.ts.map +0 -1
  205. package/build/template/routes/reference.version.$version.$type.$field.js +0 -32
  206. package/build/template/routes/reference.version.$version.$type.$field.js.map +0 -1
  207. package/build/template/routes/reference.version.$version.$type.d.ts +0 -11
  208. package/build/template/routes/reference.version.$version.$type.d.ts.map +0 -1
  209. package/build/template/routes/reference.version.$version.$type.js +0 -26
  210. package/build/template/routes/reference.version.$version.$type.js.map +0 -1
  211. package/src/template/lib/schema-utils/constants.ts +0 -4
  212. package/src/template/routes/reference.$type.$field.tsx +0 -34
  213. package/src/template/routes/reference.$type.tsx +0 -29
  214. package/src/template/routes/reference.version.$version.$type.$field.tsx +0 -35
  215. package/src/template/routes/reference.version.$version.$type.tsx +0 -30
@@ -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
@@ -0,0 +1 @@
1
+ export * from './schema/$.js'
@@ -0,0 +1 @@
1
+ export * as Api from './$$.js'
@@ -0,0 +1,5 @@
1
+ export * from './constants.js'
2
+ export * as Routing from './routing.js'
3
+
4
+ // Re-export types from server layer that are needed in template
5
+ export type { ChangelogData, ChangeSets, NonEmptyChangeSets } from '../../schema/schema.js'
@@ -0,0 +1 @@
1
+ export * as Schema from './$$.js'
@@ -0,0 +1,49 @@
1
+ /**
2
+ * Constants for schema versioning
3
+ */
4
+
5
+ /**
6
+ * The version identifier for the latest schema
7
+ */
8
+ export const VERSION_LATEST = `latest`
9
+
10
+ /**
11
+ * Fallback version name when date parsing fails
12
+ */
13
+ export const VERSION_UNKNOWN_FALLBACK = `unknown`
14
+
15
+ /**
16
+ * Convert a date to a version string in YYYY-MM-DD format
17
+ */
18
+ export const dateToVersionString = (date: Date): string => {
19
+ return date.toLocaleDateString('en-CA')
20
+ }
21
+
22
+ /**
23
+ * Convert a version string back to a Date
24
+ */
25
+ export const versionStringToDate = (version: string): Date => {
26
+ if (version === VERSION_LATEST) {
27
+ return new Date()
28
+ }
29
+
30
+ // Use modern date parsing - the version string should be in YYYY-MM-DD format
31
+ // which is ISO 8601 compatible
32
+ const parsedDate = new Date(version + 'T00:00:00Z')
33
+
34
+ // Check if the date is valid
35
+ if (!isNaN(parsedDate.getTime())) {
36
+ return parsedDate
37
+ }
38
+
39
+ // Throw error for invalid dates instead of silently defaulting
40
+ throw new Error(`Invalid version string: ${version}`)
41
+ }
42
+
43
+ /**
44
+ * Determine if a version at the given index should have a changelog file.
45
+ * Only non-oldest versions get changelog files.
46
+ */
47
+ export const shouldVersionHaveChangelog = (versionIndex: number, totalVersions: number): boolean => {
48
+ return versionIndex < totalVersions - 1
49
+ }
@@ -0,0 +1,53 @@
1
+ import { VERSION_LATEST } from './constants.js'
2
+
3
+ export interface ReferencePathParts {
4
+ version?: string
5
+ type?: string
6
+ field?: string
7
+ }
8
+
9
+ export const createReferencePath = (parts: ReferencePathParts): string => {
10
+ const basePath = createReferenceBasePath(parts.version)
11
+ return joinSegmentsAndPaths(basePath, parts.type, parts.field)
12
+ }
13
+
14
+ /**
15
+ * Create a base path for reference pages based on the current version
16
+ * Used for sidebar navigation and other UI components that need version-aware paths
17
+ */
18
+ export const createReferenceBasePath = (version?: string): string => {
19
+ return joinSegmentsAndPaths(segmentLiterals.reference, createReferenceVersionPath(version))
20
+ }
21
+
22
+ export const segmentLiterals = {
23
+ reference: 'reference',
24
+ version: 'version',
25
+ }
26
+
27
+ /**
28
+ * Create a base path for reference pages based on the current version
29
+ * Used for sidebar navigation and other UI components that need version-aware paths
30
+ */
31
+ export const createReferenceVersionPath = (version?: string): string => {
32
+ if (version === undefined) return ''
33
+ return version === VERSION_LATEST
34
+ ? ``
35
+ : `/${segmentLiterals.version}/${version}`
36
+ }
37
+
38
+ export const joinSegmentsAndPaths = (
39
+ ...segmentsOrPaths: (string | undefined | null | (string | null | undefined)[])[]
40
+ ): string => {
41
+ const path = '/' + segmentsOrPaths
42
+ .flat()
43
+ .filter((_): _ is string => _ !== undefined && _ !== null)
44
+ .map(chunkUnformatted =>
45
+ chunkUnformatted
46
+ .replace(/^\//, '')
47
+ .replace(/\/$/, '')
48
+ )
49
+ .filter(Boolean)
50
+ .join('/')
51
+
52
+ return path
53
+ }
package/src/api/iso.ts ADDED
@@ -0,0 +1 @@
1
+ export * from './iso/$.js'
@@ -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
  }