Skip to content

Commit

Permalink
feat: simplify the typeDefs compilation, create a template for ejecte…
Browse files Browse the repository at this point in the history
…d app
  • Loading branch information
lgandecki committed Aug 22, 2023
1 parent 9697ff1 commit 1d2181a
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 52 deletions.
62 changes: 10 additions & 52 deletions src/generate/templates/combineSchemas.ts
Original file line number Diff line number Diff line change
@@ -1,59 +1,17 @@
import { loadTypedefsSync } from '@graphql-tools/load';
import { GraphQLFileLoader, GraphQLFileLoaderOptions } from '@graphql-tools/graphql-file-loader';
import { GraphQLFileLoader } from '@graphql-tools/graphql-file-loader';
import { mergeTypeDefs } from '@graphql-tools/merge';
// @ts-ignore
import shelljs from 'shelljs';
import debugConfigurator from 'debug';

const debug = debugConfigurator('combineSchemas.ts');
const graphqlPaths = `${process.env.PROJECT_PATH || ''}/src/**/*.graphql`;
const graphqlFrameworkPath = `${process.env.PROJECT_PATH || ''}/generated/graphql/genericDataModelSchema.graphql`;

const graphqlPaths = shelljs.ls(`${process.env.PROJECT_PATH || ''}/src/**/*.graphql`);
debug(
'path to GraphQL Schemas',
graphqlPaths.filter((p) => p),
const schema = mergeTypeDefs(
loadTypedefsSync([graphqlPaths, graphqlFrameworkPath], {
loaders: [new GraphQLFileLoader()],
assumeValidSDL: true, // bypassing validation because the files in isolation are not valid, only after compiling together
}).map((s) => {
return s.document!;
}),
);

class ExtendedGraphQLFileLoader extends GraphQLFileLoader {
handleFileContent(rawSDL: string, pointer: string, options: GraphQLFileLoaderOptions) {
const newSdl = rawSDL.replace('extend type Query', 'type Query').replace('extend type Mutation', 'type Mutation');
// .replace(/extend type/g, "type");
return super.handleFileContent(newSdl, pointer, options);
}
}

const userSchema = loadTypedefsSync(graphqlPaths, {
loaders: [new ExtendedGraphQLFileLoader()],
assumeValidSDL: true, // this will bypass validation
}).map((s) => {
return s.document!;
});

const frameworkTypes = loadTypedefsSync(
`
# Generated directives
directive @entity(embedded: Boolean) on OBJECT
directive @chimp(embedded: Boolean) on OBJECT
directive @column(overrideType: String) on FIELD_DEFINITION
directive @id on FIELD_DEFINITION
directive @computed on FIELD_DEFINITION
directive @link(overrideType: String) on FIELD_DEFINITION
directive @embedded on FIELD_DEFINITION
directive @map(path: String!) on FIELD_DEFINITION
directive @union(discriminatorField: String) on UNION
input AdditionalEntityFields {
path: String
type: String
}
directive @predefined on SCALAR
`,
{ loaders: [] },
).map((s) => s.document!);

// eslint-disable-next-line unicorn/prefer-spread
const schema = mergeTypeDefs(userSchema.concat(frameworkTypes));

export default schema;
23 changes: 23 additions & 0 deletions src/generate/templates/ejectedSchema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { buildSubgraphSchema } from '@apollo/subgraph';

Check failure on line 1 in src/generate/templates/ejectedSchema.ts

View workflow job for this annotation

GitHub Actions / test

"@apollo/subgraph" is not found
import { loadTypedefsSync } from '@graphql-tools/load';
import { GraphQLFileLoader } from '@graphql-tools/graphql-file-loader';
import { mergeTypeDefs } from '@graphql-tools/merge';
import { resolvers } from '~app/resolvers';

Check failure on line 5 in src/generate/templates/ejectedSchema.ts

View workflow job for this annotation

GitHub Actions / test

"~app/resolvers" is not found

const graphqlPaths = `${__dirname}/**/*.graphql`;

const typeDefs = mergeTypeDefs(
loadTypedefsSync([graphqlPaths], {
loaders: [new GraphQLFileLoader()],
assumeValidSDL: true, // bypassing validation because the files in isolation are not valid, only after compiling together
}).map((s) => {
return s.document!;
}),
);

export const schema = buildSubgraphSchema([
{
typeDefs,
resolvers,
},
]);

0 comments on commit 1d2181a

Please sign in to comment.