How do you handle graphql query and fragment in python?

Viewed 234

I am trying to query using the requests library but am having trouble. I suspect it's to do with the handling of the fragment but I'm not sure.

When I run the code I get Response 400. Here is my code:

import requests
import json

query = """query GetAxieTransferHistory($axieId: ID!, $from: Int!, $size: Int!) {
  axie(axieId: $axieId) {
    id
    transferHistory(from: $from, size: $size) {
      ...TransferRecords
      __typename
    }
    __typename
  }
}

fragment TransferRecords on TransferRecords {
  total
  results {
    from
    to
    timestamp
    txHash
    withPrice
    __typename
  }
  __typename
}"""

params = {
  "axieId": "9082310",
  "from": 0,
  "size": 1
}

url = 'https://axieinfinity.com/graphql-server-v2/graphql'

r = requests.post(url, json={"query": query, "params": params})
print(r.status_code)

Thanks in advance!

1 Answers

Try changing your params key to variables

r = requests.post(url, json={"query": query, "variables": params})

Related