繁体   English   中英

压缩GraphQL查询?

[英]Compress GraphQL Query?

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

我在想可以做到的事情:

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

我看了一下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))

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

运行doctest将通过。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM