Skip to main content

Pythonic GraphQL 客户端

项目描述

皮皮

  1. GraphQL

  2. Django 查询集的爱

  3. __getattr__滥用

  4. ???

  5. 利润!!!

什么

py2graphql 是一个 Python GraphQL 客户端,它让 GraphQL 更好用。几乎感觉就像你在使用 Django 的 ORM。

安装

pip install py2graphql

例子

此 Python 等同于以下 GraphQL。

from py2graphql import Query

Query().repository(owner='juliuscaeser', name='rome').pullRequest(number=2).values('title', 'url').commits(last=250).edges.node.commit.values('id', 'message', 'messageBody')
query {
  repository(owner: "juliuscaeser", name: "rome") {
    pullRequest(number: 2) {
      title
      url
      commits(last: 250) {
        edges {
          node {
            commit {
              id
              message
              messageBody
            }
          }
        }
      }
    }
  }
}

您甚至可以使用该库来执行 HTTP 请求:

from py2graphql import Client

headers = {
    'Authorization': 'token MY_TOKEN',
}
Client(url=THE_URL, headers=headers).query().repository(owner='juliuscaeser', name='rome').fetch()

它还支持突变:

from py2graphql import Client, Query

headers = {
    'Authorization': 'token MY_TOKEN',
}
client = Client(url=THE_URL, headers=headers)
mutation = Query(name='mutation', client=client)

以及单个请求中的多个查询:

from py2graphql import Client, Query

headers = {
    'Authorization': 'token MY_TOKEN',
}
query = Client(url=THE_URL, headers=headers).query().repository(owner='juliuscaeser', name='rome')
query.pullRequest(number=2).values('title', 'url')
query.releases(first=10).edges.node.values('name')
query.get_graphql()
query {
  repository(owner: "juliuscaeser", name: "rome") {
     pullRequest(number: 2) {
       title
       url
     }
     releases(first: 10) {
       edges {
         node {
           name
         }
       }
     }
   }
}

以及 GraphQL 错误:

from py2graphql import Client, Query

headers = {
    'Authorization': 'token MY_TOKEN',
}
result = Client(url=THE_URL, headers=headers).query().repository(owner='juliuscaeser', name='rome').fetch()
result._errors
[{'message': "Field 'repository' is missing required arguments: name", 'locations': [{'line': 7, 'column': 3}]}]

项目详情


下载文件

下载适用于您平台的文件。如果您不确定要选择哪个,请了解有关安装包的更多信息。

源分布

py2graphql-0.18.0.tar.gz (7.3 kB 查看哈希

已上传 source

内置分布

py2graphql-0.18.0-py3-none-any.whl (7.7 kB 查看哈希

已上传 py3