|
| 1 | +#!/usr/bin/env node --experimental-specifier-resolution=node |
| 2 | +import path from 'path' |
| 3 | +import fs from 'fs' |
| 4 | +import { fileURLToPath } from 'node:url' |
| 5 | +import { loadFilesSync } from '@graphql-tools/load-files' |
| 6 | +import { mergeTypeDefs } from '@graphql-tools/merge' |
| 7 | +import { print } from 'graphql' |
| 8 | + |
| 9 | +/* --- Constants ------------------------------------------------------------------------------- */ |
| 10 | + |
| 11 | +const currentDir = path.dirname(fileURLToPath(import.meta.url)) |
| 12 | +const schemaPath = path.resolve(currentDir, 'schema.graphql') |
| 13 | +const typeDefsPath = path.resolve(currentDir, 'typeDefs.ts') |
| 14 | + |
| 15 | +/** --- createSchemaDefinitions() -------------------------------------------------------------- */ |
| 16 | +/** -i- Combine all custom and other (e.g. generated) graphql schema definitions */ |
| 17 | +export const createSchemaDefinitions = () => { |
| 18 | + const rootDir = path.resolve(currentDir, '../../..') |
| 19 | + const schemaPathPattern = `${rootDir}/(features|packages)/**/!(schema).graphql` |
| 20 | + const customGraphQLDefinitions = loadFilesSync(schemaPathPattern) |
| 21 | + return mergeTypeDefs([ |
| 22 | + ...customGraphQLDefinitions, |
| 23 | + /* other typedefs? */ |
| 24 | + ]) |
| 25 | +} |
| 26 | + |
| 27 | +/* --- Script ---------------------------------------------------------------------------------- */ |
| 28 | + |
| 29 | +const buildSchemaDefinitions = async () => { |
| 30 | + const schemaDefinitions = createSchemaDefinitions() |
| 31 | + const typeDefsString = print(schemaDefinitions) |
| 32 | + fs.writeFileSync(schemaPath, typeDefsString) |
| 33 | + fs.writeFileSync(typeDefsPath, `export const typeDefs = \`${typeDefsString}\``) |
| 34 | +} |
| 35 | + |
| 36 | +buildSchemaDefinitions() |
0 commit comments