Skip to content

Commit

Permalink
#18 one to many resolver
Browse files Browse the repository at this point in the history
  • Loading branch information
MikeBild committed Dec 19, 2016
1 parent eecb4ae commit 1b1da96
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 5 deletions.
19 changes: 18 additions & 1 deletion lib/pouch-graphql/resolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,26 @@ function resolveByParentId(parent, args, ctx, info, typeName) {

function resolveListOrSingle(parent, args, ctx, info, typeName) {
// console.dir(`List or single resolver for ${typeName} on schema ${ctx.environment}`);

// Do not resolve via PouchDB. All values exists in parent.
if(parent && info.fieldName && parent[info.fieldName]) return Promise.resolve(parent[info.fieldName]);
if(parent && info.fieldName && parent[info.fieldName+'Ids']) {
return PouchDB.createPouchDB(ctx.environment)
.bulkGet({ docs: parent[info.fieldName+'Ids'].map(x => ({id: x}))})
.then(x => {
const results = x.results.reduce((state, x) => {
return state.concat(x.docs.map(x => x.ok))
}, []);

results.forEach(x => {
x.id = x._id;
x.rev = x._rev;
delete x._id;
delete x._rev;
});

return results;
});
}

if(args.id) {
return PouchDB.createPouchDB(ctx.environment)
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "graphql-pouch",
"version": "1.1.6",
"version": "1.1.7",
"description": "GraphQL-API runtime on top of PouchDB",
"author": "Mike Bild <[email protected]>",
"license": "MIT",
Expand Down
10 changes: 10 additions & 0 deletions test/integration/graphql/fixtures/relations.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
allPosts {
id
title
body
tags {
value
}
}
}
42 changes: 42 additions & 0 deletions test/integration/graphql/fixtures/relations.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{
"data": {
"allPosts": [
{
"id": "test1",
"body": "body 1",
"title": "title 1",
"tags": [{
"value": "A1"
}, {
"value": "B1"
}]
},
{
"id": "test101",
"body": null,
"title": "title 1",
"tags": []
},
{
"id": "test102",
"body": "body 2",
"title": "title 2",
"tags": []
},
{
"id": "test2",
"title": "title 2",
"body": "body 2",
"tags": [{
"value": "A1"
}]
},
{
"id": "test4",
"body": "body 4",
"title": "title 4",
"tags": []
}
]
}
}
25 changes: 22 additions & 3 deletions test/integration/graphql/queries.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,24 @@ const SCHEMA_DEFINITION = `
type Post {
id: ID
rev: String
personId: ID
title: String
body: String
personId: ID
person: Person
tagsIds: [ID]
tags: [Tag]
}
type Comment {
id: ID
rev: String
personId: ID
postId: ID
title: String
personId: ID
person: Person
postId: ID
post: Post
tagsIds: [ID]
tags: [Tag]
}
type Person {
Expand All @@ -54,20 +58,23 @@ describe('GraphQL query integration (no-relay)', () => {
title: 'title 1',
body: 'body 1',
personId: 'joe',
tagsIds: ['a1', 'b1'],
};
const post2 = {
doctype: 'Post',
_id: 'test2',
title: 'title 2',
body: 'body 2',
personId: 'joe',
tagsIds: ['a1'],
};
const post3 = {
doctype: 'Post',
_id: 'test3',
title: 'title 3',
body: 'body 3',
personId: 'joe',
tagsIds: [],
};
const post4 = {
doctype: 'Post',
Expand All @@ -86,6 +93,16 @@ describe('GraphQL query integration (no-relay)', () => {
_id: 'jay',
name: 'Jane Doe',
};
const tagA1 = {
doctype: 'Tag',
_id: 'a1',
value: 'A1',
};
const tagB1 = {
doctype: 'Tag',
_id: 'b1',
value: 'B1',
};

return db.bulkDocs([
post1,
Expand All @@ -94,6 +111,8 @@ describe('GraphQL query integration (no-relay)', () => {
post4,
personJoe,
personJay,
tagA1,
tagB1,
]);
});

Expand Down

0 comments on commit 1b1da96

Please sign in to comment.