nitro-graphql 2.0.0-beta.7 → 2.0.0-beta.72

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 (242) hide show
  1. package/README.md +31 -1196
  2. package/dist/cli/adapter.d.mts +13 -0
  3. package/dist/cli/adapter.mjs +67 -0
  4. package/dist/cli/commands/build.d.mts +21 -0
  5. package/dist/cli/commands/build.mjs +50 -0
  6. package/dist/cli/commands/dev.d.mts +40 -0
  7. package/dist/cli/commands/dev.mjs +74 -0
  8. package/dist/cli/commands/generate.d.mts +25 -0
  9. package/dist/cli/commands/generate.mjs +198 -0
  10. package/dist/cli/commands/index.d.mts +5 -0
  11. package/dist/cli/commands/index.mjs +5 -0
  12. package/dist/cli/commands/init.d.mts +45 -0
  13. package/dist/cli/commands/init.mjs +195 -0
  14. package/dist/cli/commands/validate.d.mts +9 -0
  15. package/dist/cli/commands/validate.mjs +65 -0
  16. package/dist/cli/completions.d.mts +6 -0
  17. package/dist/cli/completions.mjs +32 -0
  18. package/dist/cli/config.d.mts +18 -0
  19. package/dist/cli/config.mjs +19 -0
  20. package/dist/cli/index.d.mts +26 -0
  21. package/dist/cli/index.mjs +315 -0
  22. package/dist/cli/server/debug-handler.d.mts +9 -0
  23. package/dist/cli/server/debug-handler.mjs +68 -0
  24. package/dist/cli/server/dev-server.d.mts +29 -0
  25. package/dist/cli/server/dev-server.mjs +62 -0
  26. package/dist/cli/server/graphql-handler.d.mts +21 -0
  27. package/dist/cli/server/graphql-handler.mjs +43 -0
  28. package/dist/cli/server/health-handler.d.mts +12 -0
  29. package/dist/cli/server/health-handler.mjs +29 -0
  30. package/dist/cli/server/loader.d.mts +33 -0
  31. package/dist/cli/server/loader.mjs +144 -0
  32. package/dist/cli/server/sandbox-handler.d.mts +12 -0
  33. package/dist/cli/server/sandbox-handler.mjs +17 -0
  34. package/dist/cli/server/watcher.d.mts +20 -0
  35. package/dist/cli/server/watcher.mjs +64 -0
  36. package/dist/cli/server/ws-handler.d.mts +28 -0
  37. package/dist/cli/server/ws-handler.mjs +48 -0
  38. package/dist/config.d.mts +2 -0
  39. package/dist/config.mjs +2 -0
  40. package/dist/core/codegen/client.d.mts +42 -0
  41. package/dist/core/codegen/client.mjs +551 -0
  42. package/dist/core/codegen/document-loader.d.mts +9 -0
  43. package/dist/core/codegen/document-loader.mjs +16 -0
  44. package/dist/core/codegen/index.d.mts +8 -0
  45. package/dist/core/codegen/index.mjs +8 -0
  46. package/dist/core/codegen/plugin.d.mts +19 -0
  47. package/dist/core/codegen/plugin.mjs +29 -0
  48. package/dist/core/codegen/runtime.d.mts +17 -0
  49. package/dist/core/codegen/runtime.mjs +61 -0
  50. package/dist/core/codegen/schema-loader.d.mts +27 -0
  51. package/dist/core/codegen/schema-loader.mjs +126 -0
  52. package/dist/core/codegen/server.d.mts +27 -0
  53. package/dist/core/codegen/server.mjs +142 -0
  54. package/dist/core/codegen/validation.d.mts +17 -0
  55. package/dist/core/codegen/validation.mjs +33 -0
  56. package/dist/core/config.d.mts +45 -0
  57. package/dist/core/config.mjs +76 -0
  58. package/dist/core/constants.d.mts +188 -0
  59. package/dist/core/constants.mjs +208 -0
  60. package/dist/core/debug/index.d.mts +2 -0
  61. package/dist/core/debug/index.mjs +2 -0
  62. package/dist/core/debug/template.d.mts +52 -0
  63. package/dist/core/debug/template.mjs +384 -0
  64. package/dist/core/extend/index.d.mts +2 -0
  65. package/dist/core/extend/index.mjs +2 -0
  66. package/dist/core/extend/loader.d.mts +40 -0
  67. package/dist/core/extend/loader.mjs +196 -0
  68. package/dist/core/index.d.mts +33 -0
  69. package/dist/core/index.mjs +28 -0
  70. package/dist/core/manifest.d.mts +56 -0
  71. package/dist/core/manifest.mjs +100 -0
  72. package/dist/core/pubsub/index.d.mts +109 -0
  73. package/dist/core/pubsub/index.mjs +146 -0
  74. package/dist/core/scanning/ast-scanner.d.mts +27 -0
  75. package/dist/core/scanning/ast-scanner.mjs +114 -0
  76. package/dist/core/scanning/common.d.mts +25 -0
  77. package/dist/core/scanning/common.mjs +57 -0
  78. package/dist/core/scanning/directives.d.mts +13 -0
  79. package/dist/core/scanning/directives.mjs +28 -0
  80. package/dist/core/scanning/documents.d.mts +20 -0
  81. package/dist/core/scanning/documents.mjs +36 -0
  82. package/dist/core/scanning/index.d.mts +7 -0
  83. package/dist/core/scanning/index.mjs +7 -0
  84. package/dist/core/scanning/resolvers.d.mts +14 -0
  85. package/dist/core/scanning/resolvers.mjs +58 -0
  86. package/dist/core/scanning/schemas.d.mts +13 -0
  87. package/dist/core/scanning/schemas.mjs +50 -0
  88. package/dist/core/schema/builder.d.mts +59 -0
  89. package/dist/core/schema/builder.mjs +76 -0
  90. package/dist/core/schema/federation.d.mts +33 -0
  91. package/dist/core/schema/federation.mjs +38 -0
  92. package/dist/core/schema/index.d.mts +3 -0
  93. package/dist/core/schema/index.mjs +3 -0
  94. package/dist/core/server/index.d.mts +4 -0
  95. package/dist/core/server/index.mjs +3 -0
  96. package/dist/core/server/sandbox.d.mts +18 -0
  97. package/dist/core/server/sandbox.mjs +38 -0
  98. package/dist/core/server/types.d.mts +54 -0
  99. package/dist/core/server/types.mjs +1 -0
  100. package/dist/core/server/yoga.d.mts +42 -0
  101. package/dist/core/server/yoga.mjs +87 -0
  102. package/dist/core/types/adapter.d.mts +55 -0
  103. package/dist/core/types/adapter.mjs +1 -0
  104. package/dist/core/types/codegen.d.mts +136 -0
  105. package/dist/core/types/codegen.mjs +1 -0
  106. package/dist/core/types/config.d.mts +188 -0
  107. package/dist/core/types/config.mjs +1 -0
  108. package/dist/{utils/define.d.ts → core/types/define.d.mts} +2 -30
  109. package/dist/core/types/define.mjs +1 -0
  110. package/dist/core/types/index.d.mts +5 -0
  111. package/dist/core/types/index.mjs +1 -0
  112. package/dist/core/types/scanning.d.mts +64 -0
  113. package/dist/core/types/scanning.mjs +1 -0
  114. package/dist/{utils/directive-parser.d.ts → core/utils/directive-parser.d.mts} +18 -4
  115. package/dist/{utils/directive-parser.js → core/utils/directive-parser.mjs} +29 -36
  116. package/dist/core/utils/errors.d.mts +77 -0
  117. package/dist/core/utils/errors.mjs +91 -0
  118. package/dist/core/utils/file-io.d.mts +24 -0
  119. package/dist/core/utils/file-io.mjs +45 -0
  120. package/dist/core/utils/imports.d.mts +15 -0
  121. package/dist/core/utils/imports.mjs +24 -0
  122. package/dist/core/utils/index.d.mts +7 -0
  123. package/dist/core/utils/index.mjs +7 -0
  124. package/dist/core/utils/logger.d.mts +18 -0
  125. package/dist/core/utils/logger.mjs +36 -0
  126. package/dist/core/utils/ofetch-templates.d.mts +30 -0
  127. package/dist/core/utils/ofetch-templates.mjs +134 -0
  128. package/dist/core/utils/runtime.d.mts +74 -0
  129. package/dist/core/utils/runtime.mjs +109 -0
  130. package/dist/core/utils/subscribe-templates.d.mts +8 -0
  131. package/dist/core/utils/subscribe-templates.mjs +58 -0
  132. package/dist/core/validation/external-services.d.mts +11 -0
  133. package/dist/core/validation/external-services.mjs +33 -0
  134. package/dist/core/validation/index.d.mts +2 -0
  135. package/dist/core/validation/index.mjs +2 -0
  136. package/dist/core/watcher/index.d.mts +107 -0
  137. package/dist/core/watcher/index.mjs +140 -0
  138. package/dist/define.d.mts +294 -0
  139. package/dist/define.mjs +323 -0
  140. package/dist/index.d.mts +6 -0
  141. package/dist/index.mjs +5 -0
  142. package/dist/nitro/adapter.d.mts +28 -0
  143. package/dist/nitro/adapter.mjs +90 -0
  144. package/dist/{utils/apollo.d.ts → nitro/apollo.d.mts} +3 -3
  145. package/dist/nitro/apollo.mjs +57 -0
  146. package/dist/nitro/codegen.d.mts +18 -0
  147. package/dist/nitro/codegen.mjs +171 -0
  148. package/dist/nitro/config.d.mts +50 -0
  149. package/dist/nitro/config.mjs +55 -0
  150. package/dist/nitro/index.d.mts +45 -0
  151. package/dist/nitro/index.mjs +63 -0
  152. package/dist/{utils/path-resolver.d.ts → nitro/paths.d.mts} +8 -25
  153. package/dist/nitro/paths.mjs +97 -0
  154. package/dist/nitro/rollup.d.mts +6 -0
  155. package/dist/nitro/rollup.mjs +52 -0
  156. package/dist/nitro/routes/apollo-sandbox-script.d.mts +6 -0
  157. package/dist/nitro/routes/apollo-sandbox-script.mjs +12 -0
  158. package/dist/nitro/routes/apollo-server-ws.d.mts +15 -0
  159. package/dist/nitro/routes/apollo-server-ws.mjs +50 -0
  160. package/dist/nitro/routes/apollo-server.d.mts +6 -0
  161. package/dist/nitro/routes/apollo-server.mjs +69 -0
  162. package/dist/{routes/debug.d.ts → nitro/routes/debug.d.mts} +12 -19
  163. package/dist/nitro/routes/debug.mjs +98 -0
  164. package/dist/nitro/routes/graphql-yoga-ws.d.mts +12 -0
  165. package/dist/nitro/routes/graphql-yoga-ws.mjs +50 -0
  166. package/dist/nitro/routes/graphql-yoga.d.mts +12 -0
  167. package/dist/nitro/routes/graphql-yoga.mjs +39 -0
  168. package/dist/nitro/routes/health.d.mts +10 -0
  169. package/dist/{routes/health.js → nitro/routes/health.mjs} +5 -6
  170. package/dist/nitro/setup/extend-loader.d.mts +16 -0
  171. package/dist/nitro/setup/extend-loader.mjs +80 -0
  172. package/dist/nitro/setup/file-watcher.d.mts +18 -0
  173. package/dist/nitro/setup/file-watcher.mjs +62 -0
  174. package/dist/nitro/setup/logging.d.mts +16 -0
  175. package/dist/nitro/setup/logging.mjs +64 -0
  176. package/dist/nitro/setup/rollup-integration.d.mts +24 -0
  177. package/dist/nitro/setup/rollup-integration.mjs +64 -0
  178. package/dist/nitro/setup/routes.d.mts +13 -0
  179. package/dist/nitro/setup/routes.mjs +69 -0
  180. package/dist/nitro/setup/scanner.d.mts +59 -0
  181. package/dist/nitro/setup/scanner.mjs +118 -0
  182. package/dist/nitro/setup/ts-config.d.mts +10 -0
  183. package/dist/nitro/setup/ts-config.mjs +67 -0
  184. package/dist/nitro/setup.d.mts +11 -0
  185. package/dist/nitro/setup.mjs +189 -0
  186. package/dist/nitro/types.d.mts +549 -0
  187. package/dist/nitro/types.mjs +1 -0
  188. package/dist/nitro/virtual/generators.d.mts +38 -0
  189. package/dist/nitro/virtual/generators.mjs +190 -0
  190. package/dist/nitro/virtual/stubs.d.mts +21 -0
  191. package/dist/nitro/virtual/stubs.mjs +31 -0
  192. package/dist/{ecosystem/nuxt.d.ts → nuxt.d.mts} +1 -1
  193. package/dist/nuxt.mjs +111 -0
  194. package/dist/{graphql/server.d.ts → stubs/index.d.mts} +5 -1
  195. package/dist/stubs/index.mjs +1 -0
  196. package/dist/subscribe/index.d.mts +78 -0
  197. package/dist/subscribe/index.mjs +205 -0
  198. package/native/index.d.ts +20 -0
  199. package/native/index.js +585 -0
  200. package/package.json +202 -112
  201. package/dist/ecosystem/nuxt.js +0 -67
  202. package/dist/graphql/index.d.ts +0 -5
  203. package/dist/graphql/index.js +0 -1
  204. package/dist/graphql/server.js +0 -1
  205. package/dist/index.d.ts +0 -8
  206. package/dist/index.js +0 -318
  207. package/dist/rollup.js +0 -277
  208. package/dist/routes/apollo-server.d.ts +0 -6
  209. package/dist/routes/apollo-server.js +0 -89
  210. package/dist/routes/debug.js +0 -449
  211. package/dist/routes/graphql-yoga.d.ts +0 -6
  212. package/dist/routes/graphql-yoga.js +0 -91
  213. package/dist/routes/health.d.ts +0 -10
  214. package/dist/types/index.d.ts +0 -246
  215. package/dist/types/standard-schema.d.ts +0 -59
  216. package/dist/utils/apollo.js +0 -61
  217. package/dist/utils/client-codegen.d.ts +0 -38
  218. package/dist/utils/client-codegen.js +0 -290
  219. package/dist/utils/define.js +0 -57
  220. package/dist/utils/file-generator.d.ts +0 -37
  221. package/dist/utils/file-generator.js +0 -72
  222. package/dist/utils/index.d.ts +0 -39
  223. package/dist/utils/index.js +0 -278
  224. package/dist/utils/path-resolver.js +0 -127
  225. package/dist/utils/server-codegen.d.ts +0 -7
  226. package/dist/utils/server-codegen.js +0 -136
  227. package/dist/utils/type-generation.d.ts +0 -7
  228. package/dist/utils/type-generation.js +0 -360
  229. package/dist/virtual/debug-info.d.ts +0 -9
  230. package/dist/virtual/debug-info.js +0 -26
  231. package/dist/virtual/graphql-config.d.ts +0 -9
  232. package/dist/virtual/graphql-config.js +0 -10
  233. package/dist/virtual/module-config.d.ts +0 -9
  234. package/dist/virtual/module-config.js +0 -10
  235. package/dist/virtual/server-directives.d.ts +0 -11
  236. package/dist/virtual/server-directives.js +0 -10
  237. package/dist/virtual/server-resolvers.d.ts +0 -11
  238. package/dist/virtual/server-resolvers.js +0 -10
  239. package/dist/virtual/server-schemas.d.ts +0 -11
  240. package/dist/virtual/server-schemas.js +0 -10
  241. package/dist/vite.d.ts +0 -25
  242. package/dist/vite.js +0 -40
