UPDATE method using Python requests

Similar to POST requests we can use the Update/PUT method to update specific or all fields in the schema. Here as well we need to create a mutation schema as previously. We will make the key-value maps for the attribute name and assign value to it. Thereafter we can mention the attributes we wish to fetch from the query after the update has been successful. 

We have used the post method again to update the attributes defined earlier, we have followed the schema in the documentation.

 

Python3




# Update/ POST Request
import requests
  
url = "https://fruits-api.netlify.app/graphql"
  
body = """
mutation {
  updateFruit(
    id: 1
    scientific_name: "mangiferaa"
    tree_name: "mangifera indicaa"
    fruit_name: "Mangooo"
    family: "Anacardiaceae"
    origin: "Indiana"
    description: "Mango is green"
    bloom: "Summer"
    maturation_fruit: "Mango"
    life_cycle: "101"
    climatic_zone: "humid"
  ) {
    id
    scientific_name
    tree_name
    fruit_name
    description
  }
}
"""
response = requests.post(url=url, json={"query": body})
print("response status code: ", response.status_code)
if response.status_code == 200:
    print("response : ", response.content)


Output:

 

Here, we have updated the fruit attributes, using the updateFruit mutation, we have assigned the values and parsed the mutation. For the specific design of the API, you might also try the requests.update method to update the record if the post method does not work.

GET and POST Requests in GraphQL API using Python requests

In this article, we will be understanding how to write GET and POST requests to GRAPHQL APIs using the Python request module. Dealing with GraphQL API is a bit different compared to the simple REST APIs. We have to parse in a query that involves parsing the GraphQL queries in the request body.

Similar Reads

What are Requests in Python

Requests is a python package/library which helps in working with HTTP requests using Python. We can very conveniently parse the body, headers, and other parameters to a request using methods provided by the requests library like, GET, PUT, POST, DELETE, etc. Using the requests module we will be parsing the queries to a GraphQL API....

What is GraphQL API In Python

GraphQL API is a query language for fetching data from a database. It has a different approach than traditional REST APIs. Using GraphQL API, we can fetch the only required information, this avoids the drawbacks of under fetching and over fetching compared to the REST APIs. This type of API allows us to fetch very fine-tuned data in a defined schema so that the application can consume it, only the data which is required will be fetched using the GraphQl queries to the API....

GET method using Python requests

We can send a GET request to the given GraphQL API with the get method provided in the requests library which we imported.  Here due to the design of the API we have to use the POST method to fetch the results but it would depend on the API, we will dive into that later....

POST method using Python requests

...

UPDATE method using Python requests

...

DELETE method using Python requests

We can also post data to a GraphQl API using the requests library. We simply have to follow the schema and parse the data correctly. We are using the Fruit API again to post the data to it. So, we have a different type of schema, instead, we have a mutation to write instead of a query. We follow up the documentation to the right to get the schema....