2

Below is a graphql query that retrieves user details like id,username,city,pincode by passing a variable that matches with a cityname called 'osaka' :

query = """ {
        userinfo  (cityName: "osaka") 
        {
             id
             user_name
             city
             pincode



        }
    }"""

this query is being posted to an external graphql api using requests module of python

url = 'http://192.168.200.58:8010/api/userdetails/v1/users'
r = requests.post(url, query,headers=headers, auth=HTTPBasicAuth('api', 'api123'))
json_data = json.loads(r.text)

how to query this api by passing cityname dynamically , i tried the below code but that did not work

      json = {'query': """ {
            userinfo  ($cityname: str)
            {
                id
                user_name
                city
                pincode
            }
        }""",
        "variables":{"cityName":"LA"}
 url = 'http://192.168.200.58:8010/api/userdetails/v1/users'
 r = requests.post(url, json=json,headers=headers, auth=HTTPBasicAuth('api', 'api123'))
 json_data = json.loads(r.text)
1
  • Use GraphQL variables to provide dynamic values for arguments as shown in the linked post. Commented Feb 13, 2020 at 14:36

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.