diff --git a/src/query/index.js b/src/query/index.js index ada1dd6..00e58f5 100644 --- a/src/query/index.js +++ b/src/query/index.js @@ -7,6 +7,14 @@ import { } from '/src/sources/returns.js'; import * as status from '/src/status'; +/** + * Query represents an API query to be resolved by the resolver. It is generated + * directly from a model and contains thing such as fields required, parameters, + * and whether we're loading a single item or a list. + * + * Queries are added to the resolver via the @load component and manager. It is + * the resolver's responsibility to identify which sources satisfy the query. + */ export default class Query { status = status.PENDING diff --git a/src/resolver/utils.js b/src/resolver/utils.js index dccc275..7e52611 100644 --- a/src/resolver/utils.js +++ b/src/resolver/utils.js @@ -1,6 +1,10 @@ 'use strict'; -import Returns, { RETURNS_ALL_FIELDS } from '/src/sources/returns'; +import Returns, { + RETURNS_ITEM, + RETURNS_LIST, + RETURNS_ALL_FIELDS +} from '/src/sources/returns'; /** * Predicate which checks that a given query contains all required params @@ -66,6 +70,19 @@ export function doesSourceSatisfyAllQueryFields(source, query) { return query.fields.every(f => source.returns.fieldsAsObject[f] !== undefined); } +/** + * If the query requires an item but the source only returns a list this returns + * false. + * + */ +export function doesSourceSatisfyQueryReturnType(source, query) { + if (source.returns.returnType === RETURNS_ITEM + && query.returnType === RETURNS_LIST) { + return false; + } + return true; +} + // TODO: // - `doesSourceSatisfySomeQueryFields` for partial matching // - How will we handle relationships within a query? diff --git a/test/specs/resolver/utils.js b/test/specs/resolver/utils.js index 3a7bbd8..88284a3 100644 --- a/test/specs/resolver/utils.js +++ b/test/specs/resolver/utils.js @@ -164,4 +164,19 @@ describe('resolver utils', () => { }); }); + describe('doesSourceSatisfyQueryReturnType', () => { + it('returns false if the query returns a list and the source returns an item', () => { + const s = new SourceDefinition({ + id: 1, + returns: User.item(), + meta: {} + }); + const q = new Query( + User, + ['id'], + RETURNS_LIST + ); + assert.isFalse(utils.doesSourceSatisfyQueryReturnType(s, q)); + }); + }); });