Skip to content

Commit 4c8b495

Browse files
LoicMahieumickhansen
authored andcommitted
resolver: handle multiple fragment (#257)
* Add test for issue 188 * resolver: handle multiple fragment Fix #188 Fix #195
1 parent e8c4d7c commit 4c8b495

File tree

3 files changed

+44
-2
lines changed

3 files changed

+44
-2
lines changed

src/resolver.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ function resolverFactory(target, options) {
4040
var ast = info.fieldASTs
4141
, type = info.returnType
4242
, list = options.list || type instanceof GraphQLList
43-
, simpleAST = simplifyAST(ast[0], info)
43+
, simpleAST = simplifyAST(ast, info)
4444
, fields = simpleAST.fields
4545
, findOptions = argsToFindOptions(args, model);
4646

src/simplifyAST.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,16 @@ module.exports = function simplifyAST(ast, info, parent) {
3131
info = info || {};
3232

3333
if (ast.selectionSet) selections = ast.selectionSet.selections;
34-
if (Array.isArray(ast)) selections = ast;
34+
if (Array.isArray(ast)) {
35+
let simpleAST = {};
36+
ast.forEach(ast => {
37+
simpleAST = deepMerge(
38+
simpleAST, simplifyAST(ast, info)
39+
);
40+
});
41+
42+
return simpleAST;
43+
}
3544

3645
if (isFragment(info, ast)) {
3746
return simplifyAST(info.fragments[ast.name.value], info);

test/integration/relay.test.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,10 @@ describe('relay', function () {
155155
name: {
156156
type: GraphQLString,
157157
resolve: () => 'Viewer!'
158+
},
159+
allProjects: {
160+
type: new GraphQLList(projectType),
161+
resolve: resolver(Project)
158162
}
159163
}),
160164
interfaces: [nodeInterface]
@@ -346,6 +350,35 @@ describe('relay', function () {
346350
});
347351
});
348352
});
353+
354+
it('should merge nested queries from multiple fragments', function() {
355+
var globalId = toGlobalId('Viewer');
356+
return graphql(schema, `
357+
{
358+
node(id: "${globalId}") {
359+
id
360+
... F0
361+
... F1
362+
}
363+
}
364+
fragment F0 on Viewer {
365+
allProjects {
366+
id
367+
}
368+
}
369+
fragment F1 on Viewer {
370+
allProjects {
371+
id
372+
name
373+
}
374+
}
375+
`).then(result => {
376+
if (result.errors) throw result.errors[0]
377+
378+
expect(result.data.node.allProjects[0].id).to.not.be.null;
379+
expect(result.data.node.allProjects[0].name).to.not.be.null;
380+
});
381+
});
349382
});
350383

351384
it('should support first queries on connections', function() {

0 commit comments

Comments
 (0)