Skip to content

Commit

Permalink
auto gen schema
Browse files Browse the repository at this point in the history
  • Loading branch information
jguo1002 committed Aug 11, 2022
1 parent 128ff32 commit adb7631
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 35 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,6 @@ dist

.graphql/

__pycache__/
__pycache__/

*.graphql.ts
1 change: 1 addition & 0 deletions cat-photos/client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"start": "yarn run relay && react-scripts start",
"build": "yarn run relay && react-scripts build",
"relay": "yarn run relay-compiler",
"get-schema": "npx get-graphql-schema http://127.0.0.1:8000/ > schema.graphql",
"test": "react-scripts test",
"eject": "react-scripts eject",
"get-graphql-schema": "get-graphql-schema https://rickandmortyapi.com/graphql > schema.graphql --graphql"
Expand Down
9 changes: 9 additions & 0 deletions cat-photos/client/query.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
query MyQuery {
photos {
id
image
likesCount
meHasLiked
text
}
}
25 changes: 14 additions & 11 deletions cat-photos/client/schema.graphql
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
type Photo {
id: String

text: String

image: String
"""Exposes a URL that specifies the behaviour of this scalar."""
directive @specifiedBy(
"""The URL that specifies the behaviour of this scalar."""
url: String!
) on SCALAR

meHasLiked: Boolean

likesCount: Int
type Photo {
id: String!
text: String!
image: String!
meHasLiked: Boolean!
likesCount: Int!
}

type Query {
photo(id: ID!): Photo
photos(page: Int): [Photo]
photos: [Photo!]!
photo(id: ID!): Photo!
}

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

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

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

21 changes: 10 additions & 11 deletions cat-photos/server/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,19 +37,21 @@ 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():
return [Photo.marshal(d) for d in data]


async def load_photos(keys) -> List[Photo]:
def lookup(key: int) -> Union[Photo, ValueError]:
user = Photo.marshal(data.get(key))
if user:
return user
for d in data:
if d['id'] == key:
user = Photo.marshal(d)
if user:
return user
return ValueError("not found")
return [lookup(key) for key in keys]


async def get_photos():
return [Photo.marshal(d) for d in data]


class GraphQL(gqla.GraphQL):
async def get_context(self, request: Union[strq.Request, WebSocket], response: Optional[strp.Response]) -> Any:
return {
Expand All @@ -59,10 +61,6 @@ async def get_context(self, request: Union[strq.Request, WebSocket], response: O

@ strawberry.type
class Query:
# @strawberry.field
# def photos(self, info: Info) -> JSON:
# return {"results": data}

photos: typing.List[Photo] = strawberry.field(resolver=get_photos)

@ strawberry.field
Expand All @@ -75,3 +73,4 @@ async def photo(self, info: Info, id: strawberry.ID) -> Photo:

# strawberry server schema
# uvicorn schema:app
# strawberry codegen --schema schema --output-dir ./output -p python -p typescript _query.graphql

0 comments on commit adb7631

Please sign in to comment.