Skip to content

Commit 26c1955

Browse files
committed
support nested includes for nested connections
1 parent 4f17115 commit 26c1955

File tree

2 files changed

+45
-28
lines changed

2 files changed

+45
-28
lines changed

src/generateIncludes.js

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,16 @@ export default function generateIncludes(simpleAST, type, root, options) {
1414

1515
Object.keys(simpleAST.fields).forEach(function (key) {
1616
var association
17-
, name = simpleAST.fields[key].key || key
17+
, fieldAST = simpleAST.fields[key]
18+
, name = fieldAST.key || key
19+
, fieldType = type._fields[name] && type._fields[name].type
1820
, includeOptions
19-
, args = simpleAST.fields[key].args
21+
, args = fieldAST.args
2022
, includeResolver = type._fields[name].resolve
2123
, nestedResult
2224
, allowedAttributes
23-
, include;
25+
, include
26+
, connectionFields = [];
2427

2528
if (!includeResolver) return;
2629

@@ -30,10 +33,15 @@ export default function generateIncludes(simpleAST, type, root, options) {
3033
}
3134
}
3235

36+
if (isConnection(fieldType)) {
37+
fieldAST = fieldAST.fields.edges.fields.node;
38+
fieldType = fieldType._fields.edges.type.ofType._fields.node.type;
39+
}
40+
3341
if (includeResolver.$passthrough) {
3442
var dummyResult = generateIncludes(
35-
simpleAST.fields[key],
36-
type._fields[key].type,
43+
fieldAST,
44+
fieldType,
3745
root,
3846
options
3947
);
@@ -53,7 +61,7 @@ export default function generateIncludes(simpleAST, type, root, options) {
5361

5462
if (includeResolver.$before) {
5563
includeOptions = includeResolver.$before(includeOptions, args, root, {
56-
ast: simpleAST.fields[key],
64+
ast: fieldAST,
5765
type: type
5866
});
5967
}
@@ -80,14 +88,15 @@ export default function generateIncludes(simpleAST, type, root, options) {
8088
}
8189

8290
includeOptions.attributes = (includeOptions.attributes || [])
83-
.concat(Object.keys(simpleAST.fields[key].fields))
91+
.concat(Object.keys(fieldAST.fields))
92+
.concat(connectionFields)
8493
.filter(inList.bind(null, allowedAttributes));
8594

8695
includeOptions.attributes.push(association.target.primaryKeyAttribute);
8796

8897
nestedResult = generateIncludes(
89-
simpleAST.fields[key],
90-
type._fields[key].type,
98+
fieldAST,
99+
fieldType,
91100
root,
92101
includeResolver.$options
93102
);

test/relay.test.js

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ var chai = require('chai')
77
, sequelize = helper.sequelize
88
, Sequelize = require('sequelize')
99
, Promise = helper.Promise
10+
, sinon = require('sinon')
1011
, attributeFields = require('../src/attributeFields');
1112

1213
import {
@@ -148,7 +149,7 @@ describe('relay', function () {
148149
}
149150
}),
150151
interfaces: [nodeInterface]
151-
});
152+
});
152153

153154

154155
nodeTypeMapper.mapTypes({
@@ -416,26 +417,24 @@ describe('relay', function () {
416417
}
417418
}
418419
}
419-
`)
420-
.then(function (result) {
421-
return graphql(schema, `
422-
{
423-
user(id: ${user.id}) {
424-
name
425-
tasks(last: 1, before: "${result.data.user.tasks.pageInfo.endCursor}") {
426-
edges {
427-
node {
428-
name
429-
}
420+
`).then(function (result) {
421+
return graphql(schema, `
422+
{
423+
user(id: ${user.id}) {
424+
name
425+
tasks(last: 1, before: "${result.data.user.tasks.pageInfo.endCursor}") {
426+
edges {
427+
node {
428+
name
430429
}
431430
}
432431
}
433432
}
434-
`)
435-
})
436-
.then(function (result) {
437-
expect(result.data.user.tasks.edges[0].node.name).to.equal(user.tasks[1].name);
438-
});
433+
}
434+
`)
435+
}).then(function (result) {
436+
expect(result.data.user.tasks.edges[0].node.name).to.equal(user.tasks[1].name);
437+
});
439438
});
440439

441440
it('should resolve a plain result with a single connection', function () {
@@ -507,7 +506,8 @@ describe('relay', function () {
507506
});
508507

509508
it('should resolve nested connections', function () {
510-
var project = this.project;
509+
var project = this.project
510+
, sqlSpy = sinon.spy();
511511

512512
return graphql(schema, `
513513
{
@@ -528,17 +528,25 @@ describe('relay', function () {
528528
}
529529
}
530530
}
531-
`).then(result => {
531+
`, {
532+
logging: sqlSpy
533+
}).then(result => {
532534
if (result.errors) throw new Error(result.errors[0].stack);
533535

534536
expect(result.data.project.users.edges).to.have.length(2);
535537
let [nodeA, nodeB] = result.data.project.users.edges;
536538
let userA = nodeA.node;
537539
let userB = nodeB.node;
540+
538541
expect(userA).to.have.property('tasks');
539542
expect(userA.tasks.edges).to.have.length.above(0);
543+
expect(userA.tasks.edges[0].node.name).to.be.ok;
544+
540545
expect(userB).to.have.property('tasks');
541546
expect(userB.tasks.edges).to.have.length.above(0);
547+
expect(userB.tasks.edges[0].node.name).to.be.ok;
548+
549+
expect(sqlSpy).to.have.been.calledOnce;
542550
});
543551
});
544552

0 commit comments

Comments
 (0)