Skip to main content

Pythonic GraphQL client

Project description

pypi

  1. GraphQL
  2. Django queryset love
  3. __getattr__ abuse
  4. ???
  5. Profit!!!

What

py2graphql is a Python GraphQL client that makes GraphQL feel better to use. It almost feels like you’re using Django’s ORM.

Installation

pip install py2graphql

Example

This Python equates to the following 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
            }
          }
        }
      }
    }
  }
}

You can even use the library to do the HTTP requests:

from py2graphql import Client

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

It also supports Mutations:

from py2graphql import Client, Query

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

And multiple queries in a single request:

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
         }
       }
     }
   }
}

As well as GraphQL errors:

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}]}]

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for py2graphql, version 0.6.1
Filename, size File type Python version Upload date Hashes
Filename, size py2graphql-0.6.1-py2.py3-none-any.whl (7.3 kB) File type Wheel Python version 3.7 Upload date Hashes View hashes
Filename, size py2graphql-0.6.1.tar.gz (5.3 kB) File type Source Python version None Upload date Hashes View hashes

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page