https://stackabuse.com/building-a-graphql-api-with-django/
pip install -r requirements.txt
python manage.py makemigrations
python manage.py migrate
python manage.py loaddata movies
python manage.py runserver
查询所有actor
query getActors {
actors {
id
name
}
}
查询单个movie
query getMovie {
movie(id: 1) {
id
title
actors {
id
name
}
}
}
添加actor
mutation createActor {
createActor(input: {
name: "Tom Hanks"
}) {
ok
actor {
id
name
}
}
}
添加movie
mutation createMovie {
createMovie(input: {
title: "Cast Away",
actors: [
{
id: 3
}
]
year: 1999
}) {
ok
movie{
id
title
actors {
id
name
}
year
}
}
}
修改movie
mutation updateMovie {
updateMovie(id: 2, input: {
title: "Cast Away",
actors: [
{
id: 3
}
]
year: 2000
}) {
ok
movie{
id
title
actors {
id
name
}
year
}
}
}
curl -X POST -H "Content-Type: application/json" --data '{ "query": "{ actors { name } }" }' http://127.0.0.1:8000/graphql/
{"data":{"actors":[{"name":"Michael B. Jordan"},{"name":"Sylvester Stallone"},{"name":"Tom Hanks"}]}}
curl -X POST http://127.0.0.1:8000/graphql/ -H 'Content-Type: application/json' -d '{"query": "{actor(id:1){id name}}"}'
{"data":{"actor":{"id":"1","name":"Michael B. Jordan"}}}
GET方式:http://127.0.0.1:8000/graphql/?query={actor(id:2){id name}}
curl -X GET 'http://127.0.0.1:8000/graphql/?query=%7bactor(id%3a2)%7bid+name%7d%7d'