Skip to content

Commit fea7737

Browse files
committed
fix(mappingConverter): Replace invalid GraphQL characters on underscore.
Related #9
1 parent e59b7bf commit fea7737

File tree

2 files changed

+81
-15
lines changed

2 files changed

+81
-15
lines changed

src/__tests__/mappingConverter-test.js

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

3-
import { TypeComposer, GraphQLJSON, graphql } from 'graphql-compose';
3+
import { TypeComposer, GraphQLJSON, graphql, GQC } from 'graphql-compose';
44
import {
55
convertToSourceTC,
66
propertyToSourceGraphQLType,
@@ -225,4 +225,65 @@ describe('PropertiesConverter', () => {
225225
expect(getSubFields('range')).toEqual([]);
226226
});
227227
});
228+
229+
describe('issue #9', () => {
230+
const mapping9 = {
231+
properties: {
232+
$id: {
233+
type: 'long',
234+
},
235+
lastName: {
236+
type: 'string',
237+
},
238+
email: {
239+
type: 'string',
240+
analyzer: 'email_analyzer',
241+
},
242+
$passwordHash: {
243+
type: 'string',
244+
index: 'not_analyzed',
245+
},
246+
},
247+
};
248+
const tc9 = convertToSourceTC(mapping9, 'Type9');
249+
250+
it('should replace unacceptable characters in GraphQL fieldnames', () => {
251+
expect(tc9).toBeInstanceOf(TypeComposer);
252+
expect(tc9.getFieldNames()).toEqual(
253+
expect.arrayContaining(['_id', 'lastName', 'email', '_passwordHash'])
254+
);
255+
});
256+
257+
it('should work with graphql schema without errors', () => {
258+
GQC.rootQuery().addFields({ userES: tc9 });
259+
expect(() => GQC.buildSchema()).not.toThrowError();
260+
});
261+
262+
it('should use Elastic field names from source', async () => {
263+
GQC.rootQuery().addFields({ userES: tc9 });
264+
const result = await graphql.graphql(
265+
GQC.buildSchema(),
266+
`query { userES { _id, lastName, email, _passwordHash } }`,
267+
{
268+
// simulate elastic responce
269+
userES: {
270+
$id: 123,
271+
lastName: 'Tyler',
272+
273+
$passwordHash: 'abc1234def',
274+
},
275+
}
276+
);
277+
expect(result).toEqual({
278+
data: {
279+
userES: {
280+
_id: 123,
281+
lastName: 'Tyler',
282+
283+
_passwordHash: 'abc1234def',
284+
},
285+
},
286+
});
287+
});
288+
});
228289
});

src/mappingConverter.js

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -107,30 +107,35 @@ export function convertToSourceTC(
107107
const fields = {};
108108
const pluralFields = opts.pluralFields || [];
109109

110-
Object.keys(properties).forEach(name => {
111-
const gqType = propertyToSourceGraphQLType(properties[name], `${typeName}${upperFirst(name)}`, {
112-
...opts,
113-
pluralFields: getSubFields(name, pluralFields),
114-
});
110+
Object.keys(properties).forEach(sourceName => {
111+
const fieldName = sourceName.replace(/[^_a-zA-Z0-9]/g, '_');
112+
const gqType = propertyToSourceGraphQLType(
113+
properties[sourceName],
114+
`${typeName}${upperFirst(fieldName)}`,
115+
{
116+
...opts,
117+
pluralFields: getSubFields(sourceName, pluralFields),
118+
}
119+
);
115120
if (gqType) {
116-
if (pluralFields.indexOf(name) >= 0) {
117-
fields[name] = {
121+
if (pluralFields.indexOf(sourceName) >= 0) {
122+
fields[fieldName] = {
118123
type: new GraphQLList(gqType),
119124
resolve: source => {
120-
if (Array.isArray(source[name])) {
121-
return source[name];
125+
if (Array.isArray(source[sourceName])) {
126+
return source[sourceName];
122127
}
123-
return [source[name]];
128+
return [source[sourceName]];
124129
},
125130
};
126131
} else {
127-
fields[name] = {
132+
fields[fieldName] = {
128133
type: gqType,
129134
resolve: source => {
130-
if (Array.isArray(source[name])) {
131-
return source[name][0];
135+
if (Array.isArray(source[sourceName])) {
136+
return source[sourceName][0];
132137
}
133-
return source[name];
138+
return source[sourceName];
134139
},
135140
};
136141
}

0 commit comments

Comments
 (0)