Objective of this repo is to build graphql server with fast api and connecting it to mySQL database.
GraphQL is a query language for APIs and a runtime for fulfilling those queries with your existing data. The difference between Rest and GraphQL is that for RestAPi you need to define endpoint for each response, for example
RestAPI
- Create
/users
endpoint to get user info - Create
/posts/{user_id}
endpoint to get posts by that user - Create
/comments/{post_id}/{user_id}
to get comments related to the post and user
And other endpoints based on the requirements
Where as GraphQL you can just build one endpoint and describe the attributes just like a SQL query and get the response, and in the same endpoint you can get users, post and comments data for that user, example below:
Amazing python lib for building API and even web apps check it out here FastAPI
- Clone this repo
- Create new env e.g
conda create --name gfastpy37
- Run
pip install -r requirements.txt
- Run
uvicorn main:app --reload
- Open
127.0.0.1:8000/graphql
mutation createUser {
createUser(userDetails: {name: "Fadil", address: "Somewhere on earth", phoneNumber: "0193888583", sex: "male"}) {
id
name
address
phoneNumber
sex
posts {
body
comments {
body
}
}
}
}
mutation createPost {
createPost(postDetails: {userId: 1, title: "My first blog", body: "This is a blog about myself"}) {
id
title
body
}
}
mutation createComment {
createComment(commentDetails: {
userId: 1,
postId: 1,
body: "Graphql looks interesting"
}) {
id
postId
body
}
}
query getAllUsers {
listUsers {
id
name
address
sex
posts {
title
body
comments {
body
}
}
}
}
query getUser {
getSingleUser(userId: 1) {
id
name
address
phoneNumber
sex
posts{
title
body
}
comments {
body
}
}
}
query getUserOnly {
getSingleUser(userId: 422) {
name
}
}