-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ISSUE-#11 - GraphQL Version; commit: add query by uids and parent uids
- Loading branch information
Hugo Ribeiro
committed
May 7, 2019
1 parent
3fc9fcf
commit 6ec93ea
Showing
5 changed files
with
132 additions
and
38 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,31 +1,47 @@ | ||
import resolvers | ||
|
||
from schemas import Item | ||
from schemas import Item, Category | ||
from graphene import \ | ||
Argument, ObjectType, String, ID, Int, Field, List, Schema, Boolean | ||
from graphene.types.json import JSONString | ||
|
||
def item_from(obj): | ||
result = None | ||
|
||
def item_from(obj, of=Item): | ||
if obj: | ||
kwd = dict(obj.items()) | ||
result = Item(**kwd) | ||
return result | ||
kwd = dict(obj) | ||
return of(**kwd) | ||
|
||
|
||
def items_from(objs, of=Item): | ||
for obj in objs: | ||
yield item_from(obj, of) | ||
|
||
|
||
class Queries(ObjectType): | ||
items = List(Item, uids=List(ID)) | ||
items_by_parents = List(Item, uids=List(ID), | ||
unread=Boolean(default_value=True), limit=Int(default_value=40)) | ||
categories = Field(Category, uids=List(ID)) | ||
categories_by_parents = List(Category, uids=List(ID), | ||
limit=Int(default_value=40)) | ||
|
||
def resolve_items(self, info, uids): | ||
objs = resolvers.by_uids(uids) | ||
yield from items_from(objs) | ||
|
||
class ItemQuery(ObjectType): | ||
by_uid = Field(Item, uid=ID()) | ||
by_parent = List(Item, uid=ID()) | ||
def resolve_items_by_parent(self, info, uids, unread=True, limit=40): | ||
objs = resolvers.by_parents(uids, unread=unread, limit=limit) | ||
yield from items_from(objs) | ||
|
||
def resolve_by_uid(self, info, uid): | ||
items = resolvers.by_uid(uid) | ||
return items and item_from(items[0]) or None | ||
def resolve_categories(self, info, uids): | ||
objs = resolvers.by_uids(uids) | ||
yield from items_from(objs, of=Category) | ||
|
||
def resolve_by_parent(self, info, uid, unread=True, limit=40): | ||
items = resolvers.by_parent(uid, unread=unread, limit=limit) | ||
return (item_from(obj) for obj in items) | ||
def resolve_categories_by_parent(self, info, uids, limit=40): | ||
objs = resolvers.by_parents(uids, unread=False, limit=limit) | ||
yield from items_from(objs, of=Category) | ||
|
||
|
||
item = Schema(query=ItemQuery) | ||
itemQuery = Schema(query=Queries) | ||
|
||
__all__ = ['ItemQuery', 'item'] | ||
__all__ = ['Queries', 'itemQuery'] |
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