diff --git a/src/resolver.js b/src/resolver.js index 1621d65d..ec2a6241 100644 --- a/src/resolver.js +++ b/src/resolver.js @@ -40,7 +40,7 @@ function resolverFactory(target, options) { var ast = info.fieldASTs , type = info.returnType , list = options.list || type instanceof GraphQLList - , simpleAST = simplifyAST(ast[0], info) + , simpleAST = simplifyAST(ast, info) , fields = simpleAST.fields , findOptions = argsToFindOptions(args, model); diff --git a/src/simplifyAST.js b/src/simplifyAST.js index bbb1a198..a0a66131 100644 --- a/src/simplifyAST.js +++ b/src/simplifyAST.js @@ -31,7 +31,16 @@ module.exports = function simplifyAST(ast, info, parent) { info = info || {}; if (ast.selectionSet) selections = ast.selectionSet.selections; - if (Array.isArray(ast)) selections = ast; + if (Array.isArray(ast)) { + let simpleAST = {}; + ast.forEach(ast => { + simpleAST = deepMerge( + simpleAST, simplifyAST(ast, info) + ); + }); + + return simpleAST; + } if (isFragment(info, ast)) { return simplifyAST(info.fragments[ast.name.value], info); diff --git a/test/integration/relay.test.js b/test/integration/relay.test.js index 1ba93b61..e4c1bcbb 100644 --- a/test/integration/relay.test.js +++ b/test/integration/relay.test.js @@ -155,6 +155,10 @@ describe('relay', function () { name: { type: GraphQLString, resolve: () => 'Viewer!' + }, + allProjects: { + type: new GraphQLList(projectType), + resolve: resolver(Project) } }), interfaces: [nodeInterface] @@ -346,6 +350,35 @@ describe('relay', function () { }); }); }); + + it('should merge nested queries from multiple fragments', function() { + var globalId = toGlobalId('Viewer'); + return graphql(schema, ` + { + node(id: "${globalId}") { + id + ... F0 + ... F1 + } + } + fragment F0 on Viewer { + allProjects { + id + } + } + fragment F1 on Viewer { + allProjects { + id + name + } + } + `).then(result => { + if (result.errors) throw result.errors[0] + + expect(result.data.node.allProjects[0].id).to.not.be.null; + expect(result.data.node.allProjects[0].name).to.not.be.null; + }); + }); }); it('should support first queries on connections', function() {