Skip to main content

A framework for building Python GraphQL APIs.

Project description

GraphQL-API for Python

PyPI version Python versions License: MIT Coverage Pipeline

📚 Documentation | 📦 PyPI | 🔧 GitHub


A powerful and intuitive Python library for building GraphQL APIs, designed with a code-first, decorator-based approach.

graphql-api simplifies schema definition by leveraging Python's type hints, dataclasses, and Pydantic models, allowing you to build robust and maintainable GraphQL services with minimal boilerplate.

Key Features

  • Decorator-Based Schema: Define your GraphQL schema declaratively using simple and intuitive decorators.
  • Type Hinting: Automatically converts Python type hints into GraphQL types.
  • Implicit Type Inference: Automatically maps Pydantic models, dataclasses, and classes with fields - no explicit decorators needed.
  • Pydantic & Dataclass Support: Seamlessly use Pydantic and Dataclass models as GraphQL types.
  • Asynchronous Execution: Full support for async and await for high-performance, non-blocking resolvers.
  • Apollo Federation: Built-in support for creating federated services.
  • Subscriptions: Implement real-time functionality with GraphQL subscriptions.
  • Middleware: Add custom logic to your resolvers with a flexible middleware system.
  • Relay Support: Includes helpers for building Relay-compliant schemas.

Installation

pip install graphql-api

Quick Start

Create a simple GraphQL API in just a few lines of code.

# example.py
from graphql_api.api import GraphQLAPI

# 1. Initialize the API
api = GraphQLAPI()

# 2. Define your root type with decorators
@api.type(is_root_type=True)
class Query:
    """
    The root query for our amazing API.
    """
    @api.field
    def hello(self, name: str = "World") -> str:
        """
        A classic greeting.
        """
        return f"Hello, {name}!"

# 3. Define a query
graphql_query = """
    query Greetings {
        hello(name: "Developer")
    }
"""

# 4. Execute the query
if __name__ == "__main__":
    result = api.execute(graphql_query)
    print(result.data)

Running this script will produce:

$ python example.py
{'hello': 'Hello, Developer'}

Examples

Using Pydantic Models

Leverage Pydantic for data validation and structure. graphql-api will automatically convert your models into GraphQL types.

from pydantic import BaseModel
from typing import List
from graphql_api.api import GraphQLAPI

class Book(BaseModel):
    title: str
    author: str

@api.type(is_root_type=True)
class BookAPI:
    @api.field
    def get_books(self) -> List[Book]:
        return [
            Book(title="The Hitchhiker's Guide to the Galaxy", author="Douglas Adams"),
            Book(title="1984", author="George Orwell"),
        ]

api = GraphQLAPI()

graphql_query = """
    query {
        getBooks {
            title
            author
        }
    }
"""

result = api.execute(graphql_query)
# result.data will contain the list of books

Asynchronous Resolvers

Define async resolvers for non-blocking I/O operations.

import asyncio
from graphql_api.api import GraphQLAPI

api = GraphQLAPI()

@api.type(is_root_type=True)
class AsyncAPI:
    @api.field
    async def fetch_data(self) -> str:
        await asyncio.sleep(1)
        return "Data fetched successfully!"

# To execute async queries, you'll need an async executor
# or to run it within an async context.
async def main():
    result = await api.execute("""
        query {
            fetchData
        }
    """)
    print(result.data)

if __name__ == "__main__":
    asyncio.run(main())

Mutations with Dataclasses

Use dataclasses to define the structure of your data, and mark fields as mutable to automatically separate them into the GraphQL Mutation type.

from dataclasses import dataclass
from graphql_api.api import GraphQLAPI

@dataclass
class User:
    id: int
    name: str

# A simple in-memory database
db = {1: User(id=1, name="Alice")}

api = GraphQLAPI()

@api.type(is_root_type=True)
class Root:
    @api.field
    def get_user(self, user_id: int) -> User:
        return db.get(user_id)

    @api.field(mutable=True)
    def add_user(self, user_id: int, name: str) -> User:
        new_user = User(id=user_id, name=name)
        db[user_id] = new_user
        return new_user

GraphQL automatically separates queries and mutations - you don't need separate classes. Fields marked with mutable=True are placed in the Mutation type, while regular fields go in the Query type. Fields with AsyncGenerator return types are automatically detected as subscriptions. This automatic mapping means you can define all your operations in a single class and let graphql-api handle the schema organization for you.

Related Projects

  • graphql-http - Serve your API over HTTP with authentication and GraphiQL
  • graphql-db - SQLAlchemy integration for database-backed APIs
  • graphql-mcp - Expose your API as MCP tools for AI agents

See the documentation for advanced schema patterns, federation, remote GraphQL, and more.

Documentation

Visit the official documentation for comprehensive guides, tutorials, and API reference.

Key Topics

Running Tests

To contribute or run the test suite locally:

# Install dependencies
pip install pipenv
pipenv install --dev

# Run tests
pipenv run pytest

Project details


Release history Release notifications | RSS feed

Download files

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

Source Distribution

graphql_api-1.6.4.tar.gz (250.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

graphql_api-1.6.4-py3-none-any.whl (69.0 kB view details)

Uploaded Python 3

File details

Details for the file graphql_api-1.6.4.tar.gz.

File metadata

  • Download URL: graphql_api-1.6.4.tar.gz
  • Upload date:
  • Size: 250.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.10.2 {"installer":{"name":"uv","version":"0.10.2","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for graphql_api-1.6.4.tar.gz
Algorithm Hash digest
SHA256 0208bf6ec66919e645c2953a1ed650dad644bac182a62bf234218c3e9ba3c389
MD5 f311b279bc88e6bdad0b93deb1a1cd2b
BLAKE2b-256 af6d352c171781d2225800ea44f3cf87885b49f4ecafe31f8b3b0fb34cdeeff3

See more details on using hashes here.

File details

Details for the file graphql_api-1.6.4-py3-none-any.whl.

File metadata

  • Download URL: graphql_api-1.6.4-py3-none-any.whl
  • Upload date:
  • Size: 69.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.10.2 {"installer":{"name":"uv","version":"0.10.2","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for graphql_api-1.6.4-py3-none-any.whl
Algorithm Hash digest
SHA256 38d060d476966d58b7fbfe064ddc328ab3ef2ea254301e5695e0506c72b25249
MD5 55fa88a1525e311d80385dfcb897b620
BLAKE2b-256 6fab2a538a3c8e7e10b07429ad4494a11029af34a65f871fd729bcd55529de04

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page