|
1 | 1 | import type { Nitro } from 'nitropack' |
2 | 2 |
|
3 | 3 | import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs' |
| 4 | +import { buildSubgraphSchema } from '@apollo/subgraph' |
4 | 5 | import { loadFilesSync } from '@graphql-tools/load-files' |
5 | 6 | import { mergeTypeDefs } from '@graphql-tools/merge' |
6 | 7 | import { printSchemaWithDirectives } from '@graphql-tools/utils' |
@@ -277,42 +278,20 @@ export async function serverTypeGeneration(app: Nitro) { |
277 | 278 | return // Exit early if duplicates found |
278 | 279 | } |
279 | 280 |
|
280 | | - const mergedSchemasString = schemaStrings.join('\n\n') |
281 | | - |
282 | 281 | // Add Federation directives for buildSchema if federation is enabled |
283 | 282 | const federationEnabled = app.options.graphql?.federation?.enabled === true |
284 | | - let schemaWithDirectives = mergedSchemasString |
285 | | - |
286 | | - if (federationEnabled) { |
287 | | - // Add Federation 2 directives definitions for buildSchema |
288 | | - const federationDirectives = ` |
289 | | - directive @key(fields: String!) on OBJECT | INTERFACE |
290 | | - directive @requires(fields: String!) on FIELD_DEFINITION |
291 | | - directive @provides(fields: String!) on FIELD_DEFINITION |
292 | | - directive @external on FIELD_DEFINITION | OBJECT |
293 | | - directive @tag(name: String!) on FIELD_DEFINITION | OBJECT | INTERFACE | UNION | ARGUMENT_DEFINITION | SCALAR | ENUM | ENUM_VALUE | INPUT_OBJECT | INPUT_FIELD_DEFINITION |
294 | | - directive @extends on OBJECT | INTERFACE |
295 | | - directive @shareable on FIELD_DEFINITION | OBJECT |
296 | | - directive @inaccessible on FIELD_DEFINITION | OBJECT | INTERFACE | UNION | ARGUMENT_DEFINITION | SCALAR | ENUM | ENUM_VALUE | INPUT_OBJECT | INPUT_FIELD_DEFINITION |
297 | | - directive @override(from: String!) on FIELD_DEFINITION |
298 | | - directive @composeDirective(name: String!) on SCHEMA |
299 | | - directive @link(url: String!, as: String, for: Purpose, import: [String!]) on SCHEMA |
300 | | - |
301 | | - enum Purpose { |
302 | | - SECURITY |
303 | | - EXECUTION |
304 | | - } |
305 | | - ` |
306 | | - schemaWithDirectives = `${federationDirectives}\n\n${mergedSchemasString}` |
307 | | - } |
308 | 283 |
|
309 | | - const mergedSchemas = mergeTypeDefs([schemaWithDirectives], { |
| 284 | + const mergedSchemas = mergeTypeDefs([schemaStrings.join('\n\n')], { |
310 | 285 | throwOnConflict: true, |
311 | 286 | commentDescriptions: true, |
312 | 287 | sort: true, |
313 | 288 | }) |
314 | 289 |
|
315 | | - const schema = buildSchema(mergedSchemas) |
| 290 | + const schema = federationEnabled |
| 291 | + ? buildSubgraphSchema([{ |
| 292 | + typeDefs: parse(mergedSchemas), |
| 293 | + }]) |
| 294 | + : buildSchema(mergedSchemas) |
316 | 295 |
|
317 | 296 | const data = await generateTypes(app.options.graphql?.framework || 'graphql-yoga', schema, app.options.graphql ?? {}) |
318 | 297 |
|
@@ -398,7 +377,12 @@ async function generateMainClientTypes(nitro: Nitro) { |
398 | 377 | } |
399 | 378 |
|
400 | 379 | const graphqlString = readFileSync(schemaFilePath, 'utf-8') |
401 | | - const schema = buildSchema(graphqlString) |
| 380 | + const federationEnabled = nitro.options.graphql?.federation?.enabled === true |
| 381 | + const schema = federationEnabled |
| 382 | + ? buildSubgraphSchema([{ |
| 383 | + typeDefs: parse(graphqlString), |
| 384 | + }]) |
| 385 | + : buildSchema(graphqlString) |
402 | 386 |
|
403 | 387 | const types = await generateClientTypes(schema, loadDocs, nitro.options.graphql?.codegen?.client ?? {}, nitro.options.graphql?.codegen?.clientSDK ?? {}) |
404 | 388 | if (types === false) { |
|
0 commit comments