Skip to main content

pygraphic

Status Version Tests Coverage

Client-side GraphQL query generator based on pydantic.

Why?

Working with GraphQL in Python seems simple... If you're fine with dictionaries, lack of autocompletion and unexpected errors.

Some tools allow you to generate Python code from GraphQL schemas. One of them, turms, even generates pydantic models from GQL documents. This approach can be problematic: queries are written in GraphQL, not Python, so the codebase you're actually working with is out of your control; and the main advantage of pydantic — data validation — is missing!

Workflow

Pygraphic is the opposite of turms:

  1. For each individual query, you define pydantic models that you want to request, optionally with validators and other configuration;

  2. Pygraphic converts those definitions to raw GraphQL documents (basically strings);

  3. Using a GraphQL or an HTTP client, you make requests with those documents and get back JSON responses;

  4. Pydantic converts those responses to instances of the defined models and validates them;

  5. You use the validated data, while enjoying autocompletion and type safety!

Roadmap

Pygraphic is in development stage. Some major features are missing or might work incorrectly. The API may change at any time.

See ROADMAP.md for the list of implemented/missing features.

Examples

Examples are kept in the /examples folder.
Queries that they're expected to produce are stored in the /golden_files folder.

Most examples are based on GitHub's GraphQL API. If you want to run them, you need to generate a personal access token and assign it to the environment variable called GITHUB_TOKEN (with VSCode, the best way to do this is to create a .env file in the project's root directory).

Here's the gist:

main.py

import os
import requests
from pygraphic import GQLQuery, GQLType

# Define data models
class License(GQLType):
    id: str
    name: str
    description: str

# Define query model
class GetAllLicenses(GQLQuery):
    licenses: list[License]

# Generate the GraphQL query string
query_str = GetAllLicenses.get_query_string()

# Make the request
TOKEN = os.environ["GITHUB_TOKEN"]
response = requests.post(
    url="https://api.github.com/graphql",
    json={
        "query": query_str,
    },
    headers={
        "Authorization": f"bearer {TOKEN}",
    },
)

# Parse the response
response_data = response.json().get('data')
if response_data is None:
    print('Query failed')
result = GetAllLicenses.parse_obj(response_data)

# Print the results
for license in result.licenses:
    print(license.name)

Generated query string

query GetAllLicenses {
  licenses {
    id
    name
    description
  }
}

Contribution

This project is developed on GitHub.

If you have any general questions or need help — you're welcome in the Discussions section.

If you encounter any bugs or missing features — file new Issues, but make sure to check the existing ones first.

If you want to solve an issue, go ahead and create a Pull Request! It will be reviewed and hopefully merged. Help is always appreciated.

License

Copyright © 2022, Dmitry Semenov. Released under the MIT license.

Metadata

Release files for pygraphic 0.5

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pygraphic 0.5
File Size Uploaded Provenance Actions
pygraphic-0.5.tar.gz 7.5 kB Without provenance Details for pygraphic-0.5.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for pygraphic 0.5
File Interpreter ABI Platform Actions
pygraphic-0.5-py3-none-any.whl Python 3 none any Details for pygraphic-0.5-py3-none-any.whl

Total release size: 15.6 kB

Release files / pygraphic-0.5.tar.gz

Download URL pygraphic-0.5.tar.gz
Size 7.5 kB
Tags Source
SHA-256 checksum
How to use checksums
a7f0184276db5bf9904594bfd16765b9139d8e0e9613b20e9bc8d6c692d35fef
BLAKE2b-256 checksum
How to use checksums
6363f8227201ccedc7e99bd2eac8fd4c396d45010fde354367d2a57799bd3933
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.2.0b3 CPython/3.8.10 Linux/5.15.0-1014-azure

Release files / pygraphic-0.5-py3-none-any.whl

Download URL pygraphic-0.5-py3-none-any.whl
Size 8.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
9dc7dedfdc622d932a47ae501a01043b1f8e6ebc18064f8641c1faa4630967ee
BLAKE2b-256 checksum
How to use checksums
7c7685f56f40e9a9433fa28acc8ed795395e048bbd50ff17287c48815ab11164
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.2.0b3 CPython/3.8.10 Linux/5.15.0-1014-azure

Security for pygraphic 0.5

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.5 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page