【问题标题】:Compress GraphQL Query?压缩 GraphQL 查询?
【发布时间】:2018-03-14 19:29:30
【问题描述】:

我正在寻找一种标准方法来压缩 GraphQL 查询/响应以通过 MQTT 发送它。

我在想一些可以:

  • 删除多余的空格
  • 删除多余的新行(\n\r);
  • 压缩消息(zlib?)

我查看了用于 Python 的 Graphene 和其他 GraphQL 模块,但我还没有找到我想要的东西。

是否有我遗漏的术语或者这是我不应该做的事情?

【问题讨论】:

  • 谢天谢地,这可能与通过 AJAX 压缩任何 JSON 没有什么不同。

标签: python graphql


【解决方案1】:

我能想到的用 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))

这假定所有查询都已经是 unicode (Python 2) 或 str (Python 3) 对象。

运行 doctest 将通过。

【讨论】:

    猜你喜欢
    • 2010-10-30
    • 1970-01-01
    • 1970-01-01
    • 2021-10-31
    • 1970-01-01
    • 2012-10-10
    • 1970-01-01
    • 2020-06-24
    相关资源
    最近更新 更多