@@ -0,0 +1,90 @@
1
+ import { scanDirectivesCore } from "../core/scanning/directives.mjs";
2
+ import { scanDocumentsCore } from "../core/scanning/documents.mjs";
3
+ import { scanResolversCore } from "../core/scanning/resolvers.mjs";
4
+ import { scanGraphqlCore, scanSchemasCore } from "../core/scanning/schemas.mjs";
5
+ import { join, relative } from "pathe";
6
+ //#region src/nitro/adapter.ts
7
+ /**
8
+ * Create a CoreLogger from Nitro's logger
9
+ */
10
+ function createLoggerFromNitro(nitro) {
11
+ return {
12
+ info: (msg, ...args) => nitro.logger.info(msg, ...args),
13
+ warn: (msg, ...args) => nitro.logger.warn(msg, ...args),
14
+ error: (msg, ...args) => nitro.logger.error(msg, ...args),
15
+ success: (msg, ...args) => nitro.logger.success(msg, ...args),
16
+ debug: (msg, ...args) => nitro.logger.debug(msg, ...args)
17
+ };
18
+ }
19
+ /**
20
+ * Create a ScanContext from Nitro instance
21
+ */
22
+ function createScanContextFromNitro(nitro) {
23
+ return {
24
+ rootDir: nitro.options.rootDir,
25
+ serverDir: nitro.graphql.serverDir,
26
+ clientDir: nitro.graphql.clientDir,
27
+ ignorePatterns: nitro.options.ignore,
28
+ isDev: nitro.options.dev,
29
+ logger: createLoggerFromNitro(nitro)
30
+ };
31
+ }
32
+ /**
33
+ * Create a CoreConfig from Nitro instance
34
+ */
35
+ function createCoreConfigFromNitro(nitro) {
36
+ const graphqlOptions = nitro.options.graphql || {};
37
+ const isNuxt = nitro.options.framework?.name === "nuxt";
38
+ const typesDir = join(nitro.graphql.buildDir, "types");
39
+ return {
40
+ rootDir: nitro.options.rootDir,
41
+ buildDir: nitro.graphql.buildDir,
42
+ serverDir: nitro.graphql.serverDir,
43
+ clientDir: nitro.graphql.clientDir,
44
+ typesDir,
45
+ framework: graphqlOptions.framework || "graphql-yoga",
46
+ isNuxt,
47
+ isDev: nitro.options.dev,
48
+ graphqlOptions: {
49
+ framework: graphqlOptions.framework,
50
+ endpoint: typeof graphqlOptions.endpoint === "object" ? graphqlOptions.endpoint?.graphql : graphqlOptions.endpoint,
51
+ federation: graphqlOptions.federation,
52
+ security: graphqlOptions.security
53
+ },
54
+ logger: createLoggerFromNitro(nitro),
55
+ ignorePatterns: nitro.options.ignore
56
+ };
57
+ }
58
+ /**
59
+ * Create a CoreContext from Nitro instance
60
+ */
61
+ function createCoreContextFromNitro(nitro) {
62
+ return {
63
+ config: createCoreConfigFromNitro(nitro),
64
+ graphqlBuildDir: nitro.graphql.buildDir,
65
+ watchDirs: nitro.graphql.watchDirs,
66
+ dir: nitro.graphql.dir
67
+ };
68
+ }
69
+ /**
70
+ * Nitro framework adapter implementation
71
+ */
72
+ const NitroAdapter = {
73
+ name: "nitro",
74
+ createCoreConfig: createCoreConfigFromNitro,
75
+ createCoreContext: createCoreContextFromNitro,
76
+ createScanContext: createScanContextFromNitro,
77
+ getLogger: createLoggerFromNitro,
78
+ scanSchemas: (nitro) => scanSchemasCore(createScanContextFromNitro(nitro)),
79
+ scanGraphql: (nitro) => scanGraphqlCore(createScanContextFromNitro(nitro)),
80
+ scanResolvers: (nitro) => scanResolversCore(createScanContextFromNitro(nitro)),
81
+ scanDirectives: (nitro) => scanDirectivesCore(createScanContextFromNitro(nitro)),
82
+ scanDocuments(nitro) {
83
+ return scanDocumentsCore(createScanContextFromNitro(nitro), {
84
+ externalServices: nitro.options.graphql?.externalServices,
85
+ clientDirRelative: relative(nitro.options.rootDir, nitro.graphql.clientDir)
86
+ });
87
+ }
88
+ };
89
+ //#endregion
90
+ export { NitroAdapter, NitroAdapter as default, createCoreConfigFromNitro, createCoreContextFromNitro, createLoggerFromNitro, createScanContextFromNitro };
@@ -1,9 +1,9 @@
1
1
  import { ApolloServer, BaseContext, ContextFunction } from "@apollo/server";
