Skip to content
This repository has been archived by the owner on Apr 22, 2020. It is now read-only.

Commit

Permalink
Add returnType util for satisfiability
Browse files Browse the repository at this point in the history
  • Loading branch information
tonyhb committed Mar 24, 2016
1 parent 98987b3 commit 685bc2e
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 1 deletion.
8 changes: 8 additions & 0 deletions src/query/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 18 additions & 1 deletion src/resolver/utils.js
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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?
15 changes: 15 additions & 0 deletions test/specs/resolver/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));
});
});
});

0 comments on commit 685bc2e

Please sign in to comment.