Skip to content

Vicky-kr/graphQL-wrapper-python

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 

Repository files navigation

GraphQL-wrapper-python

This is to demonstrate how to convert a simple REST API to GraphQL in python using Graphene

For this project we are using {JSON} Placeholder todo list api

If we make a get request to this link we get response like

[
  {
    "userId": 1,
    "id": 1,
    "title": "delectus aut autem",
    "completed": false
  },
  {
    "userId": 1,
    "id": 2,
    "title": "quis ut nam facilis et officia qui",
    "completed": false
  }
  ....
]
Since there is no functionality to get a certain data such as first 10 datas or only the title and id of the todo tasks so we are left with either ignore those data or make an extra endpoint. But using GraphQL if we want to get a particular data we make query as shown below to get the same result :
query myFirstQuery {
  todos {
    userId
    id
    title
    completed
  }
}
Additionally if we want the first 2 tasks and only want the title and status of the task we make query as shown below :
query mySecondQuery {
    todos (size:2){
        title
        completed
    }
}

In return we get response like this

[
  {
    "title": "delectus aut autem",
    "completed": false
  },
  {
    "title": "quis ut nam facilis et officia qui",
    "completed": false
  }
]

Advantages of GraphQL over REST

- We only get those data what we query for
- Helps in avoiding version control and creation multiple endpoints such as "baseURL/api/v2/"
- Takes the shortest distance to retrieve the data hence faster than REST api
- Since only required data is sent so it also increases the bandwidth of the websites (using GraphQL)

About

This is to demonstrate how to convert a simple REST API to graphQL API in python using graphene

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages