Skip to content

Commit 6d6d8f8

Browse files
iamakulovnodkz
authored andcommitted
fix: for arrays of objects, use all fields when calculating types
1 parent 6ba50b2 commit 6d6d8f8

File tree

3 files changed

+28
-7
lines changed

3 files changed

+28
-7
lines changed

src/ObjectParser.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,12 @@ export default class ObjectParser {
4040
if (value === null) return 'JSON';
4141

4242
if (Array.isArray(value)) {
43-
const val = value[0];
44-
if (Array.isArray(val)) return ['JSON'];
43+
if (Array.isArray(value[0])) return ['JSON'];
44+
45+
const val =
46+
typeof value[0] === 'object' && value[0] !== null
47+
? Object.assign({}, ...value)
48+
: value[0];
4549

4650
const args =
4751
opts && opts.typeName && opts.fieldName

src/__tests__/ObjectParser-test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ describe('ObjectParser', () => {
4545
typeName: 'ParentTypeName',
4646
fieldName: 'subDocument',
4747
});
48-
expect(spy).toHaveBeenCalledWith('ParentTypeName_SubDocument', valueAsArrayOfObjects[0]);
48+
expect(spy).toHaveBeenCalledWith('ParentTypeName_SubDocument', { a: 456 });
4949
});
5050

5151
it('of any', () => {

src/__tests__/composeWithJson-test.js

+21-4
Original file line numberDiff line numberDiff line change
@@ -213,13 +213,24 @@ describe('composeWithJson', () => {
213213
name: 'Luke Skywalker',
214214
limbs: [
215215
{ kind: 'arm', position: 'left', length: 76 },
216-
{ kind: 'arm', position: 'left', length: 76 },
217-
{ kind: 'leg', position: 'left', length: 81 },
218-
{ kind: 'leg', position: 'right', length: 82 },
216+
{ kind: 'arm', position: 'right', length: 76, ring: true },
217+
{ kind: 'leg', position: 'left', length: 81, sock: 'red' },
218+
{ kind: 'leg', position: 'right', length: 82, sock: 'red' },
219219
],
220220
};
221221

222222
const PersonTC = composeWithJson('PersonCustom', restApiResponse);
223+
expect(
224+
PersonTC.getFieldTC('limbs')
225+
.getFieldTC('ring')
226+
.getTypeName()
227+
).toEqual('Boolean');
228+
expect(
229+
PersonTC.getFieldTC('limbs')
230+
.getFieldTC('sock')
231+
.getTypeName()
232+
).toEqual('String');
233+
223234
const schema1 = new GraphQLSchema({
224235
query: new GraphQLObjectType({
225236
name: 'Query',
@@ -241,6 +252,7 @@ describe('composeWithJson', () => {
241252
name
242253
limbs {
243254
length
255+
ring
244256
}
245257
}
246258
}`
@@ -250,7 +262,12 @@ describe('composeWithJson', () => {
250262
data: {
251263
person: {
252264
name: 'Luke Skywalker',
253-
limbs: [{ length: 76 }, { length: 76 }, { length: 81 }, { length: 82 }],
265+
limbs: [
266+
{ length: 76, ring: null },
267+
{ length: 76, ring: true },
268+
{ length: 81, ring: null },
269+
{ length: 82, ring: null },
270+
],
254271
},
255272
},
256273
});

0 commit comments

Comments
 (0)