但是当我将其传递给我的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: '[email protected]', 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突变,出现错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
07-24 23:46