|
| 1 | +/* @flow */ |
| 2 | + |
| 3 | +import elasticsearch from 'elasticsearch'; |
| 4 | +import { TypeComposer, schemaComposer } from 'graphql-compose'; |
| 5 | +import { graphql } from 'graphql'; |
| 6 | +import { composeWithElastic } from '../..'; |
| 7 | + |
| 8 | +const ELASTICSEARCH_HOST = ''; |
| 9 | +const ELASTICSEARCH_API_VERSION = '5.2'; |
| 10 | +const mapping = { |
| 11 | + properties: { |
| 12 | + id: { |
| 13 | + type: 'keyword', |
| 14 | + }, |
| 15 | + title: { |
| 16 | + type: 'text', |
| 17 | + }, |
| 18 | + tags: { |
| 19 | + type: 'text', |
| 20 | + }, |
| 21 | + }, |
| 22 | +}; |
| 23 | + |
| 24 | +const elasticClient = new elasticsearch.Client({ |
| 25 | + host: ELASTICSEARCH_HOST, |
| 26 | + apiVersion: ELASTICSEARCH_API_VERSION, |
| 27 | + log: 'info', // FOR DETAILED DEBUG USE - 'trace' |
| 28 | +}); |
| 29 | +const elasticIndex = 'github37'; |
| 30 | +const elasticType = 'activities'; |
| 31 | + |
| 32 | +const ActivitiesEsTC = composeWithElastic({ |
| 33 | + graphqlTypeName: 'SearchActivities', |
| 34 | + elasticMapping: mapping, |
| 35 | + pluralFields: ['tags'], |
| 36 | + elasticIndex, |
| 37 | + elasticType, |
| 38 | + elasticClient, |
| 39 | +}); |
| 40 | + |
| 41 | +beforeAll(async () => { |
| 42 | + const indexExists = await elasticClient.indices.exists({ index: elasticIndex }); |
| 43 | + if (indexExists) { |
| 44 | + await elasticClient.indices.delete({ index: elasticIndex }); |
| 45 | + } |
| 46 | + |
| 47 | + // create demo record directly in elastic |
| 48 | + await elasticClient.create({ |
| 49 | + index: elasticIndex, |
| 50 | + type: elasticType, |
| 51 | + id: '333', |
| 52 | + body: { |
| 53 | + title: 'Test 1', |
| 54 | + tags: ['y', 'z'], |
| 55 | + }, |
| 56 | + }); |
| 57 | +}); |
| 58 | + |
| 59 | +describe('github issue #37 - Mutations via updateById overwrite arrays instead of appending to them', () => { |
| 60 | + it('create custom resolver', async () => { |
| 61 | + expect(ActivitiesEsTC).toBeInstanceOf(TypeComposer); |
| 62 | + |
| 63 | + ActivitiesEsTC.addResolver({ |
| 64 | + name: 'addTag', |
| 65 | + kind: 'mutation', |
| 66 | + type: 'JSON', |
| 67 | + args: { |
| 68 | + id: 'String!', |
| 69 | + tag: 'String!', |
| 70 | + }, |
| 71 | + resolve: ({ args }) => { |
| 72 | + return elasticClient.update({ |
| 73 | + index: elasticIndex, |
| 74 | + type: elasticType, |
| 75 | + id: args.id, |
| 76 | + body: { |
| 77 | + script: { |
| 78 | + inline: 'ctx._source.tags.add(params.tag)', |
| 79 | + params: { tag: args.tag }, |
| 80 | + }, |
| 81 | + }, |
| 82 | + }); |
| 83 | + }, |
| 84 | + }); |
| 85 | + |
| 86 | + // create simple Schema for EE test |
| 87 | + schemaComposer.Query.addFields({ noop: 'String' }); // by spec query MUST be always present |
| 88 | + schemaComposer.Mutation.addFields({ |
| 89 | + activitiesAddTag: ActivitiesEsTC.getResolver('addTag'), |
| 90 | + }); |
| 91 | + const schema = schemaComposer.buildSchema(); |
| 92 | + |
| 93 | + // update record via graphql |
| 94 | + const graphqlResponse = await graphql({ |
| 95 | + schema, |
| 96 | + source: ` |
| 97 | + mutation { |
| 98 | + activitiesAddTag(id: "333", tag: "x") |
| 99 | + } |
| 100 | + `, |
| 101 | + }); |
| 102 | + |
| 103 | + // check graphql response |
| 104 | + expect(graphqlResponse).toEqual({ |
| 105 | + data: { |
| 106 | + activitiesAddTag: { |
| 107 | + _id: '333', |
| 108 | + _index: 'github37', |
| 109 | + _shards: { failed: 0, successful: 1, total: 2 }, |
| 110 | + _type: 'activities', |
| 111 | + _version: 2, |
| 112 | + result: 'updated', |
| 113 | + }, |
| 114 | + }, |
| 115 | + }); |
| 116 | + |
| 117 | + // check demo record directly in elastic |
| 118 | + const elasticData = await elasticClient.get({ |
| 119 | + index: elasticIndex, |
| 120 | + type: elasticType, |
| 121 | + id: '333', |
| 122 | + _source: true, |
| 123 | + }); |
| 124 | + |
| 125 | + expect(elasticData).toEqual({ |
| 126 | + _id: '333', |
| 127 | + _index: 'github37', |
| 128 | + _source: { tags: ['y', 'z', 'x'], title: 'Test 1' }, |
| 129 | + _type: 'activities', |
| 130 | + _version: 2, |
| 131 | + found: true, |
| 132 | + }); |
| 133 | + }); |
| 134 | +}); |
0 commit comments