nitro-graphql 1.4.1 → 1.4.3

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,6 +1,6 @@
1
- import * as h33 from "h3";
1
+ import * as h30 from "h3";
2
2
 
3
3
  //#region src/routes/apollo-server.d.ts
4
- declare const _default: h33.EventHandler<h33.EventHandlerRequest, Promise<any>>;
4
+ declare const _default: h30.EventHandler<h30.EventHandlerRequest, Promise<any>>;
5
5
  //#endregion
6
6
  export { _default as default };
@@ -1,4 +1,3 @@
1
- import { startServerAndCreateH3Handler } from "../utils/apollo.js";
2
1
  import { consola as consola$1 } from "consola";
3
2
  import defu from "defu";
4
3
  import { parse } from "graphql";
@@ -12,6 +11,7 @@ import { ApolloServer } from "@apollo/server";
12
11
  import { ApolloServerPluginLandingPageLocalDefault } from "@apollo/server/plugin/landingPage/default";
13
12
  import { makeExecutableSchema } from "@graphql-tools/schema";
14
13
  import { defineEventHandler } from "h3";
14
+ import { startServerAndCreateH3Handler } from "nitro-graphql/utils/apollo";
15
15
 
16
16
  //#region src/routes/apollo-server.ts
17
17
  let buildSubgraphSchema = null;
@@ -65,6 +65,7 @@ async function createMergedSchema() {
65
65
  }
66
66
  }
67
67
  let apolloServer = null;
68
+ let serverStarted = false;
68
69
  async function createApolloServer() {
69
70
  if (!apolloServer) {
70
71
  const schema = await createMergedSchema();
@@ -73,6 +74,10 @@ async function createApolloServer() {
73
74
  introspection: true,
74
75
  plugins: [ApolloServerPluginLandingPageLocalDefault({ embed: true })]
75
76
  }, importedConfig));
77
+ if (!serverStarted) {
78
+ await apolloServer.start();
79
+ serverStarted = true;
80
+ }
76
81
  }
77
82
  return apolloServer;
78
83
  }
@@ -80,7 +85,10 @@ let serverPromise = null;
80
85
  var apollo_server_default = defineEventHandler(async (event) => {
81
86
  if (!serverPromise) serverPromise = createApolloServer();
82
87
  const server = await serverPromise;
83
- const h3Handler = startServerAndCreateH3Handler(server, { context: async () => ({ event }) });
88
+ const h3Handler = startServerAndCreateH3Handler(server, {
89
+ context: async () => ({ event }),
90
+ serverAlreadyStarted: true
91
+ });
84
92
  return h3Handler(event);
85
93
  });
86
94
 
@@ -1,6 +1,6 @@
1
- import * as h30 from "h3";
1
+ import * as h31 from "h3";
2
2
 
3
3
  //#region src/routes/graphql-yoga.d.ts
4
- declare const _default: h30.EventHandler<h30.EventHandlerRequest, Promise<Response>>;
4
+ declare const _default: h31.EventHandler<h31.EventHandlerRequest, Promise<Response>>;
5
5
  //#endregion
6
6
  export { _default as default };
@@ -1,7 +1,7 @@
1
- import * as h31 from "h3";
1
+ import * as h33 from "h3";
2
2
 
3
3
  //#region src/routes/health.d.ts
