nitro-graphql 0.3.2 → 0.4.0

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.
@@ -1,7 +1,7 @@
1
- import * as _nuxt_schema2 from "@nuxt/schema";
1
+ import * as _nuxt_schema7 from "@nuxt/schema";
2
2
 
3
3
  //#region src/ecosystem/nuxt.d.ts
4
4
  interface ModuleOptions {}
5
- declare const _default: _nuxt_schema2.NuxtModule<ModuleOptions, ModuleOptions, false>;
5
+ declare const _default: _nuxt_schema7.NuxtModule<ModuleOptions, ModuleOptions, false>;
6
6
  //#endregion
7
7
  export { ModuleOptions, _default as default };
package/dist/index.d.ts CHANGED
@@ -1,9 +1,9 @@
1
1
  import { StandardSchemaV1 } from "./types/standard-schema.js";
2
2
  import { CodegenClientConfig, CodegenServerConfig, GenImport, NitroGraphQLOptions } from "./types/index.js";
3
- import * as nitropack5 from "nitropack";
3
+ import * as nitropack0 from "nitropack";
4
4
 
5
5
  //#region src/index.d.ts
6
6
  type GraphQLFramework = 'graphql-yoga';
7
- declare const _default: nitropack5.NitroModule;
7
+ declare const _default: nitropack0.NitroModule;
8
8
  //#endregion
9
9
  export { CodegenClientConfig, CodegenServerConfig, GenImport, GraphQLFramework, NitroGraphQLOptions, StandardSchemaV1, _default as default };
package/dist/index.js CHANGED
@@ -1,4 +1,4 @@
1
- import { relativeWithDot, scanDefs, scanResolvers } from "./utils/index.js";
1
+ import { relativeWithDot, scanDocs, scanResolvers, scanSchemas } from "./utils/index.js";
2
2
  import { clientTypeGeneration, serverTypeGeneration } from "./utils/server-type-generation.js";
3
3
  import { rollupConfig } from "./rollup.js";
4
4
  import { existsSync, mkdirSync, writeFileSync } from "node:fs";
@@ -7,7 +7,7 @@ import { watch } from "chokidar";
7
7
  import consola from "consola";
8
8
  import defu from "defu";
9
9
  import { defineNitroModule } from "nitropack/kit";
10
- import { dirname, join, resolve } from "pathe";
10
+ import { dirname, join, relative, resolve } from "pathe";
11
11
 
12
12
  //#region src/index.ts
