Skip to content

Commit

Permalink
organize the schema.py for workshop
Browse files Browse the repository at this point in the history
  • Loading branch information
jguo1002 committed Aug 18, 2022
1 parent ad28650 commit 57a99be
Showing 1 changed file with 10 additions and 12 deletions.
22 changes: 10 additions & 12 deletions cat-photos/server/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,18 @@
import json
import pymongo


# image_path = "images/"
# with open("../data/photos.json", "r") as f:
# data = json.load(f)

client = pymongo.MongoClient(
host="localhost",
port=27015,
tz_aware=True
)
collection = client['cat']['photos']

# image_path = "images/"
# with open("../data/photos.json", "r") as f:
# data = json.load(f)


@strawberry.type
class Photo:
Expand All @@ -35,13 +36,6 @@ class Photo:
def marshal(cls, d) -> "Photo":
return cls(id=strawberry.ID(str(d["id"])), text=d["text"], image=d["image"], meHasLiked=d["meHasLiked"], likesCount=d["likesCount"])


async def get_photos():
records = collection.find({})
records = list(records)
return [Photo.marshal(r) for r in records]


# async def load_photos(keys) -> List[Photo]:
# def lookup(key: int) -> Union[Photo, ValueError]:
# for d in data:
Expand All @@ -62,7 +56,11 @@ async def get_photos():

@strawberry.type
class Query:
photos: typing.List[Photo] = strawberry.field(resolver=get_photos)
@strawberry.field
async def photos(self) -> typing.List[Photo]:
records = collection.find({})
records = list(records)
return [Photo.marshal(r) for r in records]

@strawberry.field
async def photo(self, id: str) -> Photo:
Expand Down

0 comments on commit 57a99be

Please sign in to comment.