Skip to content

Commit fefac70

Browse files
committed
Store generated TypeComposer from mongoose model in mongoose object, for further reusing if needed.
Sometimes one model/schema can be used in different model. So if it already generated, then we should use this unique instance.
1 parent 6564d2d commit fefac70

File tree

2 files changed

+8
-0
lines changed

2 files changed

+8
-0
lines changed

src/definition.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/* @flow */
22
/* eslint-disable */
33

4+
import type { TypeComposer } from 'graphql-compose';
45
import type { connectionSortMapOpts as _connectionSortMapOpts} from 'graphql-compose-connection/lib/definition';
56
export type connectionSortMapOpts = _connectionSortMapOpts;
67

@@ -22,6 +23,7 @@ export type MonooseModelIndex = [
2223
];
2324

2425
export type MongooseModelT = {
26+
_gqcTypeComposer: TypeComposer,
2527
modelName: string,
2628
schema: MongooseModelSchemaT,
2729
create(doc: Object | Object[]): Promise<Object>,

src/fieldsConverter.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,10 @@ export function convertModelToGraphQL(
126126
+ '`name` argument should be non-empty string.');
127127
}
128128

129+
if (model._gqcTypeComposer) {
130+
return model._gqcTypeComposer;
131+
}
132+
129133
const typeComposer = new TypeComposer(
130134
new GraphQLObjectType({
131135
name: typeName,
@@ -135,6 +139,8 @@ export function convertModelToGraphQL(
135139
})
136140
);
137141

142+
model._gqcTypeComposer = typeComposer; // eslint-disable-line
143+
138144
const mongooseFields = getFieldsFromModel(model, typeName);
139145
const graphqlFields = {};
140146

0 commit comments

Comments
 (0)