|
| 1 | +import { SchemaComposer, graphql, EnumTypeComposer } from 'graphql-compose'; |
| 2 | +import { composeMongoose } from '../../index'; |
| 3 | +import { mongoose } from '../../__mocks__/mongooseCommon'; |
| 4 | +import { Schema } from 'mongoose'; |
| 5 | + |
| 6 | +const schemaComposer = new SchemaComposer<{ req: any }>(); |
| 7 | + |
| 8 | +// mongoose.set('debug', true); |
| 9 | + |
| 10 | +const ArticleSchema = new Schema( |
| 11 | + { |
| 12 | + name: { |
| 13 | + type: String, |
| 14 | + index: true, |
| 15 | + }, |
| 16 | + label: { |
| 17 | + type: String, |
| 18 | + enum: ['', null, 'val1'], |
| 19 | + }, |
| 20 | + }, |
| 21 | + { |
| 22 | + collection: 'article', |
| 23 | + } |
| 24 | +); |
| 25 | +const ArticleModel = mongoose.model<any>('Article', ArticleSchema); |
| 26 | +const ArticleTC = composeMongoose(ArticleModel, { schemaComposer }); |
| 27 | + |
| 28 | +let lastResolverInputArg = [] as any; |
| 29 | +const enumTC = ArticleTC.getFieldTC('label') as EnumTypeComposer; |
| 30 | +schemaComposer.Query.addFields({ |
| 31 | + articles: ArticleTC.mongooseResolvers.findMany(), |
| 32 | + test: { |
| 33 | + type: enumTC.List, |
| 34 | + args: { input: enumTC.List }, |
| 35 | + resolve: (_, { input }) => { |
| 36 | + lastResolverInputArg = [...input]; |
| 37 | + return input; |
| 38 | + }, |
| 39 | + }, |
| 40 | +}); |
| 41 | + |
| 42 | +const schema = schemaComposer.buildSchema(); |
| 43 | + |
| 44 | +beforeAll(async () => { |
| 45 | + await ArticleModel.base.createConnection(); |
| 46 | + await ArticleModel.create([ |
| 47 | + { name: 'A1', label: null }, |
| 48 | + { name: 'A2', label: '' }, |
| 49 | + { name: 'A3', label: 'val1' }, |
| 50 | + ]); |
| 51 | +}); |
| 52 | +afterAll(() => { |
| 53 | + ArticleModel.base.disconnect(); |
| 54 | +}); |
| 55 | + |
| 56 | +describe('issue #384 - New feature Request: To allow null, string in Enum', () => { |
| 57 | + it('check SDL', async () => { |
| 58 | + expect(ArticleTC.toSDL({ omitDescriptions: true, deep: true, omitScalars: true })) |
| 59 | + .toMatchInlineSnapshot(` |
| 60 | +"type Article { |
| 61 | + name: String |
| 62 | + label: EnumArticleLabel |
| 63 | + _id: MongoID! |
| 64 | +} |
| 65 | +
|
| 66 | +enum EnumArticleLabel { |
| 67 | + EMPTY_STRING |
| 68 | + NULL |
| 69 | + val1 |
| 70 | +}" |
| 71 | +`); |
| 72 | + }); |
| 73 | + |
| 74 | + it('check runtime output', async () => { |
| 75 | + const result = await graphql.graphql({ |
| 76 | + schema, |
| 77 | + contextValue: {}, |
| 78 | + source: ` |
| 79 | + { |
| 80 | + articles(sort: NAME_ASC) { |
| 81 | + name |
| 82 | + label |
| 83 | + } |
| 84 | + } |
| 85 | + `, |
| 86 | + }); |
| 87 | + expect(result).toEqual({ |
| 88 | + data: { |
| 89 | + articles: [ |
| 90 | + { label: null, name: 'A1' }, // <-- special `null` case. It cannot be converted to NULL string |
| 91 | + { label: 'EMPTY_STRING', name: 'A2' }, // <-- has correct ENUM key |
| 92 | + { label: 'val1', name: 'A3' }, |
| 93 | + ], |
| 94 | + }, |
| 95 | + }); |
| 96 | + }); |
| 97 | + |
| 98 | + it('check runtime input', async () => { |
| 99 | + const result = await graphql.graphql({ |
| 100 | + schema, |
| 101 | + contextValue: {}, |
| 102 | + source: ` |
| 103 | + { |
| 104 | + test(input: [val1, NULL, EMPTY_STRING]) |
| 105 | + } |
| 106 | + `, |
| 107 | + }); |
| 108 | + |
| 109 | + // inside resolvers should be provided real values for null & string |
| 110 | + expect(lastResolverInputArg).toEqual(['val1', null, '']); |
| 111 | + |
| 112 | + // in output JSON should be provided keys |
| 113 | + // BUT be aware `null` does not converted back to `NULL` string |
| 114 | + expect(result).toEqual({ data: { test: ['val1', null, 'EMPTY_STRING'] } }); |
| 115 | + }); |
| 116 | +}); |
0 commit comments