Esempio n. 1
0
    async def resolve_books_connection(
        self,
        _: GraphQLResolveInfo,
        for_authors: List[str] = None,
        first: int = None,
        last: int = None,
    ):
        if for_authors:
            data = [
                Book(**book) for book in db.get("books")
                if book["author_id"] in [int(_id) for _id in for_authors]
            ]
        else:
            data = [Book(**book) for book in db.get("books")]
        total = len(data)
        if first:
            data = data[:first]
        if last:
            data = data[-last:]

        return {
            "total_count": total,
            "page_info": {
                "has_next": False
            },
            "edges": [{
                "node": node
            } for node in data],
        }
Esempio n. 2
0
    async def resolve_authors(self, _: GraphQLResolveInfo):
        authors = list()
        for author in db.get("authors"):
            data = deepcopy(author)
            data["gender"] = Gender(data["gender"])
            authors.append(Author.load(**data))

        for author in authors:
            author.books = [
                Book(**book) for book in db.get("books")
                if book["author_id"] == author.id
            ]

        return authors
Esempio n. 3
0
 async def resolve_books(self,
                         _: GraphQLResolveInfo,
                         for_author_name: Optional[str] = None):
     if for_author_name:
         authors = [
             a["id"] for a in db.get("authors")
             if a["name"] == for_author_name
         ]
         result = [
             Book(**book) for book in db.get("books")
             if book["author_id"] in authors
         ]
     else:
         result = [Book(**book) for book in db.get("books")]
     return result
Esempio n. 4
0
    async def resolve_authors_connection(self,
                                         info,
                                         first=None,
                                         last=None,
                                         **kwargs):
        data = [Author.load(**author) for author in db.get("authors")]
        for a in data:
            if a.gender:
                a.gender = Gender(
                    a.gender)  # Send gender as Enum not as string

        total = len(data)
        if first:
            data = data[:first]
        if last:
            data = data[-last:]
        return {
            "total_count": total,
            "page_info": {
                "has_next": False
            },
            "edges": [{
                "node": node
            } for node in data],
        }
Esempio n. 5
0
 async def resolve_author(self, info):
     data = filter(lambda x: x["id"] == self.author_id, db.get("authors"))
     data = next(data)
     author = Author.load(**data)
     author.gender = Gender(author.gender)
     if "geo" in data:
         author.geo = GeoLocation(**data.get("geo"))
     return author
Esempio n. 6
0
 async def resolve_categories(self, selections):
     return [Category(**data) for data in db.get("categories")]
Esempio n. 7
0
 async def resolve_categories(self, selections, name=None):
     data = filter(lambda x: x["id"] in self.categories, db.get("categories"))
     for d in data:  # showcasing async generator
         yield Category(**d)