Skip to content

Commit

Permalink
fix(mappingConverter): Replace invalid GraphQL characters on underscore.
Browse files Browse the repository at this point in the history
Related #9
  • Loading branch information
nodkz committed Aug 21, 2017
1 parent e59b7bf commit fea7737
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 15 deletions.
63 changes: 62 additions & 1 deletion src/__tests__/mappingConverter-test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* @flow */

import { TypeComposer, GraphQLJSON, graphql } from 'graphql-compose';
import { TypeComposer, GraphQLJSON, graphql, GQC } from 'graphql-compose';
import {
convertToSourceTC,
propertyToSourceGraphQLType,
Expand Down Expand Up @@ -225,4 +225,65 @@ describe('PropertiesConverter', () => {
expect(getSubFields('range')).toEqual([]);
});
});

describe('issue #9', () => {
const mapping9 = {
properties: {
$id: {
type: 'long',
},
lastName: {
type: 'string',
},
email: {
type: 'string',
analyzer: 'email_analyzer',
},
$passwordHash: {
type: 'string',
index: 'not_analyzed',
},
},
};
const tc9 = convertToSourceTC(mapping9, 'Type9');

it('should replace unacceptable characters in GraphQL fieldnames', () => {
expect(tc9).toBeInstanceOf(TypeComposer);
expect(tc9.getFieldNames()).toEqual(
expect.arrayContaining(['_id', 'lastName', 'email', '_passwordHash'])
);
});

it('should work with graphql schema without errors', () => {
GQC.rootQuery().addFields({ userES: tc9 });
expect(() => GQC.buildSchema()).not.toThrowError();
});

it('should use Elastic field names from source', async () => {
GQC.rootQuery().addFields({ userES: tc9 });
const result = await graphql.graphql(
GQC.buildSchema(),
`query { userES { _id, lastName, email, _passwordHash } }`,
{
// simulate elastic responce
userES: {
$id: 123,
lastName: 'Tyler',
email: '[email protected]',
$passwordHash: 'abc1234def',
},
}
);
expect(result).toEqual({
data: {
userES: {
_id: 123,
lastName: 'Tyler',
email: '[email protected]',
_passwordHash: 'abc1234def',
},
},
});
});
});
});
33 changes: 19 additions & 14 deletions src/mappingConverter.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,30 +107,35 @@ export function convertToSourceTC(
const fields = {};
const pluralFields = opts.pluralFields || [];

Object.keys(properties).forEach(name => {
const gqType = propertyToSourceGraphQLType(properties[name], `${typeName}${upperFirst(name)}`, {
...opts,
pluralFields: getSubFields(name, pluralFields),
});
Object.keys(properties).forEach(sourceName => {
const fieldName = sourceName.replace(/[^_a-zA-Z0-9]/g, '_');
const gqType = propertyToSourceGraphQLType(
properties[sourceName],
`${typeName}${upperFirst(fieldName)}`,
{
...opts,
pluralFields: getSubFields(sourceName, pluralFields),
}
);
if (gqType) {
if (pluralFields.indexOf(name) >= 0) {
fields[name] = {
if (pluralFields.indexOf(sourceName) >= 0) {
fields[fieldName] = {
type: new GraphQLList(gqType),
resolve: source => {
if (Array.isArray(source[name])) {
return source[name];
if (Array.isArray(source[sourceName])) {
return source[sourceName];
}
return [source[name]];
return [source[sourceName]];
},
};
} else {
fields[name] = {
fields[fieldName] = {
type: gqType,
resolve: source => {
if (Array.isArray(source[name])) {
return source[name][0];
if (Array.isArray(source[sourceName])) {
return source[sourceName][0];
}
return source[name];
return source[sourceName];
},
};
}
Expand Down

0 comments on commit fea7737

Please sign in to comment.