python – How to consume GraphQL api using spring boot Resttemplate

I want to consume a graphQL endpoint in a springboot application using resttemplate

Whenever,I am make a POST request with the query and variables I am always receiving the same error {“errors”:[{“message”:”Bad request”}]}

Below is the sample query, I want to send,

{
  "query": " query ($filter: SearchFilter!, $limit: Int!, $sorting: SearchSorting!) { search (filter: $filter, limit: $limit, sorting: $sorting)
  { cursor totalCount documents { id title products { name primaryCode } count Date type { ids } } } }",
  "variables": {
    "filter": {
      "date": {
        "dt": "2023-01-01"
      },
      "types": {
        "ids": [
          "1"
        ]
      },
      "products": {
        "include": [
          "aaa"
        ],
        "available": True
      },
      "category": {
        "ids": [
          "A12"
        ]
      }
    },
    "sorting": {
      "by": "DATE"
    },
    "limit": 500
  }
}

But from Python, I don’t have any issues on consuming the endpoint, and I am geting response.

Python code:

params = {
            "filter": {
      "date": {
        "dt": "2023-01-01"
      },
      "types": {
        "ids": [
          "1"
        ]
      },
      "products": {
        "include": [
          "aaa"
        ],
        "primaryOnly": True
      },
      "category": {
        "ids": [
          "A12"
        ]
      }
    },
    "sorting": {
      "field": "DATE",
      "direction": "DESC"
    },
    "limit": 500
}

query = '''
        query ($filter: SearchFilter!, $limit: Int!, $sorting: SearchSorting!) { search (filter: $filter, limit: $limit, sorting: $sorting)
  { cursor totalCount documents { id title products { name primaryCode } count releasedDt type { ids } } } }'''

result = json.loads(requests.post(uri, headers=getHeaders(), json={'query': query, "variables": params}).text)

Help to know how I can send query and variables using spring resttemplate.

Read more here: Source link