【问题标题】:Flask graphql query with '&' returns 'Syntax Error GraphQL'带有“&”的 Flask graphql 查询返回“语法错误 GraphQL”
【发布时间】:2020-09-12 22:43:23
【问题描述】:

我有一个非常简单的烧瓶 graphql 应用程序,一切都按照我的方式工作,我可以从浏览器调用它并返回给定的数据。

但是,如果我发送一个包含 '&' 的字符串,我总是会收到消息“Syntax Error GraphQL (1:22) Unterminated string \n\n1: (...) \n ^\n”

浏览器将给定的字符串拆分为参数。

有没有办法解决这个问题?

#!/usr/bin/env python3
import flask
import flask_graphql
import graphene

app = flask.Flask('graphql-test')

class Query(graphene.ObjectType):
    helloz = graphene.String(text=graphene.String())

    def resolve_helloz(self, info, text):
        return text

@app.route('/')
def index():
    # Create graphql view with the authentication passed as context
    return flask_graphql.GraphQLView.as_view(
            'index', 
            schema=graphene.Schema(query=Query), 
            graphiql=False
        )()

if __name__ == '__main__':
    # Run application
    app.run(host='127.0.0.1', port='8001')

【问题讨论】:

  • 您是否将查询作为 URL 参数发送?
  • 是的,我调用像 127.0.0.1:8001/?query={helloz(text:"test&test")} 这样的 URL 并得到一个错误

标签: flask graphql graphene-python flask-graphql


【解决方案1】:

如果您将查询作为查询参数发送,则需要转义任何具有特殊含义的字符,例如 &?& 用于分隔单个参数,因此如果不对其进行转义,query 参数最终会被解释为 {helloz(text:"test——这不是有效的 GraphQL 语法。您应该发送:

127.0.0.1:8001?query={helloz(text:"test%26test")}

【讨论】:

  • 我对数据没有影响。我只是在服务器站点上。
  • 服务器没问题。如果要作为 URL 参数发送,客户端需要正确格式化查询。您还可以在 POST 请求的正文中发送查询,无论如何这是推荐的方式。
猜你喜欢
  • 2017-07-27
  • 1970-01-01
  • 2021-10-04
  • 2018-08-16
  • 2019-04-18
  • 2019-06-14
  • 2020-01-07
  • 2019-06-11
  • 2018-12-31
相关资源
最近更新 更多