Skip to content

Commit c70e169

Browse files
fix: integrate Apollo Federation support in type generation (#37)
* fix: integrate Apollo Federation support in type generation * fix: update playground paths in tsdown configuration
1 parent f79d92e commit c70e169

File tree

2 files changed

+15
-34
lines changed

2 files changed

+15
-34
lines changed

src/utils/type-generation.ts

Lines changed: 13 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { Nitro } from 'nitropack'
22

33
import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs'
4+
import { buildSubgraphSchema } from '@apollo/subgraph'
45
import { loadFilesSync } from '@graphql-tools/load-files'
56
import { mergeTypeDefs } from '@graphql-tools/merge'
67
import { printSchemaWithDirectives } from '@graphql-tools/utils'
@@ -277,42 +278,20 @@ export async function serverTypeGeneration(app: Nitro) {
277278
return // Exit early if duplicates found
278279
}
279280

280-
const mergedSchemasString = schemaStrings.join('\n\n')
281-
282281
// Add Federation directives for buildSchema if federation is enabled
283282
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-
}
308283

309-
const mergedSchemas = mergeTypeDefs([schemaWithDirectives], {
284+
const mergedSchemas = mergeTypeDefs([schemaStrings.join('\n\n')], {
310285
throwOnConflict: true,
311286
commentDescriptions: true,
312287
sort: true,
313288
})
314289

315-
const schema = buildSchema(mergedSchemas)
290+
const schema = federationEnabled
291+
? buildSubgraphSchema([{
292+
typeDefs: parse(mergedSchemas),
293+
}])
294+
: buildSchema(mergedSchemas)
316295

317296
const data = await generateTypes(app.options.graphql?.framework || 'graphql-yoga', schema, app.options.graphql ?? {})
318297

@@ -398,7 +377,12 @@ async function generateMainClientTypes(nitro: Nitro) {
398377
}
399378

400379
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)
402386

403387
const types = await generateClientTypes(schema, loadDocs, nitro.options.graphql?.codegen?.client ?? {}, nitro.options.graphql?.codegen?.clientSDK ?? {})
404388
if (types === false) {

tsdown.config.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,10 @@ import packageJson from './package.json' with { type: 'json' }
33

44
const { dependencies, peerDependencies } = packageJson
55

6-
const playground = './playground'
7-
const playgroundNuxt = './playground-nuxt'
6+
const playgrounds = './playgrounds'
87

98
const thisPath = import.meta.url
10-
const playgroundPath = new URL(playground, thisPath).pathname
11-
const playgroundNuxtPath = new URL(playgroundNuxt, thisPath).pathname
9+
const playgroundPath = new URL(playgrounds, thisPath).pathname
1210

1311
export default defineConfig({
1412
entry: [
@@ -50,6 +48,5 @@ export default defineConfig({
5048
],
5149
ignoreWatch: [
5250
playgroundPath,
53-
playgroundNuxtPath,
5451
],
5552
})

0 commit comments

Comments
 (0)