2
- import { EventHandler, H3Event } from "h3";
3
- import { WithRequired } from "@apollo/utils.withrequired";
2
+ import { EventHandler, H3Event } from "nitro/h3";
4
3
  import { Hooks } from "crossws";
5
4
 
6
- //#region src/utils/apollo.d.ts
5
+ //#region src/nitro/apollo.d.ts
6
+ type WithRequired<T, K extends keyof T> = T & { [P in K]-?: T[P] };
7
7
  interface H3ContextFunctionArgument {
8
8
  event: H3Event;
9
9
  }
@@ -0,0 +1,57 @@
1
+ import { HeaderMap } from "@apollo/server";
2
+ import { eventHandler, getRequestURL, readBody } from "nitro/h3";
3
+ //#region src/nitro/apollo.ts
4
+ function startServerAndCreateH3Handler(server, options) {
5
+ const defaultContext = () => Promise.resolve({});
6
+ const contextFunction = options?.context ?? defaultContext;
7
+ return eventHandler(async (event) => {
8
+ const apolloServer = typeof server === "function" ? server() : server;
9
+ if (!options?.serverAlreadyStarted) apolloServer.startInBackgroundHandlingStartupErrorsByLoggingAndFailingAllRequests();
10
+ if (event.req.method === "OPTIONS") {
11
+ event.res.status = 204;
12
+ return null;
13
+ }
14
+ try {
15
+ const graphqlRequest = await toGraphqlRequest(event);
16
+ const { body, headers, status } = await apolloServer.executeHTTPGraphQLRequest({
17
+ httpGraphQLRequest: graphqlRequest,
18
+ context: () => contextFunction({ event })
19
+ });
20
+ if (body.kind === "chunked") throw new Error("Incremental delivery not implemented");
21
+ for (const [key, value] of headers) event.res.headers.set(key, value);
22
+ event.res.status = status || 200;
23
+ return body.string;
24
+ } catch (error) {
25
+ if (error instanceof SyntaxError) {
26
+ event.res.status = 400;
27
+ return error.message;
28
+ } else throw error;
29
+ }
30
+ });
31
+ }
32
+ async function toGraphqlRequest(event) {
33
+ const url = getRequestURL(event);
34
+ return {
35
+ method: event.req.method || "POST",
36
+ headers: normalizeHeaders(Object.fromEntries(event.req.headers.entries())),
37
+ search: url.search.slice(1),
38
+ body: await normalizeBody(event)
39
+ };
40
+ }
41
+ function normalizeHeaders(headers) {
42
+ const headerMap = new HeaderMap();
43
+ for (const [key, value] of Object.entries(headers)) if (value !== void 0) headerMap.set(key, value);
44
+ return headerMap;
45
+ }
46
+ async function normalizeBody(event) {
47
+ const PayloadMethods = [
48
+ "PATCH",
49
+ "POST",
50
+ "PUT",
51
+ "DELETE"
52
+ ];
53
+ const method = event.req.method;
54
+ if (method && PayloadMethods.includes(method)) return await readBody(event);
55
+ }
56
+ //#endregion
57
+ export { startServerAndCreateH3Handler };
@@ -0,0 +1,18 @@
1
+ import { Nitro } from "nitro/types";
2
+
3
+ //#region src/nitro/codegen.d.ts
4
+ /**
5
+ * Generate server-side resolver types
6
+ */
7
+ declare function generateServerTypes(nitro: Nitro, options?: {
8
+ silent?: boolean;
9
+ }): Promise<void>;
10
+ /**
11
+ * Generate client-side operation types
12
+ */
13
+ declare function generateClientTypes(nitro: Nitro, options?: {
14
+ silent?: boolean;
15
+ isInitial?: boolean;
16
+ }): Promise<void>;
17
+ //#endregion
18
+ export { generateClientTypes, generateServerTypes };
@@ -0,0 +1,171 @@
1
+ import { LOG_TAG } from "../core/constants.mjs";
2
+ import { loadGraphQLDocuments } from "../core/codegen/document-loader.mjs";
3
+ import { writeFile } from "../core/utils/file-io.mjs";
4
+ import { downloadAndSaveSchema, loadExternalSchema } from "../core/codegen/schema-loader.mjs";
5
+ import { generateClientTypesCore, generateExternalClientTypesCore, generateSubscriptionBuilder } from "../core/codegen/client.mjs";
6
+ import { generateServerTypesCore } from "../core/codegen/server.mjs";
7
+ import { validateNoDuplicateTypes } from "../core/codegen/validation.mjs";
8
+ import { loadFederationSupport } from "../core/schema/federation.mjs";
9
+ import { subscribeClientTemplate } from "../core/utils/subscribe-templates.mjs";
10
+ import { getDefaultPaths, getSdkConfig, getTypesConfig, resolveFilePath, shouldGenerateTypes } from "./paths.mjs";
11
+ import consola from "consola";
12
+ import { join, resolve } from "pathe";
13
+ import { printSchemaWithDirectives } from "@graphql-tools/utils";
14
+ import { parse, print } from "graphql";
15
+ import { existsSync, readFileSync } from "node:fs";
16
+ import { mergeTypeDefs } from "@graphql-tools/merge";
17
+ import { makeExecutableSchema } from "@graphql-tools/schema";
18
+ import { loadFilesSync } from "@graphql-tools/load-files";
19
+ //#region src/nitro/codegen.ts
20
+ const logger = consola.withTag(LOG_TAG);
21
+ async function buildSchemaFromString(source, federation) {
22
+ if (federation) {
23
+ const buildSubgraph = await loadFederationSupport();
24
+ if (!buildSubgraph) throw new Error("Federation enabled but @apollo/subgraph not installed");
25
+ return buildSubgraph([{ typeDefs: parse(source) }]);
26
+ }
27
+ return makeExecutableSchema({ typeDefs: source });
28
+ }
29
+ /**
30
+ * Generate server-side resolver types
31
+ */
32
+ async function generateServerTypes(nitro, options = {}) {
33
+ if (!shouldGenerateTypes(nitro)) return;
34
+ const schemas = nitro.scanSchemas || [];
35
+ if (!schemas.length) {
36
+ if (!options.silent) consola.info("No GraphQL schemas found");
37
+ return;
38
+ }
39
+ try {
40
+ const allStrings = loadFilesSync(schemas).map((s) => typeof s === "string" ? s : s.loc?.source?.body || "");
41
+ const validSchemas = [];
42
+ const strings = [];
43
+ schemas.forEach((schema, i) => {
44
+ if (allStrings[i]) {
45
+ validSchemas.push(schema);
46
+ strings.push(allStrings[i]);
47
+ }
48
+ });
49
+ const directiveSchemas = nitro.graphql.directiveSchemas;
50
+ if (directiveSchemas) {
51
+ validSchemas.push("<directives>");
52
+ strings.push(directiveSchemas);
53
+ }
54
+ if (!validateNoDuplicateTypes(validSchemas, strings)) return;
55
+ const mergedSchemaString = print(mergeTypeDefs([strings.join("\n\n")], {
56
+ throwOnConflict: true,
57
+ sort: true
58
+ }));
59
+ const federation = nitro.options.graphql?.federation?.enabled === true;
60
+ const sortedSchemaString = printSchemaWithDirectives(await buildSchemaFromString(mergedSchemaString, federation));
61
+ const result = await generateServerTypesCore({
62
+ framework: nitro.options.graphql?.framework || "graphql-yoga",
63
+ schemaString: sortedSchemaString,
64
+ config: nitro.options.graphql?.codegen?.server,
65
+ federationEnabled: federation
66
+ });
67
+ writeFile(resolve(nitro.graphql.buildDir, "schema.graphql"), sortedSchemaString);
68
+ const placeholders = getDefaultPaths(nitro);
69
+ const typesConfig = getTypesConfig(nitro);
70
+ const typesPath = resolveFilePath(typesConfig.server, typesConfig.enabled, true, "{typesDir}/nitro-graphql-server.d.ts", placeholders);
71
+ if (typesPath) {
72
+ writeFile(typesPath, result.types);
73
+ if (!options.silent) logger.success(`Server types: ${typesPath}`);
74
+ }
75
+ } catch (error) {
76
+ logger.error("Server type generation failed:", error);
77
+ }
78
+ }
79
+ /**
80
+ * Generate client-side operation types
81
+ */
82
+ async function generateClientTypes(nitro, options = {}) {
83
+ try {
84
+ if (nitro.scanSchemas?.length) await generateMainClientTypes(nitro, options);
85
+ if (nitro.options.graphql?.externalServices?.length) await generateExternalTypes(nitro, options);
86
+ } catch (error) {
87
+ logger.error("Client type generation failed:", error);
88
+ }
89
+ }
90
+ async function generateMainClientTypes(nitro, options = {}) {
91
+ const schemaPath = join(nitro.graphql.buildDir, "schema.graphql");
92
+ if (!existsSync(schemaPath)) {
93
+ if (!options.silent) consola.info("Schema not ready for client types");
94
+ return;
95
+ }
96
+ const docs = await loadGraphQLDocuments(nitro.scanDocuments);
97
+ const schemaString = readFileSync(schemaPath, "utf-8");
98
+ const serverScalars = nitro.options.graphql?.codegen?.server?.scalars;
99
+ const clientConfig = nitro.options.graphql?.codegen?.client || {};
100
+ const types = await generateClientTypesCore({
101
+ schemaString,
102
+ documents: docs,
103
+ config: {
104
+ ...clientConfig,
105
+ scalars: clientConfig.scalars ?? serverScalars
106
+ },
107
+ sdkConfig: nitro.options.graphql?.codegen?.clientSDK,
108
+ options
109
+ });
110
+ if (types === false) return;
111
+ const placeholders = getDefaultPaths(nitro);
112
+ const typesConfig = getTypesConfig(nitro);
113
+ const sdkConfig = getSdkConfig(nitro);
114
+ const subscriptionsEnabled = nitro.options.graphql?.subscriptions?.enabled ?? false;
115
+ const clientPath = resolveFilePath(typesConfig.client, typesConfig.enabled, true, "{typesDir}/nitro-graphql-client.d.ts", placeholders);
116
+ if (clientPath) {
117
+ writeFile(clientPath, types.types);
118
+ if (!options.silent) logger.success(`Client types: ${clientPath}`);
119
+ }
120
+ const subscriptionCode = generateSubscriptionBuilder(docs, subscriptionsEnabled);
121
+ const sdkPath = resolveFilePath(sdkConfig.main, sdkConfig.enabled, true, "{clientDir}/default/sdk.ts", placeholders);
122
+ if (sdkPath) {
123
+ writeFile(sdkPath, subscriptionCode ? types.sdk + subscriptionCode : types.sdk);
124
+ if (!options.silent) logger.success(`SDK: ${sdkPath}`);
125
+ }
126
+ if (subscriptionsEnabled) {
127
+ const subscribePath = resolveFilePath(true, true, true, "{clientDir}/default/subscribe.ts", placeholders);
128
+ if (subscribePath && !existsSync(subscribePath)) {
129
+ writeFile(subscribePath, subscribeClientTemplate);
130
+ if (!options.silent) logger.success(`Subscribe config: ${subscribePath}`);
131
+ }
132
+ }
133
+ }
134
+ async function generateExternalTypes(nitro, options = {}) {
135
+ for (const service of nitro.options.graphql?.externalServices || []) try {
136
+ if (!options.silent) consola.info(`[${service.name}] Processing external service`);
137
+ await downloadAndSaveSchema(service, nitro.options.buildDir);
138
+ const schema = await loadExternalSchema(service, nitro.options.buildDir);
139
+ if (!schema) {
140
+ consola.warn(`[${service.name}] Failed to load schema`);
141
+ continue;
142
+ }
143
+ const docs = service.documents?.length ? await loadGraphQLDocuments(service.documents).catch(() => []) : [];
144
+ if (service.documents?.length && !docs.length) {
145
+ consola.warn(`[${service.name}] No documents found`);
146
+ continue;
147
+ }
148
+ const types = await generateExternalClientTypesCore(service, schema, docs);
149
+ if (types === false) continue;
150
+ const placeholders = {
151
+ ...getDefaultPaths(nitro),
152
+ serviceName: service.name
153
+ };
154
+ const typesConfig = getTypesConfig(nitro);
155
+ const sdkConfig = getSdkConfig(nitro);
156
+ const typesPath = resolveFilePath(service.paths?.types ?? typesConfig.external, typesConfig.enabled, true, "{typesDir}/nitro-graphql-client-{serviceName}.d.ts", placeholders);
157
+ if (typesPath) {
158
+ writeFile(typesPath, types.types);
159
+ if (!options.silent) consola.success(`[${service.name}] Types: ${typesPath}`);
160
+ }
161
+ const sdkPath = resolveFilePath(service.paths?.sdk ?? sdkConfig.external, sdkConfig.enabled, true, "{clientDir}/{serviceName}/sdk.ts", placeholders);
162
+ if (sdkPath) {
163
+ writeFile(sdkPath, types.sdk);
164
+ if (!options.silent) consola.success(`[${service.name}] SDK: ${sdkPath}`);
165
+ }
166
+ } catch (error) {
167
+ consola.error(`[${service.name}] External service failed:`, error);
168
+ }
169
+ }
170
+ //#endregion
171
+ export { generateClientTypes, generateServerTypes };
@@ -0,0 +1,50 @@
1
+ import { NitroGraphQLOptions } from "./types.mjs";
2
+
3
+ //#region src/nitro/config.d.ts
4
+ /**
5
+ * Default type generation configuration
6
+ */
7
+ declare const DEFAULT_TYPES_CONFIG: {
8
+ readonly server: ".graphql/nitro-graphql-server.d.ts";
9
+ readonly client: ".graphql/nitro-graphql-client.d.ts";
10
+ readonly enabled: true;
11
+ };
12
+ /**
13
+ * Default runtime GraphQL configuration
14
+ */
15
+ declare const DEFAULT_RUNTIME_CONFIG: NitroGraphQLOptions;
16
+ /**
17
+ * Default ignore patterns for file watching
18
+ */
19
+ declare const DEFAULT_IGNORE_PATTERNS: string[];
20
+ /**
21
+ * Default SDK configuration
22
+ */
23
+ declare const DEFAULT_SDK_CONFIG: {
24
+ readonly enabled: true;
25
+ readonly main: true;
26
+ readonly external: true;
27
+ };
28
+ /**
29
+ * Default paths configuration
30
+ * These are used as placeholders and resolved based on framework
31
+ */
32
+ declare const DEFAULT_PATHS_CONFIG: {
33
+ readonly serverDir: "server/graphql";
34
+ readonly clientDir: null;
35
+ readonly typesDir: null;
36
+ };
37
+ /**
38
+ * Default TypeScript strict mode setting
39
+ */
40
+ declare const DEFAULT_TYPESCRIPT_STRICT: true;
41
+ /**
42
+ * Default watcher persistence setting
43
+ */
44
+ declare const DEFAULT_WATCHER_PERSISTENT: true;
45
+ /**
46
+ * Default watcher ignore initial setting
47
+ */
48
+ declare const DEFAULT_WATCHER_IGNORE_INITIAL: true;
49
+ //#endregion
50
+ export { DEFAULT_IGNORE_PATTERNS, DEFAULT_PATHS_CONFIG, DEFAULT_RUNTIME_CONFIG, DEFAULT_SDK_CONFIG, DEFAULT_TYPESCRIPT_STRICT, DEFAULT_TYPES_CONFIG, DEFAULT_WATCHER_IGNORE_INITIAL, DEFAULT_WATCHER_PERSISTENT };
@@ -0,0 +1,55 @@
1
+ import { DEFAULT_CLIENT_TYPES_PATH, DEFAULT_SERVER_TYPES_PATH, ENDPOINT_GRAPHQL, ENDPOINT_HEALTH } from "../core/constants.mjs";
2
+ //#region src/nitro/config.ts
3
+ /**
4
+ * Default type generation configuration
5
+ */
6
+ const DEFAULT_TYPES_CONFIG = {
7
+ server: DEFAULT_SERVER_TYPES_PATH,
8
+ client: DEFAULT_CLIENT_TYPES_PATH,
9
+ enabled: true
10
+ };
11
+ /**
12
+ * Default runtime GraphQL configuration
13
+ */
14
+ const DEFAULT_RUNTIME_CONFIG = {
15
+ endpoint: {
16
+ graphql: ENDPOINT_GRAPHQL,
17
+ healthCheck: ENDPOINT_HEALTH
18
+ },
19
+ playground: true
20
+ };
21
+ /**
22
+ * Default ignore patterns for file watching
23
+ */
24
+ const DEFAULT_IGNORE_PATTERNS = [];
25
+ /**
26
+ * Default SDK configuration
27
+ */
28
+ const DEFAULT_SDK_CONFIG = {
29
+ enabled: true,
30
+ main: true,
31
+ external: true
32
+ };
33
+ /**
34
+ * Default paths configuration
35
+ * These are used as placeholders and resolved based on framework
36
+ */
37
+ const DEFAULT_PATHS_CONFIG = {
38
+ serverDir: "server/graphql",
39
+ clientDir: null,
40
+ typesDir: null
41
+ };
42
+ /**
43
+ * Default TypeScript strict mode setting
44
+ */
45
+ const DEFAULT_TYPESCRIPT_STRICT = true;
46
+ /**
47
+ * Default watcher persistence setting
48
+ */
49
+ const DEFAULT_WATCHER_PERSISTENT = true;
50
+ /**
51
+ * Default watcher ignore initial setting
52
+ */
53
+ const DEFAULT_WATCHER_IGNORE_INITIAL = true;
54
+ //#endregion
55
+ export { DEFAULT_IGNORE_PATTERNS, DEFAULT_PATHS_CONFIG, DEFAULT_RUNTIME_CONFIG, DEFAULT_SDK_CONFIG, DEFAULT_TYPESCRIPT_STRICT, DEFAULT_TYPES_CONFIG, DEFAULT_WATCHER_IGNORE_INITIAL, DEFAULT_WATCHER_PERSISTENT };
@@ -0,0 +1,45 @@
1
+ import { ClientUtilsConfig, CodegenClientConfig, CodegenServerConfig, DefineDirectiveConfig, DefineServerConfig, DirectiveArgument, DirectiveDefinition, ExplicitPathsExtendSource, ExtendSource, ExternalGraphQLService, ExternalServicePaths, FederationConfig, FileGenerationConfig, Flatten, GenImport, GenericSdkConfig, GraphQLArgumentType, GraphQLBaseType, GraphQLScalarType, LocalDirExtendSource, NitroGraphQLOptions, PathsConfig, PubSubConfig, RuntimeConfig, SSETransportConfig, SdkConfig, SecurityConfig, StandardSchemaV1, SubscriptionsConfig, TypesConfig, WatchConfig, WebSocketTransportConfig } from "./types.mjs";
2
+ import { NitroAdapter } from "./adapter.mjs";
3
+ import { resolveSecurityConfig } from "./setup/logging.mjs";
4
+ import { setupNitroGraphQL } from "./setup.mjs";
5
+ import { NitroModule } from "nitro/types";
6
+ import { Plugin } from "vite";
7
+
8
+ //#region src/nitro/index.d.ts
9
+ /**
10
+ * Vite plugin to load GraphQL files as strings AND auto-register Nitro module
11
+ * This prevents Vite from trying to parse .graphql/.gql files as JavaScript
12
+ * and automatically sets up the nitro-graphql module via the nitro: hook
13
+ *
14
+ * @example - Vite usage
15
+ * ```ts
16
+ * import { defineConfig } from 'vite'
17
+ * import { nitro } from 'nitro/vite'
18
+ * import graphql from 'nitro-graphql'
19
+ *
20
+ * export default defineConfig({
21
+ * plugins: [
22
+ * graphql({ framework: 'graphql-yoga' }), // Auto-registers Nitro module
23
+ * nitro()
24
+ * ]
25
+ * })
26
+ * ```
27
+ *
28
+ *
29
+ * @example - Nitro direct usage
30
+ * ```ts
31
+ *
32
+ * import graphql from 'nitro-graphql'
33
+ *
34
+ * export default defineConfig({
35
+ * modules: [
36
+ * graphql({ framework: 'graphql-yoga' }) // Auto-registers Nitro module
37
+ * ]
38
+ * })
39
+ * ```
40
+ */
41
+ declare function graphqlModule(options?: NitroGraphQLOptions): Plugin & {
42
+ nitro: NitroModule;
43
+ };
44
+ //#endregion
45
+ export { ClientUtilsConfig, CodegenClientConfig, CodegenServerConfig, DefineDirectiveConfig, DefineServerConfig, DirectiveArgument, DirectiveDefinition, ExplicitPathsExtendSource, ExtendSource, ExternalGraphQLService, ExternalServicePaths, FederationConfig, FileGenerationConfig, Flatten, GenImport, GenericSdkConfig, GraphQLArgumentType, GraphQLBaseType, GraphQLScalarType, LocalDirExtendSource, NitroAdapter, NitroGraphQLOptions, PathsConfig, PubSubConfig, RuntimeConfig, SSETransportConfig, SdkConfig, SecurityConfig, StandardSchemaV1, SubscriptionsConfig, TypesConfig, WatchConfig, WebSocketTransportConfig, graphqlModule as default, resolveSecurityConfig, setupNitroGraphQL };
@@ -0,0 +1,63 @@
1
+ import { NitroAdapter } from "./adapter.mjs";
2
+ import { resolveSecurityConfig } from "./setup/logging.mjs";
3
+ import { setupNitroGraphQL } from "./setup.mjs";
4
+ import { readFile } from "node:fs/promises";
5
+ import defu from "defu";
6
+ //#region src/nitro/index.ts
7
+ const GRAPHQL_FILE_RE = /\.(?:graphql|gql)$/i;
8
+ /**
9
+ * Vite plugin to load GraphQL files as strings AND auto-register Nitro module
10
+ * This prevents Vite from trying to parse .graphql/.gql files as JavaScript
11
+ * and automatically sets up the nitro-graphql module via the nitro: hook
12
+ *
13
+ * @example - Vite usage
14
+ * ```ts
15
+ * import { defineConfig } from 'vite'
16
+ * import { nitro } from 'nitro/vite'
17
+ * import graphql from 'nitro-graphql'
18
+ *
19
+ * export default defineConfig({
20
+ * plugins: [
21
+ * graphql({ framework: 'graphql-yoga' }), // Auto-registers Nitro module
22
+ * nitro()
23
+ * ]
24
+ * })
25
+ * ```
26
+ *
27
+ *
28
+ * @example - Nitro direct usage
29
+ * ```ts
30
+ *
31
+ * import graphql from 'nitro-graphql'
32
+ *
33
+ * export default defineConfig({
34
+ * modules: [
35
+ * graphql({ framework: 'graphql-yoga' }) // Auto-registers Nitro module
36
+ * ]
37
+ * })
38
+ * ```
39
+ */
40
+ function graphqlModule(options) {
41
+ return {
42
+ name: "nitro-graphql",
43
+ enforce: "pre",
44
+ async load(id) {
45
+ if (!GRAPHQL_FILE_RE.test(id)) return null;
46
+ try {
47
+ const content = await readFile(id, "utf-8");
48
+ return `export default ${JSON.stringify(content)}`;
49
+ } catch (error) {
50
+ if (error && typeof error === "object" && "code" in error && error.code === "ENOENT") return null;
51
+ throw error;
52
+ }
53
+ },
54
+ nitro: { async setup(nitro) {
55
+ if (options) nitro.options.graphql = defu(nitro.options.graphql || {}, options);
56
+ nitro.options.graphql = nitro.options.graphql || {};
57
+ nitro.options.graphql._vitePlugin = true;
58
+ await setupNitroGraphQL(nitro);
59
+ } }
60
+ };
61
+ }
62
+ //#endregion
63
+ export { NitroAdapter, graphqlModule as default, resolveSecurityConfig, setupNitroGraphQL };
@@ -1,8 +1,7 @@
1
- import { ClientUtilsConfig, FileGenerationConfig, ScaffoldConfig, SdkConfig, TypesConfig } from "../types/index.js";
2
- import { Nitro } from "nitropack/types";
3
-
4
- //#region src/utils/path-resolver.d.ts
1
+ import { FileGenerationConfig, SdkConfig, TypesConfig } from "./types.mjs";
2
+ import { Nitro } from "nitro/types";
5
3
 
