Configs2026年5月26日·1 分钟阅读

Graphene — GraphQL Framework for Python

A Python library for building GraphQL APIs using a code-first approach with classes and type annotations, integrating with Django, SQLAlchemy, and other popular frameworks.

Agent 就绪

Agent 可直接安装

这个资产可安装;Agent 先选择当前运行时、检查安装计划,再运行匹配命令。

Native · 98/100策略:允许
Agent 入口
任意 MCP/CLI Agent
类型
Skill
安装
Single
信任
信任等级:Established
入口
Graphene Overview
直接安装命令
npx -y tokrepo@latest install c062c818-58ba-11f1-9bc6-00163e2b0d79 --target codex

先 dry-run 确认安装计划,再运行此命令。

Introduction

Graphene is a code-first GraphQL framework for Python. Instead of writing schema definition language (SDL) files, you define your types and resolvers as Python classes. This approach lets you leverage Python's type system and IDE tooling while building GraphQL APIs. Graphene integrates with Django, SQLAlchemy, and other ORMs to auto-generate types from your data models.

What Graphene Does

  • Provides a Pythonic API for defining GraphQL schemas using classes and decorators
  • Generates GraphQL types from Django models and SQLAlchemy tables automatically
  • Supports queries, mutations, subscriptions, and custom scalars out of the box
  • Includes middleware support for authentication, logging, and error handling
  • Works with any ASGI/WSGI server through integration libraries like graphene-django

Architecture Overview

Graphene maps Python classes to GraphQL types at schema build time. An ObjectType subclass becomes a GraphQL object type, its class attributes become fields, and methods prefixed with resolve_ become resolvers. When a query arrives, Graphene's execution engine traverses the query AST, calls the appropriate resolver methods, and assembles the response. Under the hood, Graphene delegates to the graphql-core library (a Python port of the reference JavaScript implementation) for parsing, validation, and execution.

Self-Hosting & Configuration

  • Install the integration package for your framework: pip install graphene-django or pip install graphene-sqlalchemy
  • Add the GraphQL view to your URL configuration (e.g., Django's GraphQLView.as_view())
  • Define ObjectType classes that mirror your data models for automatic field generation
  • Enable the GraphiQL interactive IDE in development by passing graphiql=True to the view
  • Use DataLoader for batching and caching database queries to avoid N+1 performance issues

Key Features

  • Code-first schema design keeps types and resolvers co-located in Python files
  • Django and SQLAlchemy integrations auto-generate GraphQL types from ORM models
  • Relay support with connection types, global IDs, and node interfaces built in
  • Test utilities let you execute queries in unit tests without running a server
  • Extensible middleware pipeline for cross-cutting concerns like auth and rate limiting

Comparison with Similar Tools

  • Strawberry — newer Python GraphQL library using dataclasses and type hints; Graphene uses a class-based API and has a larger ecosystem
  • Ariadne — schema-first approach using SDL files; Graphene is code-first
  • Hasura — auto-generates a full GraphQL API from PostgreSQL; Graphene gives you manual control over resolvers
  • Apollo Server (JS) — the dominant GraphQL server in JavaScript; Graphene fills the same role for Python
  • FastAPI + Pydantic — REST-oriented; Graphene is purpose-built for GraphQL with type introspection

FAQ

Q: Should I use Graphene or Strawberry for a new project? A: Strawberry is a newer alternative with modern Python type hints. Graphene has a larger community and more integrations. Choose based on your preferred API style.

Q: Does Graphene support file uploads? A: Yes, through the graphene-file-upload package which implements the multipart request spec for GraphQL.

Q: How do I handle authentication? A: Use middleware to inspect the request context and attach user information. Resolvers can then check permissions before returning data.

Q: Can Graphene handle subscriptions? A: Yes. Graphene supports subscriptions via graphene-subscriptions or through integration with Django Channels for WebSocket transport.

Sources

讨论

登录后参与讨论。
还没有评论,来写第一条吧。

相关资产