13
13
  var src_default = defineNitroModule({
@@ -18,7 +18,12 @@ var src_default = defineNitroModule({
18
18
  buildDir: "",
19
19
  watchDirs: [],
20
20
  clientDir: "",
21
- serverDir: resolve(nitro.options.srcDir, "graphql")
21
+ serverDir: resolve(nitro.options.srcDir, "graphql"),
22
+ dir: {
23
+ build: relative(nitro.options.rootDir, nitro.options.buildDir),
24
+ client: "graphql",
25
+ server: "server"
26
+ }
22
27
  };
23
28
  nitro.hooks.hook("rollup:before", (nitro$1, rollupConfig$1) => {
24
29
  rollupConfig$1.external = rollupConfig$1.external || [];
@@ -46,6 +51,11 @@ var src_default = defineNitroModule({
46
51
  case "nuxt":
47
52
  watchDirs.push(join(nitro.options.rootDir, "app", "graphql"));
48
53
  nitro.graphql.clientDir = resolve(nitro.options.rootDir, "app", "graphql");
54
+ nitro.graphql.dir.client = "app/graphql";
55
+ break;
56
+ case "nitro":
57
+ nitro.graphql.clientDir = resolve(nitro.options.rootDir, "graphql");
58
+ nitro.graphql.dir.client = "graphql";
49
59
  break;
50
60
  default:
51
61
  }
@@ -54,7 +64,7 @@ var src_default = defineNitroModule({
54
64
  ignoreInitial: true,
55
65
  ignored: nitro.options.ignore
56
66
  }).on("all", async (event, path) => {
57
- if (path.endsWith(".graphql") || path.endsWith(".gql")) await clientTypeGeneration(nitro, path);
67
+ if (path.endsWith(".graphql") || path.endsWith(".gql")) await clientTypeGeneration(nitro);
58
68
  });
59
69
  nitro.hooks.hook("close", () => {
60
70
  watcher.close();
@@ -62,22 +72,26 @@ var src_default = defineNitroModule({
62
72
  const tsConfigPath = resolve(nitro.options.buildDir, nitro.options.typescript.tsconfigPath);
63
73
  const tsconfigDir = dirname(tsConfigPath);
64
74
  const typesDir = resolve(nitro.options.buildDir, "types");
65
- const defs = await scanDefs(nitro);
66
- nitro.scanDefs = defs;
75
+ const schemas = await scanSchemas(nitro);
76
+ nitro.scanSchemas = schemas;
77
+ const docs = await scanDocs(nitro);
78
+ nitro.scanDocuments = docs;
67
79
  const resolvers = await scanResolvers(nitro);
68
80
  nitro.scanResolvers = resolvers;
69
81
  nitro.hooks.hook("dev:start", async () => {
70
- const defs$1 = await scanDefs(nitro);
71
- nitro.scanDefs = defs$1;
82
+ const schemas$1 = await scanSchemas(nitro);
83
+ nitro.scanSchemas = schemas$1;
72
84
  const resolvers$1 = await scanResolvers(nitro);
73
85
  nitro.scanResolvers = resolvers$1;
86
+ const docs$1 = await scanDocs(nitro);
87
+ nitro.scanDocuments = docs$1;
74
88
  });
75
89
  await rollupConfig(nitro);
76
90
  await serverTypeGeneration(nitro);
77
- await clientTypeGeneration(nitro, nitro.graphql.clientDir);
91
+ await clientTypeGeneration(nitro);
78
92
  nitro.hooks.hook("close", async () => {
79
93
  await serverTypeGeneration(nitro);
80
- await clientTypeGeneration(nitro, nitro.graphql.clientDir);
94
+ await clientTypeGeneration(nitro);
81
95
  });
82
96
  const runtime = fileURLToPath(new URL("routes", import.meta.url));
83
97
  const methods = [
package/dist/rollup.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import { getImportId, scanGraphql } from "./utils/index.js";
2
- import { serverTypeGeneration } from "./utils/server-type-generation.js";
2
+ import { clientTypeGeneration, serverTypeGeneration } from "./utils/server-type-generation.js";
3
3
  import { resolve } from "pathe";
4
4
  import { readFile } from "node:fs/promises";
5
5
  import { parse } from "graphql";
@@ -7,7 +7,7 @@ import { genImport } from "knitwork";
7
7
 
8
8
  //#region src/rollup.ts
9
9
  async function rollupConfig(app) {
10
- virtualDefs(app);
10
+ virtualSchemas(app);
11
11
  virtualResolvers(app);
12
12
  getGraphQLConfig(app);
13
13
  app.hooks.hook("rollup:before", (nitro, rollupConfig$1) => {
@@ -41,20 +41,21 @@ async function rollupConfig(app) {
41
41
  });
42
42
  app.hooks.hook("dev:reload", async () => {
43
43
  await serverTypeGeneration(app);
44
+ await clientTypeGeneration(app);
44
45
  });
45
46
  }
46
- function virtualDefs(app) {
47
- const getDefs = () => {
48
- const defs = [...app.scanDefs, ...app.options.graphql?.typedefs ?? []];
49
- return defs;
47
+ function virtualSchemas(app) {
48
+ const getSchemas = () => {
49
+ const schemas = [...app.scanSchemas, ...app.options.graphql?.typedefs ?? []];
50
+ return schemas;
50
51
  };
51
52
  app.options.virtual ??= {};
52
- app.options.virtual["#nitro-internal-virtual/server-defs"] = () => {
53
- const imports = getDefs();
53
+ app.options.virtual["#nitro-internal-virtual/server-schemas"] = () => {
54
+ const imports = getSchemas();
54
55
  const code = `
55
56
  ${imports.map((handler) => `import ${getImportId(handler)} from '${handler}';`).join("\n")}
56
57
 
57
- export const defs = [
58
+ export const schemas = [
58
59
  ${imports.map((h) => `{ def: ${getImportId(h)} }`).join(",\n")}
59
60
  ];
60
61
  `;
@@ -1,6 +1,6 @@
1
- import * as h30 from "h3";
1
+ import * as h35 from "h3";
2
2
 
3
3
  //#region src/routes/apollo-server.d.ts
4
- declare const _default: h30.EventHandler<h30.EventHandlerRequest, any>;
4
+ declare const _default: h35.EventHandler<h35.EventHandlerRequest, any>;
5
5
  //#endregion
6
6
  export { _default as default };
@@ -2,16 +2,16 @@ import { startServerAndCreateH3Handler } from "../utils/apollo.js";
2
2
  import defu from "defu";
3
3
  import { mergeResolvers, mergeTypeDefs } from "@graphql-tools/merge";
4
4
  import { importedConfig } from "#nitro-internal-virtual/graphql-config";
5
- import { defs } from "#nitro-internal-virtual/server-defs";
6
5
  import { resolvers } from "#nitro-internal-virtual/server-resolvers";
6
+ import { schemas } from "#nitro-internal-virtual/server-schemas";
7
7
  import { ApolloServer } from "@apollo/server";
8
8
  import { ApolloServerPluginLandingPageLocalDefault } from "@apollo/server/plugin/landingPage/default";
9
9
 
10
10
  //#region src/routes/apollo-server.ts
11
11
  function createMergedSchema() {
12
12
  try {
13
- const mergedDefs = defs.map((schema) => schema.def).join("\n\n");
14
- const typeDefs = mergeTypeDefs([mergedDefs]);
13
+ const mergedSchemas = schemas.map((schema) => schema.def).join("\n\n");
14
+ const typeDefs = mergeTypeDefs([mergedSchemas]);
15
15
  const mergedResolvers = mergeResolvers(resolvers.map((r) => r.resolver));
16
16
  return {
17
17
  typeDefs,
@@ -22,7 +22,7 @@ function createMergedSchema() {
22
22
  throw error;
23
23
  }
24
24
  }
25
- let apolloServer;
25
+ let apolloServer = null;
26
26
  function createApolloServer() {
27
27
  if (!apolloServer) {
28
28
  const { typeDefs, resolvers: mergedResolvers } = createMergedSchema();
@@ -35,7 +35,7 @@ function createApolloServer() {
35
35
  }
36
36
  return apolloServer;
37
37
  }
38
- var apollo_server_default = startServerAndCreateH3Handler(createApolloServer, { context: async (event) => ({ event }) });
38
+ var apollo_server_default = startServerAndCreateH3Handler(apolloServer || createApolloServer, { context: async (event) => ({ event }) });
39
39
 
40
40
  //#endregion
41
41
  export { apollo_server_default as default };
@@ -1,6 +1,6 @@
1
- import * as h36 from "h3";
1
+ import * as h33 from "h3";
2
2
 
3
3
  //#region src/routes/graphql-yoga.d.ts
4
- declare const _default: h36.EventHandler<h36.EventHandlerRequest, Promise<Response>>;
4
+ declare const _default: h33.EventHandler<h33.EventHandlerRequest, Promise<Response>>;
5
5
  //#endregion
6
6
  export { _default as default };
@@ -1,8 +1,8 @@
1
1
  import defu from "defu";
2
2
  import { mergeResolvers, mergeTypeDefs } from "@graphql-tools/merge";
3
3
  import { importedConfig } from "#nitro-internal-virtual/graphql-config";
4
- import { defs } from "#nitro-internal-virtual/server-defs";
5
4
  import { resolvers } from "#nitro-internal-virtual/server-resolvers";
5
+ import { schemas } from "#nitro-internal-virtual/server-schemas";
6
6
  import { defineEventHandler, toWebRequest } from "h3";
7
7
  import { createSchema, createYoga } from "graphql-yoga";
8
8
 
@@ -26,8 +26,8 @@ new window.EmbeddedSandbox({
26
26
  </html>`;
27
27
  function createMergedSchema() {
28
28
  try {
29
- const mergedDefs = defs.map((schema) => schema.def).join("\n\n");
30
- const typeDefs = mergeTypeDefs([mergedDefs]);
29
+ const mergedSchemas = schemas.map((schema) => schema.def).join("\n\n");
30
+ const typeDefs = mergeTypeDefs([mergedSchemas]);
31
31
  const mergedResolvers = mergeResolvers(resolvers.map((r) => r.resolver));
32
32
  return createSchema({
33
33
  typeDefs,
@@ -1,7 +1,7 @@
1
- import * as h33 from "h3";
1
+ import * as h31 from "h3";
2
2
 
3
3
  //#region src/routes/health.d.ts
4
- declare const _default: h33.EventHandler<h33.EventHandlerRequest, Promise<{
4
+ declare const _default: h31.EventHandler<h31.EventHandlerRequest, Promise<{
5
5
  status: string;
6
6
  message: string;
7
7
  timestamp: string;
@@ -19,13 +19,19 @@ interface GenImport {
19
19
  }
20
20
  declare module 'nitropack/types' {
21
21
  interface Nitro {
22
- scanDefs: string[];
22
+ scanSchemas: string[];
23
+ scanDocuments: string[];
23
24
  scanResolvers: GenImport[];
24
25
  graphql: {
25
26
  buildDir: string;
26
27
  watchDirs: string[];
27
28
  clientDir: string;
28
29
  serverDir: string;
30
+ dir: {
31
+ build: string;
32
+ client: string;
33
+ server: string;
34
+ };
29
35
  };
30
36
  }
31
37
  }
@@ -3,12 +3,12 @@ import { eventHandler, getHeaders, isMethod, readBody, setHeaders } from "h3";
3
3
 
4
4
  //#region src/utils/apollo.ts
5
5
  function startServerAndCreateH3Handler(server, options) {
6
- const apolloServer = typeof server === "function" ? server() : server;
7
- apolloServer.startInBackgroundHandlingStartupErrorsByLoggingAndFailingAllRequests();
8
6
  const defaultContext = () => Promise.resolve({});
9
7
  const contextFunction = options?.context ?? defaultContext;
10
8
  return eventHandler({
11
9
  handler: async (event) => {
10
+ const apolloServer = typeof server === "function" ? server() : server;
11
+ apolloServer.startInBackgroundHandlingStartupErrorsByLoggingAndFailingAllRequests();
12
12
  if (isMethod(event, "OPTIONS")) return null;
13
13
  try {
14
14
  const graphqlRequest = await toGraphqlRequest(event);
@@ -9,7 +9,7 @@ declare function scanGraphql(nitro: Nitro): Promise<string[]>;
9
9
  declare function scanResolvers(nitro: Nitro): Promise<GenImport[]>;
10
10
  declare function scanDirectives(nitro: Nitro): Promise<string[]>;
11
11
  declare function scanTypeDefs(nitro: Nitro): Promise<string[]>;
12
- declare function scanDefs(nitro: Nitro): Promise<string[]>;
13
- declare function unique(arr: any[]): any[];
12
+ declare function scanSchemas(nitro: Nitro): Promise<string[]>;
13
+ declare function scanDocs(nitro: Nitro): Promise<string[]>;
14
14
  //#endregion
15
- export { GLOB_SCAN_PATTERN, getImportId, relativeWithDot, scanDefs, scanDirectives, scanGraphql, scanResolvers, scanTypeDefs, unique };
15
+ export { GLOB_SCAN_PATTERN, getImportId, relativeWithDot, scanDirectives, scanDocs, scanGraphql, scanResolvers, scanSchemas, scanTypeDefs };
@@ -71,10 +71,14 @@ async function scanTypeDefs(nitro) {
71
71
  const files = await scanFiles(nitro, "graphql", "**/*.typedef.{ts,js}");
72
72
  return files.map((f) => f.fullPath);
73
73
  }
74
- async function scanDefs(nitro) {
74
+ async function scanSchemas(nitro) {
75
75
  const files = await scanFiles(nitro, "graphql", "**/*.graphql");
76
76
  return files.map((f) => f.fullPath);
77
77
  }
78
+ async function scanDocs(nitro) {
79
+ const files = await scanDir(nitro, nitro.options.rootDir, nitro.graphql.dir.client, "**/*.graphql");
80
+ return files.map((f) => f.fullPath);
81
+ }
78
82
  async function scanFiles(nitro, name, globPattern = GLOB_SCAN_PATTERN) {
79
83
  const files = await Promise.all(nitro.options.scanDirs.map((dir) => scanDir(nitro, dir, name, globPattern))).then((r) => r.flat());
80
84
  return files;
@@ -99,9 +103,6 @@ async function scanDir(nitro, dir, name, globPattern = GLOB_SCAN_PATTERN) {
99
103
  };
100
104
  }).sort((a, b) => a.path.localeCompare(b.path));
101
105
  }
102
- function unique(arr) {
103
- return [...new Set(arr)];
104
- }
105
106
 
106
107
  //#endregion
107
- export { GLOB_SCAN_PATTERN, getImportId, relativeWithDot, scanDefs, scanDirectives, scanGraphql, scanResolvers, scanTypeDefs, unique };
108
+ export { GLOB_SCAN_PATTERN, getImportId, relativeWithDot, scanDirectives, scanDocs, scanGraphql, scanResolvers, scanSchemas, scanTypeDefs };
@@ -2,6 +2,6 @@ import { Nitro } from "nitropack";
2
2
 
3
3
  //#region src/utils/server-type-generation.d.ts
4
4
  declare function serverTypeGeneration(app: Nitro): Promise<void>;
5
- declare function clientTypeGeneration(app: Nitro, path: string): Promise<void>;
5
+ declare function clientTypeGeneration(nitro: Nitro): Promise<void>;
6
6
  //#endregion
7
7
  export { clientTypeGeneration, serverTypeGeneration };
@@ -11,14 +11,14 @@ import { printSchemaWithDirectives } from "@graphql-tools/utils";
11
11
  //#region src/utils/server-type-generation.ts
12
12
  async function serverTypeGeneration(app) {
13
13
  try {
14
- const defs = app.scanDefs || [];
15
- if (!defs.length) {
14
+ const schemas = app.scanSchemas || [];
15
+ if (!schemas.length) {
16
16
  consola.info("No GraphQL definitions found for server type generation.");
17
17
  return;
18
18
  }
19
- const loadDefs = loadFilesSync(defs);
20
- const mergedDefs = mergeTypeDefs(loadDefs);
21
- const schema = buildASTSchema(mergedDefs, {
19
+ const loadSchemas = loadFilesSync(schemas);
20
+ const mergedSchemas = mergeTypeDefs(loadSchemas);
21
+ const schema = buildASTSchema(mergedSchemas, {
22
22
  assumeValidSDL: true,
23
23
  assumeValid: true
24
24
  });
@@ -34,22 +34,21 @@ async function serverTypeGeneration(app) {
34
34
  consola.error("Server schema generation error:", error);
35
35
  }
36
36
  }
37
- async function clientTypeGeneration(app, path) {
37
+ async function clientTypeGeneration(nitro) {
38
38
  try {
39
- const root = app.graphql.watchDirs.find((dir) => path.startsWith(dir)) || path;
40
- if (!root) return;
41
- const docs = await loadGraphQLDocuments(root);
42
- const schemaFilePath = join(app.graphql.buildDir, "schema.graphql");
39
+ const docs = nitro.scanDocuments;
40
+ const loadDocs = await loadGraphQLDocuments(docs);
41
+ const schemaFilePath = join(nitro.graphql.buildDir, "schema.graphql");
43
42
  if (!existsSync(schemaFilePath)) {
44
43
  consola.info("Schema file not ready yet for client type generation. Server types need to be generated first.");
45
44
  return;
46
45
  }
47
46
  const graphqlString = readFileSync(schemaFilePath, "utf-8");
48
47
  const schema = buildSchema(graphqlString);
49
- const types = await generateClientTypes(schema, docs);
48
+ const types = await generateClientTypes(schema, loadDocs, nitro.options.graphql?.codegen?.client ?? {});
50
49
  if (types === false) return;
51
- const clientTypesPath = resolve(app.options.buildDir, "types", "nitro-graphql-client.d.ts");
52
- const sdkTypesPath = resolve(app.graphql.clientDir, "sdk.ts");
50
+ const clientTypesPath = resolve(nitro.options.buildDir, "types", "nitro-graphql-client.d.ts");
51
+ const sdkTypesPath = resolve(nitro.graphql.clientDir, "sdk.ts");
53
52
  writeFileSync(clientTypesPath, types.types, "utf-8");
54
53
  writeFileSync(sdkTypesPath, types.sdk, "utf-8");
55
54
  } catch (error) {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "nitro-graphql",
3
3
  "type": "module",
4
- "version": "0.3.2",
4
+ "version": "0.4.0",
5
5
  "description": "GraphQL integration for Nitro",
6
6
  "license": "MIT",
7
7
  "sideEffects": false,