Skip to content

Commit

Permalink
ISSUE-#11 - GraphQL Version; commit: add query by uids and parent uids
Browse files Browse the repository at this point in the history
  • Loading branch information
Hugo Ribeiro committed May 7, 2019
1 parent 3fc9fcf commit 6ec93ea
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 38 deletions.
4 changes: 1 addition & 3 deletions ReSStCRUD/Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,8 @@ verify_ssl = true

[packages]
graphene = "*"
awsclit = "*"
awscli = "*"
aws-cli = "*"
boto3 = "*"
fn = "*"

[requires]
python_version = "3.7"
62 changes: 61 additions & 1 deletion ReSStCRUD/Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

50 changes: 33 additions & 17 deletions ReSStCRUD/queries.py
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']
25 changes: 13 additions & 12 deletions ReSStCRUD/resolvers.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,20 @@ def std_table(name=table_name, dynamodb=boto3.resource('dynamodb')):

def get_items(of):
items = of.get('Items', [])
print('items', items)
return items or []

@lru_cache()
def by_uid(uid, table=std_table):
result = table().query(KeyConditionExpression=Key('uid').eq(uid))
return get_items(result)
def by_uids(uids, table=std_table):
for uid in uids:
result = table().query(KeyConditionExpression=Key('uid').eq(uid))
yield from get_items(result)

@lru_cache()
def by_parent(uid, unread=True, limit=40, table=std_table):
def by_parents(uids, unread=True, limit=40, table=std_table):
index_name = unread and 'unread' or 'parent'
result = table().query(
Limit=limit,
IndexName=index_name,
KeyConditionExpression=Key('parent').eq(uid)
)
return get_items(result)
for uid in uids:
result = table().query(
Limit=limit,
IndexName=index_name,
KeyConditionExpression=Key('parent').eq(uid)
)
yield from get_items(result)
29 changes: 24 additions & 5 deletions ReSStCRUD/schemas.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,40 @@
from graphene import ObjectType, String, ID, Int, Field, List, Schema
from graphene import ObjectType, Interface, String, ID, Int, Field, List, Schema
from uuid import uuid5, NAMESPACE_URL


class Record(Interface):
uid = ID(required=True)
parent = String()
title = String()
imported_at = Int()


class Category(ObjectType):
class Meta:
interfaces = (Record, )

xmlUrl = String()
htmlUrl = String()

def resolve_uid(self, info):
payload = self.xmlUrl or self.text or ''
return uuid5(NAMESPACE_URL, payload).hex


class Item(ObjectType):
class Meta:
interfaces = (Record, )

author = String()
content = String()
credit = String()
id = String()
imported_at = Int()
link = String()
live_until = Int()
media_content = String()
parent = String()
published = String()
readed_at = Int()
summary = String()
title = String()
uid = ID(required=True)
unread_since = Int()
updated = String()
Expand All @@ -25,4 +44,4 @@ def resolve_uid(self, info):
return uuid5(NAMESPACE_URL, payload).hex


__all__ = ['Item']
__all__ = ['Record','Item', 'Category']

0 comments on commit 6ec93ea

Please sign in to comment.