|
| 1 | +/* @flow */ |
| 2 | + |
| 3 | +import elasticsearch from 'elasticsearch'; |
| 4 | +import { TypeComposer, Resolver } from 'graphql-compose'; |
| 5 | +import { GraphQLSchema, GraphQLObjectType } from 'graphql'; |
| 6 | +import { composeWithElastic } from '../../'; |
| 7 | + |
| 8 | +const ELASTICSEARCH_HOST = ''; |
| 9 | +const ELASTICSEARCH_API_VERSION = '5.0'; |
| 10 | +const mapping = { |
| 11 | + properties: { |
| 12 | + id: { |
| 13 | + type: 'keyword', |
| 14 | + }, |
| 15 | + title: { |
| 16 | + type: 'text', |
| 17 | + }, |
| 18 | + description: { |
| 19 | + type: 'text', |
| 20 | + }, |
| 21 | + }, |
| 22 | +}; |
| 23 | + |
| 24 | +const ActivitiesEsTC = composeWithElastic({ |
| 25 | + graphqlTypeName: 'SearchActivities', |
| 26 | + elasticIndex: 'myindex', |
| 27 | + elasticType: 'activities', |
| 28 | + elasticMapping: mapping, |
| 29 | + elasticClient: new elasticsearch.Client({ |
| 30 | + host: ELASTICSEARCH_HOST, |
| 31 | + apiVersion: ELASTICSEARCH_API_VERSION, |
| 32 | + log: 'info', |
| 33 | + }), |
| 34 | +}); |
| 35 | + |
| 36 | +describe('github issue #72 - hits returns me the found id, score, type...', () => { |
| 37 | + it('test `search` resolver', () => { |
| 38 | + expect(ActivitiesEsTC).toBeInstanceOf(TypeComposer); |
| 39 | + |
| 40 | + const resolver = ActivitiesEsTC.getResolver('search'); |
| 41 | + expect(resolver).toBeInstanceOf(Resolver); |
| 42 | + |
| 43 | + const OutputType: any = resolver.getType(); |
| 44 | + const OutputTC = TypeComposer.create(OutputType); |
| 45 | + expect(OutputTC.getFieldNames()).toEqual([ |
| 46 | + 'hits', |
| 47 | + 'count', |
| 48 | + 'aggregations', |
| 49 | + 'max_score', |
| 50 | + 'took', |
| 51 | + 'timed_out', |
| 52 | + '_shards', |
| 53 | + ]); |
| 54 | + |
| 55 | + const HitsTC = OutputTC.getFieldTC('hits'); |
| 56 | + expect(HitsTC).toBeInstanceOf(TypeComposer); |
| 57 | + expect(HitsTC.getFieldNames()).toEqual([ |
| 58 | + '_index', |
| 59 | + '_type', |
| 60 | + '_id', |
| 61 | + '_score', |
| 62 | + '_source', |
| 63 | + '_shard', |
| 64 | + '_node', |
| 65 | + '_explanation', |
| 66 | + '_version', |
| 67 | + 'highlight', |
| 68 | + 'sort', |
| 69 | + 'fields', |
| 70 | + ]); |
| 71 | + |
| 72 | + const SourceTC = HitsTC.getFieldTC('_source'); |
| 73 | + expect(SourceTC).toBeInstanceOf(TypeComposer); |
| 74 | + expect(SourceTC.getFieldNames()).toEqual(['id', 'title', 'description']); |
| 75 | + }); |
| 76 | + |
| 77 | + it('test schema', () => { |
| 78 | + const schema: any = new GraphQLSchema({ |
| 79 | + query: new GraphQLObjectType({ |
| 80 | + name: 'Query', |
| 81 | + fields: { |
| 82 | + searchActivities: ActivitiesEsTC.getResolver('search').getFieldConfig(), |
| 83 | + }, |
| 84 | + }), |
| 85 | + }); |
| 86 | + |
| 87 | + const fc: any = schema._queryType.getFields().searchActivities; |
| 88 | + |
| 89 | + const OutputTC = TypeComposer.create(fc.type); |
| 90 | + expect(OutputTC.getFieldNames()).toEqual([ |
| 91 | + 'hits', |
| 92 | + 'count', |
| 93 | + 'aggregations', |
| 94 | + 'max_score', |
| 95 | + 'took', |
| 96 | + 'timed_out', |
| 97 | + '_shards', |
| 98 | + ]); |
| 99 | + |
| 100 | + const HitsTC = OutputTC.getFieldTC('hits'); |
| 101 | + expect(HitsTC).toBeInstanceOf(TypeComposer); |
| 102 | + expect(HitsTC.getFieldNames()).toEqual([ |
| 103 | + '_index', |
| 104 | + '_type', |
| 105 | + '_id', |
| 106 | + '_score', |
| 107 | + '_source', |
| 108 | + '_shard', |
| 109 | + '_node', |
| 110 | + '_explanation', |
| 111 | + '_version', |
| 112 | + 'highlight', |
| 113 | + 'sort', |
| 114 | + 'fields', |
| 115 | + ]); |
| 116 | + |
| 117 | + const SourceTC = HitsTC.getFieldTC('_source'); |
| 118 | + expect(SourceTC).toBeInstanceOf(TypeComposer); |
| 119 | + expect(SourceTC.getFieldNames()).toEqual(['id', 'title', 'description']); |
| 120 | + }); |
| 121 | +}); |
0 commit comments