Skip to content

Commit

Permalink
fix: for arrays of objects, use all fields when calculating types
Browse files Browse the repository at this point in the history
  • Loading branch information
iamakulov authored and nodkz committed Aug 21, 2019
1 parent 6ba50b2 commit 6d6d8f8
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 7 deletions.
8 changes: 6 additions & 2 deletions src/ObjectParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,12 @@ export default class ObjectParser {
if (value === null) return 'JSON';

if (Array.isArray(value)) {
const val = value[0];
if (Array.isArray(val)) return ['JSON'];
if (Array.isArray(value[0])) return ['JSON'];

const val =
typeof value[0] === 'object' && value[0] !== null
? Object.assign({}, ...value)
: value[0];

const args =
opts && opts.typeName && opts.fieldName
Expand Down
2 changes: 1 addition & 1 deletion src/__tests__/ObjectParser-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ describe('ObjectParser', () => {
typeName: 'ParentTypeName',
fieldName: 'subDocument',
});
expect(spy).toHaveBeenCalledWith('ParentTypeName_SubDocument', valueAsArrayOfObjects[0]);
expect(spy).toHaveBeenCalledWith('ParentTypeName_SubDocument', { a: 456 });
});

it('of any', () => {
Expand Down
25 changes: 21 additions & 4 deletions src/__tests__/composeWithJson-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -213,13 +213,24 @@ describe('composeWithJson', () => {
name: 'Luke Skywalker',
limbs: [
{ kind: 'arm', position: 'left', length: 76 },
{ kind: 'arm', position: 'left', length: 76 },
{ kind: 'leg', position: 'left', length: 81 },
{ kind: 'leg', position: 'right', length: 82 },
{ kind: 'arm', position: 'right', length: 76, ring: true },
{ kind: 'leg', position: 'left', length: 81, sock: 'red' },
{ kind: 'leg', position: 'right', length: 82, sock: 'red' },
],
};

const PersonTC = composeWithJson('PersonCustom', restApiResponse);
expect(
PersonTC.getFieldTC('limbs')
.getFieldTC('ring')
.getTypeName()
).toEqual('Boolean');
expect(
PersonTC.getFieldTC('limbs')
.getFieldTC('sock')
.getTypeName()
).toEqual('String');

const schema1 = new GraphQLSchema({
query: new GraphQLObjectType({
name: 'Query',
Expand All @@ -241,6 +252,7 @@ describe('composeWithJson', () => {
name
limbs {
length
ring
}
}
}`
Expand All @@ -250,7 +262,12 @@ describe('composeWithJson', () => {
data: {
person: {
name: 'Luke Skywalker',
limbs: [{ length: 76 }, { length: 76 }, { length: 81 }, { length: 82 }],
limbs: [
{ length: 76, ring: null },
{ length: 76, ring: true },
{ length: 81, ring: null },
{ length: 82, ring: null },
],
},
},
});
Expand Down

0 comments on commit 6d6d8f8

Please sign in to comment.