From 3e656032516cc672bb9648be7e03ce02159049fd Mon Sep 17 00:00:00 2001 From: ianwremmel <1182361+ianwremmel@users.noreply.github.com> Date: Sat, 8 Apr 2023 19:44:33 -0700 Subject: [PATCH] feat(foundation): move schema.graphqls to IR package --- .graphqlrc.js | 6 ++--- .nx-cache-buster | 2 +- .../foundation-cli/src/codegen.ts | 4 ++-- .../schema.graphqls | 0 .../src/index.ts | 1 + .../src/schema.ts | 22 +++++++++++++++++++ .../foundation-parser/src/test-helpers.ts | 10 ++------- .../src/plugin.ts | 16 +++----------- .../src/plugin.ts | 17 +++----------- 9 files changed, 37 insertions(+), 41 deletions(-) rename schema.graphqls => packages/@code-like-a-carpenter/foundation-intermediate-representation/schema.graphqls (100%) create mode 100644 packages/@code-like-a-carpenter/foundation-intermediate-representation/src/schema.ts diff --git a/.graphqlrc.js b/.graphqlrc.js index b886745d..d08cb21a 100644 --- a/.graphqlrc.js +++ b/.graphqlrc.js @@ -17,9 +17,9 @@ const config = { `examples/${example}/schema/**/*.graphqls`, // This line loads types necessary for the collective schema to be valid. 'examples/common.graphqls', - // This line is temporary and should be removed once the schema is - // supplied via addToSchema - 'schema.graphqls', + // I haven't come up with a better way to get the core schema other than + // specifying it directly out of node_modules + 'node_modules/@code-like-a-carpenter/foundation-intermediate-representation/schema.graphqls', ], }; return acc; diff --git a/.nx-cache-buster b/.nx-cache-buster index d1abccc3..c1349b28 100644 --- a/.nx-cache-buster +++ b/.nx-cache-buster @@ -1 +1 @@ -2023-04-05T09:33:26-07:00 +2023-04-08T19:43:30-07:00 diff --git a/packages/@code-like-a-carpenter/foundation-cli/src/codegen.ts b/packages/@code-like-a-carpenter/foundation-cli/src/codegen.ts index b9dcc4e9..a80616f3 100644 --- a/packages/@code-like-a-carpenter/foundation-cli/src/codegen.ts +++ b/packages/@code-like-a-carpenter/foundation-cli/src/codegen.ts @@ -25,8 +25,8 @@ export async function generateCode({config}: {config: string}) { } = await loadConfig(config); const schema = await loadSchema(relativeRoot, schemaGlob, [ - foundationCloudformationPlugin.addToSchema(), - foundationTypescriptPlugin.addToSchema(), + foundationCloudformationPlugin.addToSchema, + foundationTypescriptPlugin.addToSchema, ]); const cwd = process.cwd(); diff --git a/schema.graphqls b/packages/@code-like-a-carpenter/foundation-intermediate-representation/schema.graphqls similarity index 100% rename from schema.graphqls rename to packages/@code-like-a-carpenter/foundation-intermediate-representation/schema.graphqls diff --git a/packages/@code-like-a-carpenter/foundation-intermediate-representation/src/index.ts b/packages/@code-like-a-carpenter/foundation-intermediate-representation/src/index.ts index 44c29c8a..100fb402 100644 --- a/packages/@code-like-a-carpenter/foundation-intermediate-representation/src/index.ts +++ b/packages/@code-like-a-carpenter/foundation-intermediate-representation/src/index.ts @@ -1,5 +1,6 @@ export * from './field'; export * from './intermediate-representation'; export * from './model'; +export * from './schema'; export * from './table'; export * from './types'; diff --git a/packages/@code-like-a-carpenter/foundation-intermediate-representation/src/schema.ts b/packages/@code-like-a-carpenter/foundation-intermediate-representation/src/schema.ts new file mode 100644 index 00000000..10c07772 --- /dev/null +++ b/packages/@code-like-a-carpenter/foundation-intermediate-representation/src/schema.ts @@ -0,0 +1,22 @@ +import fs from 'node:fs'; +import path from 'node:path'; + +function loadSchema() { + try { + // we're in dist. + return fs.readFileSync( + path.resolve(__dirname, '..', '..', 'schema.graphqls'), + 'utf8' + ); + } catch (err) { + if (err instanceof Error && 'code' in err && err.code === 'ENOENT') { + // we're in src. + return fs.readFileSync( + path.resolve(__dirname, '..', 'schema.graphqls'), + 'utf8' + ); + } + throw err; + } +} +export const schema = loadSchema(); diff --git a/packages/@code-like-a-carpenter/foundation-parser/src/test-helpers.ts b/packages/@code-like-a-carpenter/foundation-parser/src/test-helpers.ts index a339071e..77ffaa2f 100644 --- a/packages/@code-like-a-carpenter/foundation-parser/src/test-helpers.ts +++ b/packages/@code-like-a-carpenter/foundation-parser/src/test-helpers.ts @@ -1,17 +1,11 @@ -import {readFile} from 'node:fs/promises'; -import path from 'node:path'; - import {buildSchema} from 'graphql'; +import {schema as coreSchema} from '@code-like-a-carpenter/foundation-intermediate-representation'; + import {ParserConfigSchema} from './config'; import {parse} from './parser'; export async function parseSchema(raw: string) { - const coreSchema = await readFile( - path.resolve(__dirname, '..', '..', '..', '..', './schema.graphqls'), - 'utf8' - ); - const schema = buildSchema(`${coreSchema}\n${raw}`); return parse( diff --git a/packages/@code-like-a-carpenter/foundation-plugin-cloudformation/src/plugin.ts b/packages/@code-like-a-carpenter/foundation-plugin-cloudformation/src/plugin.ts index 99510368..7a3693c0 100644 --- a/packages/@code-like-a-carpenter/foundation-plugin-cloudformation/src/plugin.ts +++ b/packages/@code-like-a-carpenter/foundation-plugin-cloudformation/src/plugin.ts @@ -1,11 +1,7 @@ import assert from 'assert'; -import fs, {readFileSync} from 'fs'; -import path from 'path'; +import fs from 'fs'; -import type { - AddToSchemaResult, - PluginFunction, -} from '@graphql-codegen/plugin-helpers'; +import type {PluginFunction} from '@graphql-codegen/plugin-helpers'; import yml from 'js-yaml'; import {CLOUDFORMATION_SCHEMA} from 'js-yaml-cloudformation-schema'; @@ -19,13 +15,7 @@ import {combineFragments} from './fragments/combine-fragments'; import {defineTable} from './table'; import type {ServerlessApplicationModel} from './types'; -/** @override */ -export function addToSchema(): AddToSchemaResult { - return readFileSync( - path.resolve(__dirname, '../../../../../schema.graphqls'), - 'utf8' - ); -} +export {schema as addToSchema} from '@code-like-a-carpenter/foundation-intermediate-representation'; /** * Loads an existing consumer-generated CF template or returns a basic template diff --git a/packages/@code-like-a-carpenter/foundation-plugin-typescript/src/plugin.ts b/packages/@code-like-a-carpenter/foundation-plugin-typescript/src/plugin.ts index f46667e6..8a9ea975 100644 --- a/packages/@code-like-a-carpenter/foundation-plugin-typescript/src/plugin.ts +++ b/packages/@code-like-a-carpenter/foundation-plugin-typescript/src/plugin.ts @@ -1,17 +1,12 @@ import assert from 'assert'; -import {readFileSync} from 'fs'; -import path from 'path'; -import type { - AddToSchemaResult, - PluginFunction, -} from '@graphql-codegen/plugin-helpers'; +import type {PluginFunction} from '@graphql-codegen/plugin-helpers'; import {parse} from '@code-like-a-carpenter/foundation-parser'; import {makePlugin} from '@code-like-a-carpenter/graphql-codegen-helpers'; -import {ConfigSchema} from './config'; import type {Config} from './config'; +import {ConfigSchema} from './config'; import {filterNull} from './helpers'; import {blindWriteTemplate} from './tables/templates/blind-write'; import {createItemTemplate} from './tables/templates/create-item'; @@ -26,13 +21,7 @@ import {readItemTemplate} from './tables/templates/read-item'; import {unmarshallTpl} from './tables/templates/unmarshall'; import {updateItemTemplate} from './tables/templates/update-item'; -/** @override */ -export function addToSchema(): AddToSchemaResult { - return readFileSync( - path.resolve(__dirname, '../../../../../schema.graphqls'), - 'utf8' - ); -} +export {schema as addToSchema} from '@code-like-a-carpenter/foundation-intermediate-representation'; /** @override */ export const plugin: PluginFunction = makePlugin(