11/* eslint-disable no-unused-expressions */
22
33import { expect } from 'chai' ;
4+ import {
5+ GraphQLString ,
6+ GraphQLFloat ,
7+ GraphQLBoolean ,
8+ GraphQLList ,
9+ GraphQLEnumType ,
10+ GraphQLSchema ,
11+ GraphQLObjectType ,
12+ graphql ,
13+ } from 'graphql' ;
14+ import GraphQLJSON from 'graphql-type-json' ;
15+ import {
16+ GraphQLDate ,
17+ GraphQLBuffer ,
18+ GraphQLGeneric ,
19+ } from 'graphql-compose' ;
420import { UserModel } from '../__mocks__/userModel.js' ;
521import {
622 deriveComplexType ,
@@ -12,23 +28,11 @@ import {
1228 embeddedToGraphQL ,
1329 enumToGraphQL ,
1430 documentArrayToGraphQL ,
31+ mixedToGraphQL ,
1532} from '../fieldsConverter' ;
16-
17- import {
18- GraphQLString ,
19- GraphQLFloat ,
20- GraphQLBoolean ,
21- GraphQLList ,
22- GraphQLEnumType ,
23- } from 'graphql' ;
33+ import { composeWithMongoose } from '../composeWithMongoose' ;
2434import GraphQLMongoID from '../types/mongoid' ;
2535
26- import {
27- GraphQLDate ,
28- GraphQLBuffer ,
29- GraphQLGeneric ,
30- } from 'graphql-compose' ;
31-
3236/*
3337Object.prototype.getClassName = function getClassName() {
3438 const funcNameRegex = /function (.{1,})\(/;
@@ -101,6 +105,10 @@ describe('fieldConverter', () => {
101105 expect ( deriveComplexType ( fields . gender ) ) . not . to . equal ( ComplexTypes . SCALAR ) ;
102106 expect ( deriveComplexType ( fields . subDoc ) ) . not . to . equal ( ComplexTypes . SCALAR ) ;
103107 } ) ;
108+
109+ it ( 'schould derive MIXED mongoose type' , ( ) => {
110+ expect ( deriveComplexType ( fields . someDynamic ) ) . to . equal ( ComplexTypes . MIXED ) ;
111+ } ) ;
104112 } ) ;
105113
106114 describe ( 'convertFieldToGraphQL()' , ( ) => {
@@ -191,6 +199,53 @@ describe('fieldConverter', () => {
191199 } ) ;
192200 } ) ;
193201
202+ describe ( 'mixedToGraphQL()' , ( ) => {
203+ let user ;
204+ const UserTC = composeWithMongoose ( UserModel ) ;
205+
206+ before ( 'add test user document to mongoDB' , ( done ) => {
207+ user = new UserModel ( {
208+ name : 'nodkz' ,
209+ someDynamic : {
210+ a : 123 ,
211+ b : [ 1 , 2 ] ,
212+ c : { c : 1 } ,
213+ d : null ,
214+ e : 'str' ,
215+ } ,
216+ } ) ;
217+ user . save ( done ) ;
218+ } ) ;
219+
220+ it ( 'should produce GraphQLJSON' , ( ) => {
221+ const someDynamicType = mixedToGraphQL ( fields . someDynamic ) ;
222+ expect ( someDynamicType ) . to . equal ( GraphQLJSON ) ;
223+ } ) ;
224+
225+ it ( 'should properly return data via graphql query' , async ( ) => {
226+ const schema = new GraphQLSchema ( {
227+ query : new GraphQLObjectType ( {
228+ name : 'Query' ,
229+ fields : {
230+ user : UserTC . getResolver ( 'findById' ) . getFieldConfig ( ) ,
231+ } ,
232+ } ) ,
233+ } ) ;
234+
235+ const query = `{
236+ user(_id: "${ user . _id } ") {
237+ name
238+ someDynamic
239+ }
240+ }` ;
241+ const result = await graphql ( schema , query ) ;
242+ expect ( result ) . deep . property ( 'data.user.name' ) . to . equals ( user . name ) ;
243+ expect ( result )
244+ . deep . property ( 'data.user.someDynamic' )
245+ . deep . equals ( user . someDynamic ) ;
246+ } ) ;
247+ } ) ;
248+
194249 describe ( 'referenceToGraphQL()' , ( ) => {
195250 xit ( '' , ( ) => {
196251
0 commit comments