GraphQL и Python с JSon - синтаксическая ошибка с неэкранированными символами - PullRequest
0 голосов
/ 21 сентября 2018

Я пытаюсь отправить мутацию GraphQL и получаю синтаксическую ошибку, которую не могу понять.Я видел, что была ошибка, касающаяся экранированных / неэкранированных персонажей.Я пытался избежать, двойные кавычки, одинарные кавычки, без кавычек.Кажется, я не могу обойти эту проблему.Трассировка по коду.

# http headers for api call
headers = {
    'accept': "application/json",
    'content-type':"application/json",
    'authorization': "bearer " + token,
}

# create inventory variable for mutation 
# will convert the csv to the json input in production
inventory:[{"supplier_id":24522,"supplier_part_number":"1-1002-9-SN","quantity_on_hand":5,"item_next_availability_date":"05-01-2018T00:00:00", "discontinued":true}]


# payload of the json query to pass to the API
#GraphQL Query to pull in Purchase Order Data 
payload = '''
{"query":"mutation save_inventory($inventory: [inventoryInput]!) {
  inventory {
    save(inventory: $inventory, dry_run: true) {
      handle
    }
  }
}"}
'''

# send API call and assign response to variable
response = requests.post(api_url, data=payload, headers=headers)

Ошибка, которую я не могу понять ниже.

  {"errors":[{"message":"Syntax Error GraphQL (1:1) Unexpected <EOF>\n\n1: \n   ^\n","category":"graphql","locations":[{"l
    ine":1,"column":1}]}]}

1 Ответ

0 голосов
/ 21 сентября 2018

Мой Python ржавый, но я думаю, что вы хотите использовать json вместо data.Вы также не передаете указанную вами переменную inventory.Попробуйте что-то вроде:

json = {
  "query": '''   
    mutation save_inventory($inventory: [inventoryInput]!) {
      inventory {
        save(inventory: $inventory, dry_run: true) {
        handle
      }
    }
  }
  ''',
  "variables": {
    "inventory": inventory
  }
}

response = requests.post(api_url, json=json, headers=headers)
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...