4
- declare const _default: h31.EventHandler<h31.EventHandlerRequest, Promise<{
4
+ declare const _default: h33.EventHandler<h33.EventHandlerRequest, Promise<{
5
5
  status: string;
6
6
  message: string;
7
7
  timestamp: string;
@@ -10,6 +10,7 @@ interface H3ContextFunctionArgument {
10
10
  interface H3HandlerOptions<TContext extends BaseContext> {
11
11
  context?: ContextFunction<[H3ContextFunctionArgument], TContext>;
12
12
  websocket?: Partial<Hooks>;
13
+ serverAlreadyStarted?: boolean;
13
14
  }
14
15
  declare function startServerAndCreateH3Handler(server: ApolloServer<BaseContext> | (() => ApolloServer<BaseContext>), options?: H3HandlerOptions<BaseContext>): EventHandler;
15
16
  declare function startServerAndCreateH3Handler<TContext extends BaseContext>(server: ApolloServer<TContext> | (() => ApolloServer<TContext>), options: WithRequired<H3HandlerOptions<TContext>, 'context'>): EventHandler;
@@ -8,7 +8,7 @@ function startServerAndCreateH3Handler(server, options) {
8
8
  return eventHandler({
9
9
  handler: async (event) => {
10
10
  const apolloServer = typeof server === "function" ? server() : server;
11
- apolloServer.startInBackgroundHandlingStartupErrorsByLoggingAndFailingAllRequests();
11
+ if (!options?.serverAlreadyStarted) apolloServer.startInBackgroundHandlingStartupErrorsByLoggingAndFailingAllRequests();
12
12
  if (isMethod(event, "OPTIONS")) return null;
13
13
  try {
14
14
  const graphqlRequest = await toGraphqlRequest(event);
@@ -4,6 +4,7 @@ import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
4
4
  import consola from "consola";
5
5
  import { basename, dirname, join, resolve } from "pathe";
6
6
  import { buildSchema, parse } from "graphql";
7
+ import { buildSubgraphSchema } from "@apollo/subgraph";
7
8
  import { loadFilesSync } from "@graphql-tools/load-files";
8
9
  import { mergeTypeDefs } from "@graphql-tools/merge";
9
10
  import { printSchemaWithDirectives } from "@graphql-tools/utils";
@@ -192,36 +193,13 @@ async function serverTypeGeneration(app) {
192
193
  const schemaStrings = loadSchemas.map((schema$1) => typeof schema$1 === "string" ? schema$1 : schema$1.loc?.source?.body || "").filter(Boolean);
193
194
  const isValid = validateNoDuplicateTypes(schemas, schemaStrings);
194
195
  if (!isValid) return;
195
- const mergedSchemasString = schemaStrings.join("\n\n");
196
196
  const federationEnabled = app.options.graphql?.federation?.enabled === true;
197
- let schemaWithDirectives = mergedSchemasString;
198
- if (federationEnabled) {
199
- const federationDirectives = `
200
- directive @key(fields: String!) on OBJECT | INTERFACE
201
- directive @requires(fields: String!) on FIELD_DEFINITION
202
- directive @provides(fields: String!) on FIELD_DEFINITION
203
- directive @external on FIELD_DEFINITION | OBJECT
204
- directive @tag(name: String!) on FIELD_DEFINITION | OBJECT | INTERFACE | UNION | ARGUMENT_DEFINITION | SCALAR | ENUM | ENUM_VALUE | INPUT_OBJECT | INPUT_FIELD_DEFINITION
205
- directive @extends on OBJECT | INTERFACE
206
- directive @shareable on FIELD_DEFINITION | OBJECT
207
- directive @inaccessible on FIELD_DEFINITION | OBJECT | INTERFACE | UNION | ARGUMENT_DEFINITION | SCALAR | ENUM | ENUM_VALUE | INPUT_OBJECT | INPUT_FIELD_DEFINITION
208
- directive @override(from: String!) on FIELD_DEFINITION
209
- directive @composeDirective(name: String!) on SCHEMA
210
- directive @link(url: String!, as: String, for: Purpose, import: [String!]) on SCHEMA
211
-
212
- enum Purpose {
213
- SECURITY
214
- EXECUTION
215
- }
216
- `;
217
- schemaWithDirectives = `${federationDirectives}\n\n${mergedSchemasString}`;
218
- }
219
- const mergedSchemas = mergeTypeDefs([schemaWithDirectives], {
197
+ const mergedSchemas = mergeTypeDefs([schemaStrings.join("\n\n")], {
220
198
  throwOnConflict: true,
221
199
  commentDescriptions: true,
222
200
  sort: true
223
201
  });
224
- const schema = buildSchema(mergedSchemas);
202
+ const schema = federationEnabled ? buildSubgraphSchema([{ typeDefs: parse(mergedSchemas) }]) : buildSchema(mergedSchemas);
225
203
  const data = await generateTypes(app.options.graphql?.framework || "graphql-yoga", schema, app.options.graphql ?? {});
226
204
  const printSchema = printSchemaWithDirectives(schema);
227
205
  const schemaPath = resolve(app.graphql.buildDir, "schema.graphql");
@@ -281,7 +259,8 @@ async function generateMainClientTypes(nitro) {
281
259
  return;
282
260
  }
283
261
  const graphqlString = readFileSync(schemaFilePath, "utf-8");
284
- const schema = buildSchema(graphqlString);
262
+ const federationEnabled = nitro.options.graphql?.federation?.enabled === true;
263
+ const schema = federationEnabled ? buildSubgraphSchema([{ typeDefs: parse(graphqlString) }]) : buildSchema(graphqlString);
285
264
  const types = await generateClientTypes(schema, loadDocs, nitro.options.graphql?.codegen?.client ?? {}, nitro.options.graphql?.codegen?.clientSDK ?? {});
286
265
  if (types === false) return;
287
266
  const clientTypesPath = resolve(nitro.options.buildDir, "types", "nitro-graphql-client.d.ts");
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "nitro-graphql",
3
3
  "type": "module",
4
- "version": "1.4.1",
4
+ "version": "1.4.3",
5
5
  "description": "GraphQL integration for Nitro",
6
6
  "license": "MIT",
7
7
  "sideEffects": false,
@@ -40,6 +40,10 @@
40
40
  "types": "./dist/utils/define.d.ts",
41
41
  "import": "./dist/utils/define.js"
42
42
  },
43
+ "./utils/apollo": {
44
+ "types": "./dist/utils/apollo.d.ts",
45
+ "import": "./dist/utils/apollo.js"
46
+ },
43
47
  "./internal": {
44
48
  "types": "./dist/internal/index.d.ts",
45
49
  "import": "./dist/internal/index.js"
@@ -112,7 +116,7 @@
112
116
  "changelogen": "^0.6.2",
113
117
  "crossws": "0.3.5",
114
118
  "eslint": "^9.34.0",
115
- "graphql": "^16.11.0",
119
+ "graphql": "16.11.0",
116
120
  "graphql-yoga": "^5.15.1",
117
121
  "h3": "1.15.3",
118
122
  "nitropack": "^2.12.5",