Skip to content

Instantly share code, notes, and snippets.

@craicoverflow
Created April 7, 2020 14:11
Show Gist options
  • Save craicoverflow/ed4b805e3d44b0504c631afd7dfdcc9a to your computer and use it in GitHub Desktop.
Save craicoverflow/ed4b805e3d44b0504c631afd7dfdcc9a to your computer and use it in GitHub Desktop.
async function createModels(modelsDir: string, schema: GraphQLSchema, defaultDataProvider: GraphbackDataProvider, dbProviderOverrides: { [modelName: string]: GraphbackDataProvider } = {}, pubSub: PubSub): Promise<{ [serviceName: string]: CRUDService }> {
const models = require(modelsDir);
const modelsConfig = { ...models.modelConfigs };
delete models.modelConfigs;
const noteConfig = {
name: "Note",
pubSub: {
publishCreate: false,
publishUpdate: false,
publishDelete: false,
},
}
const services = {};
for (const modelName of Object.keys(models)) {
const modelType = schema.getType(modelName) as GraphQLObjectType
const dbProvider = dbProviderOverrides[modelName] || defaultDataProvider
const modelService: GraphbackCRUDService = new models[modelName](modelType, dbProvider, {
pubSub,
...noteConfig.pubSub
});
services[modelName] = modelService;
}
return services;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment