-
Notifications
You must be signed in to change notification settings - Fork 9
/
createDataSources.ts
39 lines (38 loc) · 1.43 KB
/
createDataSources.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/**
* Generate default data sources for models.
*
* As a default, will generate Mongoose data sources
*/
import { VulcanGraphqlModelServer } from "..";
import type { VulcanGenericDataSource } from "./typings";
interface ModelDataSources {
[typeName: string]: VulcanGenericDataSource;
}
/**
* Build a default graphql context for a list of models
*
* NOTE: when coding custom mutation or resolvers, you DON'T need to rely on this context
* It is used internally by Vulcan to generate the default query and mutation resolvers,
* and field/relation resolvers for the dataSources
* @param models
*/
export const createDataSources =
(models: Array<VulcanGraphqlModelServer>) => (): ModelDataSources => {
const dataSources = models.reduce((dataSources, model) => {
// TODO: we should find a way to guarantee that all models have a default connector
/* const connector =
model.crud.connector || createMongooseConnector(model);*/
if (!model.graphql.createDataSource)
throw new Error(
"GraphQL models must have a createDataSource function."
);
return {
...dataSources,
// TODO: in context we use "model.graphql.typeName" but here we use "name"
// The getters are robust to both, but we should clarify
[model.name]: model.graphql.createDataSource() /*||
createMongooseDataSource(model, connector),*/,
};
}, {});
return dataSources;
};