Skip to content

Commit 00ba6ed

Browse files
committed
chore: update
1 parent a40ce3e commit 00ba6ed

File tree

3 files changed

+33
-2
lines changed

3 files changed

+33
-2
lines changed

src/__mocks__/userModel.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,18 @@ const UserSchema = new Schema(
130130
],
131131
},
132132

133+
statusCode: {
134+
type: Number,
135+
enum: Object.values({
136+
ACTIVE: 1,
137+
INACTIVE: 2,
138+
}),
139+
},
140+
141+
numTest: {
142+
type: Number,
143+
},
144+
133145
// createdAt, created via option `timestamp: true` (see bottom)
134146
// updatedAt, created via option `timestamp: true` (see bottom)
135147
},

src/__tests__/fieldConverter-test.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,14 @@ describe('fieldConverter', () => {
116116
it('should derive MIXED mongoose type', () => {
117117
expect(deriveComplexType(fields.someDynamic)).toBe(ComplexTypes.MIXED);
118118
});
119+
120+
it('should derive Number mongoose type', () => {
121+
expect(deriveComplexType(fields.numTest)).toBe(ComplexTypes.SCALAR);
122+
});
123+
124+
it('should derive Enum Number mongoose type', () => {
125+
expect(deriveComplexType(fields.statusCode)).toBe(ComplexTypes.SCALAR);
126+
});
119127
});
120128

121129
describe('convertFieldToGraphQL()', () => {
@@ -163,6 +171,17 @@ describe('fieldConverter', () => {
163171
expect(schemaComposer.has('BSONDecimal')).toBeTruthy();
164172
expect(schemaComposer.get('BSONDecimal').getType()).toBe(GraphQLBSONDecimal);
165173
});
174+
175+
it('should use Scalar[float] for Enum Numbers', () => {
176+
schemaComposer.clear();
177+
expect(schemaComposer.has('statusCode')).toBeFalsy();
178+
const mongooseField = {
179+
path: 'statusCode',
180+
instance: 'Number',
181+
enumValues: [1, 2, 3],
182+
};
183+
expect(convertFieldToGraphQL(mongooseField, '', schemaComposer)).toBe('Float');
184+
});
166185
});
167186

168187
describe('scalarToGraphQL()', () => {

src/fieldsConverter.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,8 @@ export function deriveComplexType(field: MongooseFieldT): ComplexTypes {
294294
return ComplexTypes.REFERENCE;
295295
} else if (fieldType === 'Decimal128') {
296296
return ComplexTypes.DECIMAL;
297+
} else if (fieldType === 'Number') {
298+
return ComplexTypes.SCALAR;
297299
}
298300

299301
const enums = _getFieldEnums(field);
@@ -394,8 +396,6 @@ export function enumToGraphQL(
394396
key = 'NULL';
395397
} else if (value === '') {
396398
key = 'EMPTY_STRING';
397-
} else if (typeof value === 'number') {
398-
key = value;
399399
} else {
400400
key = value
401401
?.toString()

0 commit comments

Comments
 (0)