4
+ //#region src/nitro/paths.d.ts
6
5
  /**
7
6
  * Placeholder values for path resolution
8
7
  */
@@ -12,12 +11,12 @@ interface PathPlaceholders {
12
11
  rootDir: string;
13
12
  framework: 'nuxt' | 'nitro';
14
13
  typesDir: string;
15
- serverGraphql: string;
16
- clientGraphql: string;
14
+ serverDir: string;
15
+ clientDir: string;
17
16
  }
18
17
  /**
19
18
  * Replace placeholders in a path string
20
- * Supports: {serviceName}, {buildDir}, {rootDir}, {framework}, {typesDir}, {serverGraphql}, {clientGraphql}
19
+ * Supports: {serviceName}, {buildDir}, {rootDir}, {framework}, {typesDir}, {serverDir}, {clientDir}
21
20
  */
22
21
  declare function replacePlaceholders(path: string, placeholders: PathPlaceholders): string;
23
22
  /**
@@ -34,26 +33,10 @@ declare function shouldGenerateFile(config: FileGenerationConfig | undefined, ca
34
33
  * Returns: resolved absolute path or null if file should not be generated
35
34
  */
36
35
  declare function resolveFilePath(config: FileGenerationConfig | undefined, categoryEnabled: boolean | undefined, topLevelEnabled: boolean, defaultPath: string, placeholders: PathPlaceholders): string | null;
