Skip to content

Commit

Permalink
feat(foundation): move schema.graphqls to IR package
Browse files Browse the repository at this point in the history
  • Loading branch information
ianwremmel committed Apr 9, 2023
1 parent 48a8a12 commit 3e65603
Show file tree
Hide file tree
Showing 9 changed files with 37 additions and 41 deletions.
6 changes: 3 additions & 3 deletions .graphqlrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion .nx-cache-buster
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2023-04-05T09:33:26-07:00
2023-04-08T19:43:30-07:00
4 changes: 2 additions & 2 deletions packages/@code-like-a-carpenter/foundation-cli/src/codegen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export * from './field';
export * from './intermediate-representation';
export * from './model';
export * from './schema';
export * from './table';
export * from './types';
Original file line number Diff line number Diff line change
@@ -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();
Original file line number Diff line number Diff line change
@@ -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(
Expand Down
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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<Config> = makePlugin(
Expand Down

0 comments on commit 3e65603

Please sign in to comment.