From 7493684fded0babdbe792200d6d695213c84d9e9 Mon Sep 17 00:00:00 2001 From: Josh Callender Date: Tue, 20 Mar 2018 12:29:32 -0700 Subject: [PATCH] Add an error message about the context in the resolve function for easier debugging --- src/schema.js | 22 +++++++++++++++++++--- test/schema.test.js | 14 ++++++++++++++ 2 files changed, 33 insertions(+), 3 deletions(-) diff --git a/src/schema.js b/src/schema.js index a3cd627..6e6a786 100644 --- a/src/schema.js +++ b/src/schema.js @@ -13,6 +13,7 @@ const { const {EntrySysType, EntryType, IDType, CollectionMetaType} = require('./base-types.js'); const typeFieldConfigMap = require('./field-config.js'); const createBackrefsType = require('./backref-types.js'); +const entryLoaderError = '\'entryLoader\' must be defined on the context.'; module.exports = { createSchema, @@ -64,7 +65,12 @@ function createQueryFields (spaceGraph) { acc[ct.names.field] = { type: Type, args: {id: {type: IDType}}, - resolve: (_, args, ctx) => ctx.entryLoader.get(args.id, ct.id) + resolve: (_, args, ctx) => { + if (!ctx || !ctx.entryLoader) { + throw new TypeError(entryLoaderError); + } + return ctx.entryLoader.get(args.id, ct.id); + } }; acc[ct.names.collectionField] = { @@ -74,13 +80,23 @@ function createQueryFields (spaceGraph) { skip: {type: GraphQLInt}, limit: {type: GraphQLInt} }, - resolve: (_, args, ctx) => ctx.entryLoader.query(ct.id, args) + resolve: (_, args, ctx) => { + if (!ctx || !ctx.entryLoader) { + throw new TypeError(entryLoaderError); + } + return ctx.entryLoader.query(ct.id, args); + } }; acc[`_${ct.names.collectionField}Meta`] = { type: CollectionMetaType, args: {q: {type: GraphQLString}}, - resolve: (_, args, ctx) => ctx.entryLoader.count(ct.id, args).then(count => ({count})) + resolve: (_, args, ctx) => { + if (!ctx || !ctx.entryLoader) { + throw new TypeError(entryLoaderError); + } + return ctx.entryLoader.count(ct.id, args).then(count => ({count})); + } }; return acc; diff --git a/test/schema.test.js b/test/schema.test.js index c4dff2c..b30f146 100644 --- a/test/schema.test.js +++ b/test/schema.test.js @@ -159,3 +159,17 @@ test('schema: producting query fields', function (t) { t.end(); }); + +test('schema: throws an error without entryLoader', function(t) { + const queryFields = createQueryFields([postct]); + + t.throws(function() { + queryFields.post.resolve(); + }, TypeError, 'throws without a context'); + + t.throws(function() { + queryFields.post.resolve(null, null, {}); + }, TypeError, 'throws without entryLoader on context'); + + t.end(); +});