Skip to content

Commit 6ba50b2

Browse files
iamakulovnodkz
authored andcommitted
feat: add support for nested arrays of objects
* Add support for nested arrays of objects * Fix `yarn test` emitting syntax errors locally (Due to babel transforms not being applied) * Use a better type name for nested array members * Update tests
1 parent 84da761 commit 6ba50b2

File tree

5 files changed

+78
-3
lines changed

5 files changed

+78
-3
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
"babel-core": "^7.0.0-bridge.0",
3838
"babel-eslint": "^10.0.1",
3939
"babel-jest": "^24.8.0",
40+
"cross-env": "^5.2.0",
4041
"eslint": "^5.16.0",
4142
"eslint-config-airbnb-base": "^13.1.0",
4243
"eslint-config-prettier": "^4.3.0",
@@ -64,7 +65,7 @@
6465
"coverage": "jest --coverage --maxWorkers 2",
6566
"lint": "eslint --ext .js ./src",
6667
"flow": "./node_modules/.bin/flow",
67-
"test": "npm run coverage && npm run lint && npm run flow",
68+
"test": "cross-env NODE_ENV=test npm run coverage && npm run lint && npm run flow",
6869
"semantic-release": "semantic-release",
6970
"fixture-demo": "./node_modules/.bin/babel-node ./src/__fixtures__/app.js"
7071
}

src/ObjectParser.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,15 @@ export default class ObjectParser {
4242
if (Array.isArray(value)) {
4343
const val = value[0];
4444
if (Array.isArray(val)) return ['JSON'];
45-
return [(this.getFieldConfig(val): any)];
45+
46+
const args =
47+
opts && opts.typeName && opts.fieldName
48+
? {
49+
typeName: opts.typeName,
50+
fieldName: opts.fieldName,
51+
}
52+
: {};
53+
return [(this.getFieldConfig(val, args): any)];
4654
}
4755

4856
if (opts && opts.typeName && opts.fieldName) {

src/__tests__/ObjectParser-test.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,16 @@ describe('ObjectParser', () => {
3838
expect(OP.getFieldConfig([false, true])).toEqual(['Boolean']);
3939
});
4040

41+
it('of object', () => {
42+
const spy = jest.spyOn(OP, 'createTC');
43+
const valueAsArrayOfObjects = [{ a: 123 }, { a: 456 }];
44+
OP.getFieldConfig(valueAsArrayOfObjects, {
45+
typeName: 'ParentTypeName',
46+
fieldName: 'subDocument',
47+
});
48+
expect(spy).toHaveBeenCalledWith('ParentTypeName_SubDocument', valueAsArrayOfObjects[0]);
49+
});
50+
4151
it('of any', () => {
4252
expect(OP.getFieldConfig([null])).toEqual(['JSON']);
4353
});

src/__tests__/composeWithJson-test.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,4 +207,52 @@ describe('composeWithJson', () => {
207207
},
208208
});
209209
});
210+
211+
it('check array of swallow objects', async () => {
212+
const restApiResponse = {
213+
name: 'Luke Skywalker',
214+
limbs: [
215+
{ 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 },
219+
],
220+
};
221+
222+
const PersonTC = composeWithJson('PersonCustom', restApiResponse);
223+
const schema1 = new GraphQLSchema({
224+
query: new GraphQLObjectType({
225+
name: 'Query',
226+
fields: {
227+
person: {
228+
type: PersonTC.getType(),
229+
resolve: () => {
230+
return restApiResponse;
231+
},
232+
},
233+
},
234+
}),
235+
});
236+
237+
const res = await graphql.graphql(
238+
schema1,
239+
`{
240+
person {
241+
name
242+
limbs {
243+
length
244+
}
245+
}
246+
}`
247+
);
248+
249+
expect(res).toEqual({
250+
data: {
251+
person: {
252+
name: 'Luke Skywalker',
253+
limbs: [{ length: 76 }, { length: 76 }, { length: 81 }, { length: 82 }],
254+
},
255+
},
256+
});
257+
});
210258
});

yarn.lock

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2388,6 +2388,14 @@ create-error-class@^3.0.0:
23882388
dependencies:
23892389
capture-stack-trace "^1.0.0"
23902390

2391+
cross-env@^5.2.0:
2392+
version "5.2.0"
2393+
resolved "https://registry.yarnpkg.com/cross-env/-/cross-env-5.2.0.tgz#6ecd4c015d5773e614039ee529076669b9d126f2"
2394+
integrity sha512-jtdNFfFW1hB7sMhr/H6rW1Z45LFqyI431m3qU6bFXcQ3Eh7LtBuG3h74o7ohHZ3crrRkkqHlo4jYHFPcjroANg==
2395+
dependencies:
2396+
cross-spawn "^6.0.5"
2397+
is-windows "^1.0.0"
2398+
23912399
cross-spawn@^5.0.1:
23922400
version "5.1.0"
23932401
resolved "https://registry.yarnpkg.com/cross-spawn/-/cross-spawn-5.1.0.tgz#e8bd0efee58fcff6f8f94510a0a554bbfa235449"
@@ -4168,7 +4176,7 @@ is-typedarray@~1.0.0:
41684176
version "1.0.0"
41694177
resolved "https://registry.yarnpkg.com/is-typedarray/-/is-typedarray-1.0.0.tgz#e479c80858df0c1b11ddda6940f96011fcda4a9a"
41704178

4171-
is-windows@^1.0.2:
4179+
is-windows@^1.0.0, is-windows@^1.0.2:
41724180
version "1.0.2"
41734181
resolved "https://registry.yarnpkg.com/is-windows/-/is-windows-1.0.2.tgz#d1850eb9791ecd18e6182ce12a30f396634bb19d"
41744182

0 commit comments

Comments
 (0)