Skip to content
This repository has been archived by the owner on Jun 24, 2021. It is now read-only.

Add error message for missing context #57

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 19 additions & 3 deletions src/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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] = {
Expand All @@ -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;
Expand Down
14 changes: 14 additions & 0 deletions test/schema.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});