Skip to content
This repository was archived by the owner on Oct 6, 2022. It is now read-only.

Commit 363563e

Browse files
committed
Fix for when there's multiple examples for scalar field
1 parent f96a96c commit 363563e

File tree

3 files changed

+23
-22
lines changed

3 files changed

+23
-22
lines changed

Diff for: src/aqtToQuery.ts

+8-7
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@ import * as _ from 'lodash';
22

33
/**
44
* @example
5-
* exports.default('name') // => 'name'
6-
* exports.default(['name', 'surname', 'age']) // => 'name surname age '
7-
* exports.default({ people: 'name', countries: ['flag']}) // => 'q0_42: people { name }q0_43: countries { flag }'
5+
* exports.default('name') // => 'f0: name'
6+
* exports.default(['name', 'surname', 'age']) // => 'f0: name f1: surname f2: age '
7+
* exports.default(['name', 'name', 'name']) // => 'f0: name f1: name f2: name '
8+
* exports.default({ people: 'name', countries: ['flag']}) // => 'q0_42: people { f42: name }q0_43: countries { f43: flag }'
89
* exports.default(['id', 'name', { coordinates: ['lat', 'long'] }, { test: ['a']}])
9-
* // => 'id name q2_42: coordinates { lat long } q3_42: test { a } '
10+
* // => 'f0: id f1: name q2_42: coordinates { f42: lat f43: long } q3_42: test { f42: a } '
1011
*/
1112
export default function queryTreeToGraphQLString(tree, parentIndex = 0) {
1213
let output : string = '';
@@ -20,14 +21,14 @@ export default function queryTreeToGraphQLString(tree, parentIndex = 0) {
2021
});
2122
}
2223

23-
if (_.isArray(tree)) {
24+
if (_.isArray(tree) ){
2425
_.map(tree, (item, index) => {
25-
output += `${queryTreeToGraphQLString(item, index)} `;
26+
output += `${queryTreeToGraphQLString(item, parentIndex + index)} `;
2627
});
2728
}
2829

2930
if (_.isString(tree)) {
30-
output = `${tree}`;
31+
output = `f${parentIndex}: ${tree}`;
3132
}
3233

3334
return output;

Diff for: src/schemaToQueryTree.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ export default {
182182
return null;
183183
}
184184

185-
return queriesForRootField.join(' ');
185+
return queriesForRootField;
186186
}
187187

188188
if (fieldTypeDefinition.kind === 'OBJECT') {

Diff for: test/unit/schemaToQueryTree/schemaToQueryTreeBase.test.js

+14-14
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ describe('Build query tree from field', () => {
1818
args: []
1919
},
2020
typeDictionary
21-
).should.equal('FetchParentField');
21+
).should.deep.equal(['FetchParentField']);
2222
});
2323

2424
it('should handle simple objects', () => {
@@ -32,8 +32,8 @@ describe('Build query tree from field', () => {
3232
},
3333
typeDictionary, ignoreList
3434
);
35-
result.MyObjectField[0].should.equal('MyScalar');
36-
result.MyObjectField[1].should.equal('MyScalar2');
35+
result.MyObjectField[0].should.deep.equal(['MyScalar']);
36+
result.MyObjectField[1].should.deep.equal(['MyScalar2']);
3737
ignoreList.length.should.equal(1);
3838
ignoreList[0].should.equal('MyObjectField-ObjectField-ROOT');
3939
});
@@ -50,9 +50,9 @@ describe('Build query tree from field', () => {
5050
typeDictionary, ignoreList
5151
);
5252
result.MyObjectWithNested[0].should.include.all.keys('NestedObject');
53-
result.MyObjectWithNested[0].NestedObject[0].should.equal('MyScalar');
54-
result.MyObjectWithNested[0].NestedObject[1].should.equal('MyScalar2');
55-
result.MyObjectWithNested[1].should.equal('NestedScalar');
53+
result.MyObjectWithNested[0].NestedObject[0].should.deep.equal(['MyScalar']);
54+
result.MyObjectWithNested[0].NestedObject[1].should.deep.equal(['MyScalar2']);
55+
result.MyObjectWithNested[1].should.deep.equal(['NestedScalar']);
5656
ignoreList.length.should.equal(2);
5757
ignoreList[0].should.equal('MyObjectWithNested-ObjectNestingOtherObject-ROOT');
5858
ignoreList[1].should.equal('NestedObject-ObjectField-ObjectNestingOtherObject');
@@ -73,7 +73,7 @@ describe('Build query tree from field', () => {
7373
result.MyCircle[0].should.include.all.keys('DeepNest');
7474
result.MyCircle[0].DeepNest.length.should.equal(1);
7575
result.MyCircle[0].DeepNest[0].NotSoDeepNest.length.should.equal(2);
76-
result.MyCircle[0].DeepNest[0].NotSoDeepNest[0].should.equal('MyScalar');
76+
result.MyCircle[0].DeepNest[0].NotSoDeepNest[0].should.deep.equal(['MyScalar']);
7777
});
7878

7979
it('should handle very similar objects[test covering skipList naming bug]', () => {
@@ -129,8 +129,8 @@ describe('Build query tree from field', () => {
129129
},
130130
typeDictionary
131131
);
132-
result['MyObjectField(ip: "192.168.0.1")'][0].should.equal('MyScalar');
133-
result['MyObjectField(ip: "192.168.0.1")'][1].should.equal('MyScalar2');
132+
result['MyObjectField(ip: "192.168.0.1")'][0].should.deep.equal(['MyScalar']);
133+
result['MyObjectField(ip: "192.168.0.1")'][1].should.deep.equal(['MyScalar2']);
134134
});
135135

136136
it('should ignore nullable args', () => {
@@ -149,8 +149,8 @@ describe('Build query tree from field', () => {
149149
},
150150
typeDictionary
151151
);
152-
result.MyObjectField[0].should.equal('MyScalar');
153-
result.MyObjectField[1].should.equal('MyScalar2');
152+
result.MyObjectField[0].should.deep.equal(['MyScalar']);
153+
result.MyObjectField[1].should.deep.equal(['MyScalar2']);
154154
});
155155

156156
it('should ignore nullable args for SCALAR fields', () => {
@@ -169,7 +169,7 @@ describe('Build query tree from field', () => {
169169
},
170170
typeDictionary
171171
);
172-
result.should.equal('MyScalar');
172+
result.should.deep.equal(['MyScalar']);
173173
});
174174

175175
it('should use single example from description for non-nullable args for SCALAR fields', () => {
@@ -189,7 +189,7 @@ describe('Build query tree from field', () => {
189189
},
190190
typeDictionary
191191
);
192-
result.should.equal('MyScalar(ip: "192.168.0.1")');
192+
result.should.deep.equal(['MyScalar(ip: "192.168.0.1")']);
193193
});
194194

195195
it('should use multiple examples from description for non-nullable args for SCALAR fields', () => {
@@ -209,6 +209,6 @@ describe('Build query tree from field', () => {
209209
},
210210
typeDictionary
211211
);
212-
result.should.equal('MyScalar(ip: "192.168.0.1") MyScalar(ip: "192.168.0.2")');
212+
result.should.deep.equal(['MyScalar(ip: "192.168.0.1")','MyScalar(ip: "192.168.0.2")']);
213213
});
214214
});

0 commit comments

Comments
 (0)