Skip to content

Commit 61a9f41

Browse files
israelglarnodkz
authored andcommitted
feat: add support for required properties
* feat: add support for required properties * Add model with required fields * Fix lint errors Closes #144
1 parent 04e82c3 commit 61a9f41

File tree

5 files changed

+32
-4
lines changed

5 files changed

+32
-4
lines changed

flow-typed/npm/mongoose_v4.x.x.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,7 @@ type Mongoose$SchemaField<Schema> = {
184184
options?: ?{
185185
description: ?string
186186
},
187+
isRequired?: boolean,
187188
enumValues?: ?(string[]),
188189
schema?: Schema,
189190
_index?: ?{ [optionName: string]: mixed }

src/__mocks__/userModel.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,3 +111,15 @@ UserSchema.virtual('nameVirtual').get(function() {
111111
const UserModel = mongoose.model('User', UserSchema);
112112

113113
export { UserSchema, UserModel };
114+
115+
const UserRequiredSchema: SchemaType<any> = new Schema({
116+
name: {
117+
type: String,
118+
description: 'Person name',
119+
required: true,
120+
},
121+
});
122+
123+
const UserRequiredModel = mongoose.model('UserRequired', UserRequiredSchema);
124+
125+
export { UserRequiredSchema, UserRequiredModel };

src/__tests__/fieldConverter-test.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
/* eslint-disable no-unused-expressions, no-template-curly-in-string */
33

44
import { EnumTypeComposer, schemaComposer } from 'graphql-compose';
5-
import { UserModel } from '../__mocks__/userModel';
5+
import { UserModel, UserRequiredModel } from '../__mocks__/userModel';
66
import {
77
deriveComplexType,
88
getFieldsFromModel,
@@ -14,6 +14,7 @@ import {
1414
enumToGraphQL,
1515
documentArrayToGraphQL,
1616
referenceToGraphQL,
17+
convertModelToGraphQL,
1718
} from '../fieldsConverter';
1819
import GraphQLMongoID from '../types/mongoid';
1920

@@ -108,6 +109,13 @@ describe('fieldConverter', () => {
108109
});
109110
});
110111

112+
describe('convertFieldToGraphQL()', () => {
113+
it('should set required field', () => {
114+
const tc = convertModelToGraphQL(UserRequiredModel, 'UserRequired', schemaComposer);
115+
expect(tc.isFieldNonNull('name')).toBeTruthy();
116+
});
117+
});
118+
111119
describe('convertFieldToGraphQL()', () => {
112120
it('should convert any mongoose field to graphQL type', () => {
113121
const mongooseField = {

src/fieldsConverter.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ export function convertModelToGraphQL(
127127
return schemaComposer.getTC(model.schema);
128128
}
129129

130+
const requiredFields = [];
130131
const typeComposer = schemaComposer.getOrCreateTC(typeName);
131132
// $FlowFixMe await landing [email protected] or above
132133
schemaComposer.set(model.schema, typeComposer);
@@ -137,6 +138,11 @@ export function convertModelToGraphQL(
137138

138139
Object.keys(mongooseFields).forEach(fieldName => {
139140
const mongooseField: MongooseFieldT = mongooseFields[fieldName];
141+
142+
if (mongooseField.isRequired) {
143+
requiredFields.push(fieldName);
144+
}
145+
140146
graphqlFields[fieldName] = {
141147
type: convertFieldToGraphQL(mongooseField, typeName, schemaComposer),
142148
description: _getFieldDescription(mongooseField),
@@ -158,6 +164,7 @@ export function convertModelToGraphQL(
158164
});
159165

160166
typeComposer.addFields(graphqlFields);
167+
requiredFields.map(f => typeComposer.makeFieldNonNull(f));
161168
return typeComposer;
162169
}
163170

src/utils/getIndexesFromModel.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ function isSpecificIndex(idx) {
2020
}
2121

2222
/*
23-
* Get mongoose model, and return array of fields with indexes.
24-
* MongooseModel -> [ { _id: 1 }, { name: 1, surname: -1 } ]
25-
*/
23+
* Get mongoose model, and return array of fields with indexes.
24+
* MongooseModel -> [ { _id: 1 }, { name: 1, surname: -1 } ]
25+
*/
2626
export function getIndexesFromModel(
2727
mongooseModel: MongooseModel,
2828
opts: getIndexesFromModelOpts = {}

0 commit comments

Comments
 (0)