从我的python代码进行graphQL突变,出现错误 [英] Making a graphQL mutation from my python code, getting error

查看:75
本文介绍了从我的python代码进行graphQL突变,出现错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从python对我的Shopify商店进行突变.我是graphQL的新手,我已经可以使用graphiQL进行突变,但是我不确定如何直接从我的代码中进行突变.

这是我的make查询文件,它已经成功完成了简单的查询

 `导入请求def make_query(自我,查询,网址,标头):"返回查询响应"request = requests.post(URL,json = {'query':query},headers = headers)如果request.status_code == 200:返回request.json()别的:引发异常(查询无法通过返回{}.{}的代码来运行.".format(request.status_code,query))` 

现在在graphiQL中起作用的突变示例是:

"变异{customerCreate(输入:{email:'wamblamkazam@send22u.info',密码:'password'}){userErrors {字段消息} customer {id}}}"

但是当我将其传递给我的make_query函数时,会出现此错误

  {'errors':[{'message':'解析'\'上的错误(错误)出现在[1,41]','locations':[{'line':1,'column':41}]}]}} 

我该如何解决?我所做的变异之一也是使用变量,而我却无法从我的代码中找到如何直接执行此操作的示例

解决方案

GraphQl提供了一种以JSON发送数据的方法.您可以在查询中使用变量,并将JSON对象作为变量值发送:

  def make_query(自身,查询,变量,URL,标题):"做出查询响应"request = request.post(URL,json = {'query':查询,'variables':变量},headers = headers)如果request.status_code == 200:返回request.json()别的:引发异常(查询无法通过返回{}.{}的代码来运行.".format(request.status_code,查询)) 

查询如下:

  query ="变异CreateCustomer($ input:CustomerInput){customerCreate(customerData:$ input){顾客{名称}}}"变量= {'输入':客户} 

您还可以使用一个刚刚发现的小型库,如下所示:

  client = GraphQLClient('http://127.0.0.1:5000/graphql')查询="变异CreateCustomer($ input:CustomerInput){customerCreate(customerData:$ input){顾客{名称}}}"变量= {'输入':客户}client.execute(查询,变量) 

I am trying to make a mutation to my Shopify store from python. I am new to graphQL, I have been able to make the mutation using graphiQL but I am not certain how to do it directly from my code.

This is my make query file, it has worked successfully for a simple query

`import requests 
 def make_query(self, query, url, headers):
    """
    Return query response
    """
    request = requests.post(url, json={'query': query}, headers=headers)
    if request.status_code == 200:
        return request.json()
    else:
        raise Exception("Query failed to run by returning code of {}. {}".format(request.status_code, query))`

Now an example of the mutation that worked in graphiQL is this:

"mutation {customerCreate(input: {email: 'wamblamkazam@send22u.info', password: 'password'}) {userErrors { field message}customer{id}}}"

But when I pass it into my make_query function it gives this error

{'errors': [{'message': 'Parse error on "\'" (error) at [1, 41]', 'locations': [{'line': 1, 'column': 41}]}]}

How do I fix this? Also one of the mutations I am making uses variables, and I haven't been able to find an example of how to do this directly from my code

解决方案

GraphQl gives a way to send data in JSON. You can use variables in the query, and send the JSON object as the variable value:

def make_query(self, query, variables, url, headers):
    """
    Make query response
    """
    request = request.post(url, json={'query': query, 'variables': variables}, headers=headers)
    if request.status_code == 200:
        return request.json()
    else:
        raise Exception("Query failed to run by returning code of {}. {}".format(request.status_code, query))

With the query looking like this:

query = """
    mutation CreateCustomer($input:CustomerInput){
        customerCreate(customerData: $input){
            customer{
                name
            }
        }
    }
"""
variables = {'input': customer}

You can also use a small library a just found to look like this:

client = GraphQLClient('http://127.0.0.1:5000/graphql')

query = """
mutation CreateCustomer($input:CustomerInput){
    customerCreate(customerData: $input){
        customer{
            name
        }
    }
}
"""

variables = {'input': customer}

client.execute(query, variables)

这篇关于从我的python代码进行graphQL突变,出现错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