Skip to main content

pygraphic

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!

Release Checklist

Pygraphic is in development stage. Major features might either be missing or work incorrectly. The API may change at any time.

  • Basic queries
  • Queries with parameters
  • Custom scalars (not needed, comes with pydantic)
  • Conversion between camelCase and snake_case
  • Mutations
  • Subscriptions
  • Tests
  • Stable codebase

Example

Server schema

type User {
  id: int!
  username: String!
  friends: [User!]!
}

get_all_users.py

from __future__ import annotations
from pygraphic import GQLQuery, GQLType

class User(GQLType):
    id: int
    username: str
    friends: list[UserFriend]

class UserFriend(GQLType):
    id: int
    username: str

class GetAllUsers(GQLQuery):
    users: list[User]

main.py

import requests
from .get_all_users import GetAllUsers

# Generate query string
gql = GetAllUsers.get_query_string()

# Make the request
url = "http://127.0.0.1/graphql"
response = requests.post(url, json={"query": gql})

# Extract data from the response
json = response.json()
data = json.get("data")
if data is None:
    raise Exception("Query failed", json.get("error"))

# Parse the data
result = GetAllUsers.parse_obj(data)

# Print validated data
for user in result.users:
    print(user.username)
    print(user.friends)

Generated query string

query GetAllUsers {
  users {
    id
    username
    friends {
      id
      username
    }
  }
}

See more in /examples.

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.4

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.4
File Size Uploaded
pygraphic-0.4.tar.gz 6.0 kB Details

Built distribution (wheel)

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

Total release size: 12.9 kB

Release files / pygraphic-0.4.tar.gz

Download URL pygraphic-0.4.tar.gz
Size 6.0 kB
Tags Source
SHA-256 checksum
How to use checksums
084a50697fe8295e5cb5a9cbc43837a894aef68cf3f8eb7bf13a455a515611a3
BLAKE2b-256 checksum
How to use checksums
2131b7153639c230cd00aee90f64cd044445e326d7ed2476c7be713acbf69a5e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.9.13

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

Download URL pygraphic-0.4-py3-none-any.whl
Size 6.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
3f7f1b68ddeac1b68ead6e63b3970b6bb1571522ad8edd56d4553dc201ac18a9
BLAKE2b-256 checksum
How to use checksums
73391b9ce27e0bae0d13eb2a6e3eac7bbb2b35c1e4ed4d0450c457e85d55b55b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.9.13

Release history Release notifications | RSS feed

0.5

2 release files

This release

0.4 This release

2 release files

0.3

2 release files

0.2

2 release files

0.1

2 release files

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