apollo-server-autoloader - Autoload your typeDefs, resolvers and datasources
This package enables autoloading of schemas, resolvers and datasources based on configurable naming conventions.
Install
npm i apollo-server-autoloader
yarn add apollo-server-autoloader
Usage
The following example asumes, that you define your types, resolvers and datasources in the api folder of your project.
import * as path from 'path'
import { buildFederatedSchema } from '@apollo/federation'
import { ApolloServer } from '...'
import { Autoloader } from 'apollo-server-autoloader'
const autoloaderConfig = {
searchPaths: [path.resolve(__dirname, 'api')]
}
const createServer = async () => {
import autoloader = Autoloader(autoloaderConfig)
const typeDefs = await autoloader.getTypeDefs()
const resolvers = await autoloader.getResolvers()
const datasources = await autoloader.getDatasources()
return new ApolloServer({
dataSources,
schema: buildFederatedSchema([{ typeDefs, resolvers }])
...
});
}
Autoloader config
interface AutoloaderConfig {
searchPaths: string[]
conventions?: FileNamingConventions
fileExtensions?: string[]
exclude: string[]
}
interface FileNamingConventions {
types?: NamingConvention
resolvers?: NamingConvention
datasources?: NamingConvention
}
interface NamingConvention {
[key: string]: ExportPattern
}
Example Config
Asume your project structure looks like follows:
|- /api
|- |- schemaFile.ts
|- |- queriesFile.ts
|- |- mutationsFile.ty
|- |- DatasourceFile.ts
|- |- /subolder
|- |- |- schemaFile.ts
...
Your file exports look like follows:
export const myTypeDef = gql`...`
export const myQueries = { ... }
export const myMutations = { ... }
export class MyDatasource { ... }
In this case the corresponding config for the autoloader should look like follows:
const config = {
searchPaths: [path.resolve(__dirname, 'api')],
conventions: {
types: {
schemaFile: 'myTypeDef',
},
resolvers: {
queriesFile: 'myQueries',
mutationsFile: 'myMutations',
},
datasources: {
DatasourceFile: 'MyDatasource',
}
},
fileExtensions: ['.ts'],
exclude: ['[^\/]+\.(spec|test)', '__tests__'],
}
Default config
The default config for the autoloader looks like follows:
export const defaultConfig: AutoloaderConfig = {
searchPaths: ['.'],
conventions: {
types: { schema: 'typeDef' },
resolvers: { queries: 'queries', mutations: 'mutations' },
datasources: { Datasource: 'Datasource' },
},
fileExtensions: ['ts']
}
If you don't change the config, this means you must name your schema files schema.ts, your resolver files queries.ts or mutations.ts and your datasource files Datasource.ts. The export for schema files must be named typeDef, for resolver files queries or mutations and Datasource for datasource files.