我正在寻找一种压缩 GraphQL 查询/响应以通过 MQTT 发送的标准方法。
我正在考虑一些可以:
\n, \r);我查看了 Graphene 和其他 Python 的 GraphQL 模块,但我还没有找到我想要的东西。
我是否缺少术语或者这是我不应该做的事情?
我能想到的在 Python 中压缩 GraphQL 查询的最简单方法是:
import shlex
query_with_strings = """
query someQuery {
Field(
search: "string with spaces"
) {
foo
}
}
"""
def compress_graphql(q):
"""Compress a GraphQL query by removing unnecessary whitespace.
>>> compress_graphql(query_with_strings)
u'query someQuery { Field( search: "string with spaces" ) { foo } }'
"""
return u' '.join(shlex.split(q, posix=False))
Run Code Online (Sandbox Code Playgroud)
这假设所有查询都已经是unicode(Python 2) 或str(Python 3) 对象。
运行 doctest 就会通过。