37
- /**
38
- * Check if scaffold files should be generated (category-level check)
39
- */
40
- declare function shouldGenerateScaffold(nitro: Nitro): boolean;
41
- /**
42
- * Get scaffold configuration (handles false case)
43
- */
44
- declare function getScaffoldConfig(nitro: Nitro): ScaffoldConfig;
45
- /**
46
- * Check if client utilities should be generated (category-level check)
47
- */
48
- declare function shouldGenerateClientUtils(nitro: Nitro): boolean;
49
- /**
50
- * Get client utilities configuration (handles false case)
51
- */
52
- declare function getClientUtilsConfig(nitro: Nitro): ClientUtilsConfig;
53
36
  /**
54
37
  * Check if SDK files should be generated (category-level check)
55
38
  */
56
- declare function shouldGenerateSDK(nitro: Nitro): boolean;
39
+ declare function shouldGenerateSdk(nitro: Nitro): boolean;
57
40
  /**
58
41
  * Get SDK configuration (handles false case)
59
42
  */
@@ -67,4 +50,4 @@ declare function shouldGenerateTypes(nitro: Nitro): boolean;
67
50
  */
68
51
  declare function getTypesConfig(nitro: Nitro): TypesConfig;
69
52
  //#endregion
70
- export { PathPlaceholders, getClientUtilsConfig, getDefaultPaths, getScaffoldConfig, getSdkConfig, getTypesConfig, replacePlaceholders, resolveFilePath, shouldGenerateClientUtils, shouldGenerateFile, shouldGenerateSDK, shouldGenerateScaffold, shouldGenerateTypes };
53
+ export { PathPlaceholders, getDefaultPaths, getSdkConfig, getTypesConfig, replacePlaceholders, resolveFilePath, shouldGenerateFile, shouldGenerateSdk, shouldGenerateTypes };