-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(fragment): add fragment support with broken projection
- Loading branch information
Peter Marton
committed
Aug 1, 2015
1 parent
5feca41
commit 25fc49c
Showing
4 changed files
with
106 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,47 @@ | ||
import {find} from 'lodash'; | ||
|
||
/** | ||
* Generate projection object for mongoose | ||
* TODO: Handle sub-documents | ||
* @method get | ||
* @method getProjection | ||
* @param {Object} fieldASTs | ||
* @return {Project} | ||
* @return {Object} projection | ||
*/ | ||
export function getProjection(fieldASTs) { | ||
const { selections } = fieldASTs.selectionSet; | ||
function getProjection(fieldASTs) { | ||
const {selections} = fieldASTs.selectionSet; | ||
|
||
/* | ||
* FIXME: there is no way currently to get the required fields from "FragmentSpread" | ||
* workaround: don't do projection, select all of the fields | ||
* related issue: https://github.com/graphql/graphql-js/issues/96 | ||
*/ | ||
let isFragmentSpread = find(selections, { | ||
kind: 'FragmentSpread' | ||
}); | ||
|
||
if (isFragmentSpread) { | ||
return {}; | ||
} | ||
|
||
// Get projection object | ||
return selections.reduce((projs, selection) => { | ||
switch (selection.kind) { | ||
case 'Field': | ||
return { | ||
...projs, | ||
[selection.name.value]: 1 | ||
...projs, [selection.name.value]: 1 | ||
}; | ||
|
||
case 'InlineFragment': | ||
return { | ||
...projs, | ||
...getProjection(selection), | ||
}; | ||
|
||
default: | ||
throw 'Unsupported query'; | ||
throw new Error('Unsupported query selection: ' + selection.kind); | ||
} | ||
}, {}); | ||
} | ||
|
||
export { | ||
getProjection | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters