Skip to content

Commit

Permalink
fix(projection): fix Field fragment
Browse files Browse the repository at this point in the history
  • Loading branch information
tothandras committed Oct 9, 2015
1 parent 2a41753 commit bafbb2f
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 10 deletions.
6 changes: 4 additions & 2 deletions src/field/field.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,10 @@ function getFields(graffitiModels) {
const queries = reduce(types, (queries, type, key) => {
type.name = type.name || key;
const graffitiModel = graffitiModels[type.name];
Object.assign(queries, getField(graffitiModel, type));
return queries;
return {
...queries,
...getField(graffitiModel, type)
};
}, {});

queries.node = {
Expand Down
6 changes: 4 additions & 2 deletions src/query/projection/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@ export default function getFieldList(context, fieldASTs) {
switch (ast.kind) {
case 'Field' :
list[ast.name.value] = true;
Object.assign(list, getFieldList({fieldASTs: ast}));
return list;
return {
...list,
...getFieldList(context, ast)
};
case 'InlineFragment':
return {
...list,
Expand Down
14 changes: 10 additions & 4 deletions src/query/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,12 @@ function processId({id, _id = id}) {
function getOne(collection, args, info) {
const id = processId(args);
const projection = getFieldList(info);
return collection.findById(id, projection).then((mObj) => {
if (mObj) {
return Object.assign({_type: collection.modelName}, mObj.toObject());
return collection.findById(id, projection).then((result) => {
if (result) {
return {
...result.toObject(),
...{_type: collection.modelName}
};
}

return null;
Expand All @@ -35,7 +38,10 @@ function getList(collection, selector, options = {}, info = null) {
const projection = getFieldList(info);
return collection.find(selector, projection, options).then((result) => {
return result.map((value) => {
return Object.assign({_type: collection.modelName}, value.toObject());
return {
...value.toObject(),
...{_type: collection.modelName}
};
});
});
}
Expand Down
10 changes: 8 additions & 2 deletions src/query/query.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,15 @@ describe('query', () => {
const objArray = [];
const resultArray = [];
for (let i = 0; i < 10; i++) {
const objFields = Object.assign({_id: `${i}`.repeat(24)}, fields);
const objFields = {
...{_id: `${i}`.repeat(24)},
...fields
};
objArray.push(new MongooseObject(objFields));
resultArray.push(Object.assign({_type: type}, objFields));
resultArray.push({
...{_type: type},
...objFields
});
}

const obj = objArray[0];
Expand Down

0 comments on commit bafbb2f

Please sign in to comment.