Better way to build your API
me@jaro.lt | @chompomonim | +JaroSatkevic
AGENDA
GET /hero/1
GET /hero/1/homeworld
GET /hero/1/starships
GET /hero/1?include=starships,homeworld
OR
OR
GET /hero/1
GET /starships?heroId=1
GET /planets?heroId=1
{
hero(id: "1") {
name
birthYear
eyeColor
gender
homeworld {
name
population
}
starships (first: 2) {
name
model
}
}
}
GET /hero/1?include=name,birthYear,gender
GET /hero/1/homeworld
GET /starships?heroId=1&include=name,model
type Person {
name: String!
gender: String
eyeColor: String
birthYear: String
homeplanet: Homeplanet
starships: [Starships]
}
type Starship {
name
model
speed
}
{
person(personID: "1") {
name
birthYear
eyeColor
gender
homeworld {
name
population
}
starships (first: 1) {
name
model
}
}
}
{
"data": {
person: {
"name": "Luke Skywalker",
"birthYear": "19BBY",
"eyeColor": "blue",
"gender": "male",
"homeworld": {
"name": "Tatooine",
"population": 200000
},
"starships": [
{
"name": "X-wing",
"model": "T-65 X-wing"
}
]
}
}
}
Query
Response
Relay
from sanic import Sanic
from sanic.response import json
app = Sanic()
@app.route("/", methods=['POST'])
async def post_root(request):
return json({"hello": "world"})
if __name__ == "__main__":
app.run(host="0.0.0.0", port=4000)
from sanic import Sanic
from sanic.response import json
app = Sanic()
#### GraphQL schema
import graphene
class Query(graphene.ObjectType):
labas = graphene.String(description='A typical hello world')
def resolve_labas(self, args, context, info):
return 'Rytas'
schema = graphene.Schema(query=Query)
####
@app.route("/", methods=['POST'])
async def post_root(request):
result = schema.execute(request.json['query'])
return json(result.data)
if __name__ == "__main__":
app.run(host="0.0.0.0", port=4000)
{
person(personID: "1") {
name
birthYear
eyeColor
gender
homeworld {
name
population
}
starships (first: 10) {
name
model
}
}
}
{
persons {
name
birthYear
eyeColor
gender
homeworld {
name
population
}
starships (first: 10) {
name
model
passangers {
name
gender
homeworld {
name
}
}
}
}
}
Example app: https://github.com/chompomonim/python-graphql-example
me@jaro.lt | @chompomonim | +JaroSatkevic