【问题标题】:Handling Graphql Mutation With Python Requests使用 Python 请求处理 Graphql 突变
【发布时间】:2020-09-02 00:32:46
【问题描述】:

我正在尝试使用 waveapps API。看来我没有正确地进行变异操作。

我想创建一个Transaction via WaveApps API。以下是我的代码:

query = """ mutation ($input: MoneyTransactionCreateInput!) {moneyTransactionCreate(input: $input) {moneyTransaction {id externalId date description anchor{accountId amount direction} lineItems[{accountId amount balance}]}}}"""

moneyTransaction = {'businessId':'5ODAtNzQ3OS00ZGQ4LTg5NWYtMzU4ZWNiNDNmMTI2', 
'externalId':'21', 'date':'2020-05-16', 'description':'my money', 'anchor':{'accountId':'1', 
'amount':'15.00', 'direction':'DEPOSIT'},
'lineItems':[{'accountId':'1', 'amount':'15.00', 'balance':'CREDIT'}]}

variables = {'input': moneyTransaction}

rex = requests.post(wave_url, json={'query':query, 'variables':variables}, headers=after_headers)

我收到了这个错误:

'{"errors":[{"extensions":{"id":"e6b88a8d-a5f8-4331-80db-191dbb319690","code":"GRAPHQL_PARSE_FAILED"},"message":"Syntax Error: Expected Name, found [","locations":[{"line":1,"column":183}]}]}\n'

我遗漏了一些东西,但无法弄清楚问题所在。我是 Graphql 的新手。

【问题讨论】:

    标签: python python-requests graphql


    【解决方案1】:

    您的查询中有多余的方括号。方括号仅在指定 List 类型时使用——从不需要在选择集中使用它们。

    mutation ($input: MoneyTransactionCreateInput!) {
      moneyTransactionCreate(input: $input) {
        moneyTransaction {
          id
          externalId
          date
          description
          anchor {
            accountId
            amount
            direction
          }
          lineItems {
            accountId
            amount
            balance
          }
        }
      }
    }
    

    【讨论】:

      【解决方案2】:

      这也有效。

      mutation ($inputMoneyTransactionCreate: MoneyTransactionCreateInput!) {
        moneyTransactionCreate(input: $inputMoneyTransactionCreate) {
          didSucceed
          inputErrors {
            code
            message
            path
          }
        }
      }
      

      变量改为:

      variables = {'inputMoneyTransactionCreate': moneyTransaction}
      

      【讨论】:

        猜你喜欢
        • 2019-04-07
        • 2020-02-16
        • 2019-02-08
        • 2021-04-15
        • 2020-07-16
        • 1970-01-01
        • 2020-10-20
        • 2021-07-08
        • 2021-03-11
        相关资源
        最近更新 更多