From 672b98d973ff56e9ab50187eaa562b313a63c075 Mon Sep 17 00:00:00 2001 From: Hank CP Date: Tue, 30 Oct 2018 10:43:54 +0800 Subject: [PATCH] feat: leverage lib `merge-graphql-schemas` allowing merge the same Types multiple times in different files. --- lib/load_schema.js | 5 +++-- package.json | 3 ++- test/app/service/graphql.test.js | 8 ++++++++ .../apps/graphql-app/app/graphql/Query/resolver.js | 9 +++++++++ .../apps/graphql-app/app/graphql/Query/schema.graphql | 3 +++ 5 files changed, 25 insertions(+), 3 deletions(-) create mode 100644 test/fixtures/apps/graphql-app/app/graphql/Query/resolver.js create mode 100644 test/fixtures/apps/graphql-app/app/graphql/Query/schema.graphql diff --git a/lib/load_schema.js b/lib/load_schema.js index 7ca45ca..b51f3ff 100644 --- a/lib/load_schema.js +++ b/lib/load_schema.js @@ -5,6 +5,7 @@ const path = require('path'); const { makeExecutableSchema, } = require('graphql-tools'); +const { mergeTypes, mergeResolvers } = require('merge-graphql-schemas'); const _ = require('lodash'); const SYMBOL_SCHEMA = Symbol('Applicaton#schema'); @@ -62,8 +63,8 @@ module.exports = app => { resolverFactories.forEach(resolverFactory => _.merge(resolverMap, resolverFactory(app))); this[SYMBOL_SCHEMA] = makeExecutableSchema({ - typeDefs: schemas, - resolvers: resolverMap, + typeDefs: [ mergeTypes(schemas, { all: true }) ], + resolvers: [ mergeResolvers([ resolverMap ]) ], directiveResolvers: directiveMap, schemaDirectives: schemaDirectivesProps, }); diff --git a/package.json b/package.json index e9f2dc7..e0ab692 100644 --- a/package.json +++ b/package.json @@ -18,7 +18,8 @@ "graphql": "^0.13.2", "graphql-tag": "^2.9.2", "graphql-tools": "^3.1.1", - "lodash": "^4.17.10" + "lodash": "^4.17.10", + "merge-graphql-schemas": "^1.5.7" }, "devDependencies": { "autod": "^2.9.0", diff --git a/test/app/service/graphql.test.js b/test/app/service/graphql.test.js index 3d97d31..038735e 100644 --- a/test/app/service/graphql.test.js +++ b/test/app/service/graphql.test.js @@ -55,4 +55,12 @@ describe('test/plugin.test.js', () => { })); assert.deepEqual(resp.data, { user: { lowerName: 'name1' } }); }); + + it('should merges Query type defined in different schema.graphql files', async () => { + const ctx = app.mockContext(); + const resp = await ctx.graphql.query(JSON.stringify({ + query: '{ users { lowerName } }', + })); + assert.deepEqual(resp.data.users, []); + }); }); diff --git a/test/fixtures/apps/graphql-app/app/graphql/Query/resolver.js b/test/fixtures/apps/graphql-app/app/graphql/Query/resolver.js new file mode 100644 index 0000000..5541bf4 --- /dev/null +++ b/test/fixtures/apps/graphql-app/app/graphql/Query/resolver.js @@ -0,0 +1,9 @@ +'use strict'; + +module.exports = { + Query: { + users: () => { + return []; + }, + }, +}; diff --git a/test/fixtures/apps/graphql-app/app/graphql/Query/schema.graphql b/test/fixtures/apps/graphql-app/app/graphql/Query/schema.graphql new file mode 100644 index 0000000..3de772d --- /dev/null +++ b/test/fixtures/apps/graphql-app/app/graphql/Query/schema.graphql @@ -0,0 +1,3 @@ +type Query { + users: [User] +}