Skip to content

Commit e6c6448

Browse files
committed
feat: Exposed main methods, options renaming, small clean-ups.
1 parent 9fc2150 commit e6c6448

File tree

7 files changed

+184
-320
lines changed

7 files changed

+184
-320
lines changed

src/__tests__/mappingConverter-test.js

Lines changed: 21 additions & 173 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* @flow */
22

3-
import { TypeComposer, InputTypeComposer, GraphQLJSON } from 'graphql-compose';
3+
import { TypeComposer, GraphQLJSON } from 'graphql-compose';
44

55
import {
66
GraphQLString,
@@ -14,10 +14,7 @@ import {
1414
import {
1515
convertToSourceTC,
1616
propertyToSourceGraphQLType,
17-
convertToAggregatableITC,
1817
inputPropertiesToGraphQLTypes,
19-
convertToSearchableITC,
20-
convertToAnalyzedITC,
2118
getSubFields,
2219
} from '../mappingConverter';
2320

@@ -173,7 +170,7 @@ describe('PropertiesConverter', () => {
173170
describe('inputPropertiesToGraphQLTypes()', () => {
174171
it('should throw error on incorrect prop', () => {
175172
expect(() => {
176-
inputPropertiesToGraphQLTypes({}, () => true);
173+
inputPropertiesToGraphQLTypes({});
177174
}).toThrowError('incorrect Elastic property config');
178175
});
179176

@@ -182,35 +179,31 @@ describe('PropertiesConverter', () => {
182179
{
183180
type: 'text',
184181
},
185-
() => true,
186182
'lastname'
187183
);
188184
expect(fields._all.lastname).toEqual(GraphQLString);
189185
expect(fields.text.lastname).toEqual(GraphQLString);
190186
});
191187

192188
it('should accept mapping', () => {
193-
const fields = inputPropertiesToGraphQLTypes(mapping, () => true);
189+
const fields = inputPropertiesToGraphQLTypes(mapping);
194190
expect(Object.keys(fields._all).length).toBeGreaterThan(2);
195191
});
196192

197193
it('should convert nested fields', () => {
198-
const fields = inputPropertiesToGraphQLTypes(
199-
{
200-
properties: {
201-
name: {
202-
type: 'text',
203-
fields: {
204-
keyword: {
205-
type: 'keyword',
206-
ignore_above: 256,
207-
},
194+
const fields = inputPropertiesToGraphQLTypes({
195+
properties: {
196+
name: {
197+
type: 'text',
198+
fields: {
199+
keyword: {
200+
type: 'keyword',
201+
ignore_above: 256,
208202
},
209203
},
210204
},
211205
},
212-
() => true
213-
);
206+
});
214207
expect(Object.keys(fields._all).length).toEqual(2);
215208
expect(Object.keys(fields._all)).toEqual(
216209
expect.arrayContaining(['name', 'name__keyword'])
@@ -223,53 +216,18 @@ describe('PropertiesConverter', () => {
223216
);
224217
});
225218

226-
it('should use filterFn', () => {
227-
const fields = inputPropertiesToGraphQLTypes(
228-
{
229-
properties: {
230-
name: {
231-
type: 'text',
232-
fields: {
233-
keyword: {
234-
type: 'keyword',
235-
ignore_above: 256,
236-
},
237-
},
238-
},
239-
},
240-
},
241-
prop => prop.type !== 'text'
242-
);
243-
expect(Object.keys(fields._all).length).toEqual(1);
244-
expect(Object.keys(fields._all)).toEqual(
245-
expect.arrayContaining(['name__keyword'])
246-
);
247-
expect(Object.keys(fields.keyword).length).toEqual(1);
248-
expect(Object.keys(fields.keyword)).toEqual(
249-
expect.arrayContaining(['name__keyword'])
250-
);
251-
});
252-
253219
it('should not return index:false fields', () => {
254-
const itc = convertToSearchableITC(mapping, 'SearchInput');
255-
expect(itc.getFieldNames()).not.toEqual(
256-
expect.arrayContaining(['noIndex'])
257-
);
258-
259-
const fields = inputPropertiesToGraphQLTypes(
260-
{
261-
properties: {
262-
name: {
263-
type: 'text',
264-
index: false,
265-
},
266-
date: {
267-
type: 'date',
268-
},
220+
const fields = inputPropertiesToGraphQLTypes({
221+
properties: {
222+
name: {
223+
type: 'text',
224+
index: false,
225+
},
226+
date: {
227+
type: 'date',
269228
},
270229
},
271-
prop => prop.type !== 'text'
272-
);
230+
});
273231
expect(Object.keys(fields._all).length).toEqual(1);
274232
expect(Object.keys(fields._all)).toEqual(
275233
expect.arrayContaining(['date'])
@@ -281,116 +239,6 @@ describe('PropertiesConverter', () => {
281239
});
282240
});
283241

284-
describe('convertToAggregatableITC()', () => {
285-
it('should throw error on empty mapping', () => {
286-
// $FlowFixMe
287-
expect(() => convertToAggregatableITC()).toThrowError(
288-
'incorrect mapping'
289-
);
290-
});
291-
292-
it('should throw error on empty typeName', () => {
293-
expect(() => {
294-
// $FlowFixMe
295-
convertToAggregatableITC(mapping);
296-
}).toThrowError('empty name');
297-
});
298-
299-
it('should return InputTypeComposer', () => {
300-
const itc = convertToAggregatableITC(mapping, 'AggsInput');
301-
expect(itc).toBeInstanceOf(InputTypeComposer);
302-
expect(itc.getTypeName()).toBe('AggsInput');
303-
expect(itc.getFieldNames().length).toBeGreaterThan(1);
304-
});
305-
306-
it('should return array of aggregatable fields', () => {
307-
const itc = convertToAggregatableITC(mapping, 'AggsInput');
308-
expect(itc.getFieldNames()).toEqual(
309-
expect.arrayContaining([
310-
'name__keyword',
311-
'birthday',
312-
'avatarUrl__thumb__keyword',
313-
])
314-
);
315-
});
316-
317-
it('should not return text fields', () => {
318-
const itc = convertToAggregatableITC(mapping, 'AggsInput');
319-
expect(itc.getFieldNames()).not.toEqual(
320-
expect.arrayContaining(['lastname'])
321-
);
322-
});
323-
});
324-
325-
describe('convertToSearchableITC()', () => {
326-
it('should throw error on empty mapping', () => {
327-
// $FlowFixMe
328-
expect(() => convertToSearchableITC()).toThrowError('incorrect mapping');
329-
});
330-
331-
it('should throw error on empty typeName', () => {
332-
expect(() => {
333-
// $FlowFixMe
334-
convertToSearchableITC(mapping);
335-
}).toThrowError('empty name');
336-
});
337-
338-
it('should return InputTypeComposer', () => {
339-
const itc = convertToSearchableITC(mapping, 'SearchInput');
340-
expect(itc).toBeInstanceOf(InputTypeComposer);
341-
expect(itc.getTypeName()).toBe('SearchInput');
342-
expect(itc.getFieldNames().length).toBeGreaterThan(1);
343-
});
344-
345-
it('should return array of searchable fields', () => {
346-
const itc = convertToSearchableITC(mapping, 'SearchInput');
347-
expect(itc.getFieldNames()).toEqual(
348-
expect.arrayContaining([
349-
'name__keyword',
350-
'name',
351-
'avatarUrl__big',
352-
'avatarUrl__thumb__keyword',
353-
'avatarUrl__thumb',
354-
'lastname',
355-
'birthday',
356-
])
357-
);
358-
});
359-
});
360-
361-
describe('convertToAnalyzedITC()', () => {
362-
it('should throw error on empty mapping', () => {
363-
// $FlowFixMe
364-
expect(() => convertToAnalyzedITC()).toThrowError('incorrect mapping');
365-
});
366-
367-
it('should throw error on empty typeName', () => {
368-
expect(() => {
369-
// $FlowFixMe
370-
convertToAnalyzedITC(mapping);
371-
}).toThrowError('empty name');
372-
});
373-
374-
it('should return InputTypeComposer', () => {
375-
const itc = convertToAnalyzedITC(mapping, 'AnalyzedInput');
376-
expect(itc).toBeInstanceOf(InputTypeComposer);
377-
expect(itc.getTypeName()).toBe('AnalyzedInput');
378-
expect(itc.getFieldNames().length).toBeGreaterThan(1);
379-
});
380-
381-
it('should return array of searchable fields', () => {
382-
const itc = convertToAnalyzedITC(mapping, 'AnalyzedInput');
383-
expect(itc.getFieldNames()).toEqual(
384-
expect.arrayContaining([
385-
'name',
386-
'avatarUrl__big',
387-
'avatarUrl__thumb',
388-
'lastname',
389-
])
390-
);
391-
});
392-
});
393-
394242
describe('getSubFields()', () => {
395243
it('should return array of sub fields', () => {
396244
expect(

src/composeWithElastic.js

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,36 +4,69 @@ import {
44
inputPropertiesToGraphQLTypes,
55
} from './mappingConverter';
66
import type { ElasticMappingT } from './mappingConverter';
7+
import createSearchResolver from './resolvers/search';
8+
import createSearchConnectionResolver from './resolvers/searchConnection';
79

810
export type composeWithElasticOptsT = {
9-
typeName: string,
11+
graphqlTypeName: string,
12+
elasticIndex: string,
13+
elasticType: string,
14+
elasticMapping: ElasticMappingT,
15+
elasticClient: Object,
1016
pluralFields?: string[],
1117
};
1218

1319
export function composeWithElastic(
14-
mapping: ElasticMappingT,
1520
opts: composeWithElasticOptsT = {}
1621
): TypeComposer {
1722
if (!opts) {
1823
throw new Error('Opts is required argument for composeWithElastic()');
1924
}
2025

21-
if (typeof opts.typeName !== 'string' || !opts.typeName) {
26+
if (!opts.elasticMapping || !opts.elasticMapping.properties) {
2227
throw new Error(
23-
'Opts.typeName is required option for TypeName in composeWithElastic()'
28+
'You provide incorrect elasticMapping property. It should be an object `{ properties: {} }`'
2429
);
2530
}
2631

32+
if (!opts.elasticIndex || typeof opts.elasticIndex !== 'string') {
33+
throw new Error(
34+
'Third arg for Resolver search() should contain `elasticIndex` string property from your Elastic server.'
35+
);
36+
}
37+
38+
if (!opts.elasticType || typeof opts.elasticType !== 'string') {
39+
throw new Error(
40+
'Third arg for Resolver search() should contain `elasticType` string property from your Elastic server.'
41+
);
42+
}
43+
44+
if (typeof opts.graphqlTypeName !== 'string' || !opts.graphqlTypeName) {
45+
throw new Error(
46+
'Opts.graphqlTypeName is required property for generated GraphQL Type name in composeWithElastic()'
47+
);
48+
}
49+
opts.prefix = opts.graphqlTypeName; // eslint-disable-line
50+
2751
if (opts.pluralFields && !Array.isArray(opts.pluralFields)) {
2852
throw new Error(
2953
'Opts.pluralFields should be an Array of strings with field names ' +
3054
'which are plural (you may use dot notation for nested fields).'
3155
);
3256
}
3357

34-
const tc = convertToSourceTC(mapping, opts.typeName, opts);
58+
const fieldMap = inputPropertiesToGraphQLTypes(opts.elasticMapping);
59+
const sourceTC = convertToSourceTC(
60+
opts.elasticMapping,
61+
opts.graphqlTypeName,
62+
opts
63+
);
64+
65+
const searchR = createSearchResolver(fieldMap, sourceTC, opts);
66+
const searchConnectionR = createSearchConnectionResolver(searchR, opts);
3567

36-
const propsMap = inputPropertiesToGraphQLTypes(mapping);
68+
sourceTC.addResolver(searchR);
69+
sourceTC.addResolver(searchConnectionR);
3770

38-
return tc;
71+
return sourceTC;
3972
}

0 commit comments

Comments
 (0)