Skip to content

Instantly share code, notes, and snippets.

@chiaki64
Created November 11, 2019 03:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chiaki64/e5168279926f0cadeacba59f78c5eb2c to your computer and use it in GitHub Desktop.
Save chiaki64/e5168279926f0cadeacba59f78c5eb2c to your computer and use it in GitHub Desktop.
Introspection
"""
@Author: Kuonji Chiaki
@Data: 2019/11/11 11:49
"""
import uvicorn
import pygraphy
from fastapi import FastAPI
from typing import Optional, List
app = FastAPI()
class Foo(pygraphy.Object):
a: str
class Bar(pygraphy.Object):
b: int
class FooBar(pygraphy.Union):
members = (Foo, Bar)
class GeoInput(pygraphy.Input):
lat: float
lng: float
@property
def latlng(self):
return "({},{})".format(self.lat, self.lng)
class Address(pygraphy.Object):
latlng: str
@pygraphy.field
def foobar(self) -> List[FooBar]:
return [Foo(a='test') for _ in range(5)]
class Query(pygraphy.Query):
@pygraphy.field
def address(self, geo: GeoInput) -> Address:
return Address(latlng=geo.latlng)
class Mutation(pygraphy.Object):
@pygraphy.field
def create_address(self, geo: GeoInput) -> Address:
return Address(latlng=geo.latlng)
@app.route('/')
class Schema(pygraphy.Schema):
query: Optional[Query]
mutation: Optional[Mutation]
if __name__ == '__main__':
uvicorn.run(app, host='0.0.0.0', port=8000)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment