File tree Expand file tree Collapse file tree 4 files changed +37
-4
lines changed
Expand file tree Collapse file tree 4 files changed +37
-4
lines changed Original file line number Diff line number Diff line change 11{
2- "name" : " graphql-compose-mongoose" ,
3- "version" : " 0 .0.0-semantically-released " ,
2+ "name" : " @area51ai/ graphql-compose-mongoose" ,
3+ "version" : " 10 .0.12 " ,
44 "description" : " Plugin for `graphql-compose` which derive a graphql types from a mongoose model." ,
55 "license" : " MIT" ,
66 "files" : [
Original file line number Diff line number Diff 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 } ,
Original file line number Diff line number Diff 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()' , ( ) => {
Original file line number Diff line number Diff line change @@ -26,7 +26,7 @@ type MongooseFieldT = {
2626 originalRequiredValue ?: string | ( ( ) => any ) ;
2727 isRequired ?: boolean ;
2828 defaultValue ?: any ;
29- enumValues ?: string [ ] ;
29+ enumValues ?: string [ ] | number [ ] ;
3030 schema ?: Schema ;
3131 _index ?: { [ optionName : string ] : any } ;
3232} ;
@@ -69,7 +69,7 @@ function _getFieldDescription(field: MongooseFieldT): string | undefined {
6969 return undefined ;
7070}
7171
72- function _getFieldEnums ( field : MongooseFieldT ) : string [ ] | undefined {
72+ function _getFieldEnums ( field : MongooseFieldT ) : string [ ] | number [ ] | undefined {
7373 if ( field . enumValues && field . enumValues . length > 0 ) {
7474 return field . enumValues ;
7575 }
@@ -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 ) ;
You can’t perform that action at this time.
0 commit comments