-
Notifications
You must be signed in to change notification settings - Fork 139
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: simplify the typeDefs compilation, create a template for ejecte…
…d app
- Loading branch information
Showing
2 changed files
with
33 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { buildSubgraphSchema } from '@apollo/subgraph'; | ||
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'; | ||
|
||
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, | ||
}, | ||
]); |