Top Related Projects
GraphQL framework for Python
A GraphQL library for Python that leverages type annotations 🍓
A GraphQL client in Python
The little ASGI framework that shines. 🌟
Quick Overview
Ariadne is a Python library for building GraphQL APIs. It provides a flexible and extensible way to define and implement GraphQL schemas, resolvers, and middleware. Ariadne aims to make it easy to build and deploy GraphQL APIs, with a focus on developer experience and performance.
Pros
- Flexibility: Ariadne allows you to define your GraphQL schema using a variety of methods, including code-first, schema-first, and a mix of both approaches.
- Extensibility: Ariadne's plugin system makes it easy to add custom functionality, such as authentication, caching, and more.
- Performance: Ariadne is designed to be fast and efficient, with support for asynchronous resolvers and batching.
- Developer Experience: Ariadne provides a clean and intuitive API, with helpful error messages and debugging tools.
Cons
- Relative Newness: Ariadne is a relatively new library, and may not have the same level of community support and documentation as some older GraphQL libraries.
- Limited Ecosystem: While Ariadne has a growing ecosystem of plugins and integrations, it may not have the same breadth of available tools and libraries as some other GraphQL solutions.
- Learning Curve: Ariadne's flexibility and extensibility can also mean a steeper learning curve for developers new to the library.
- Performance Tradeoffs: While Ariadne is designed to be performant, the flexibility and extensibility of the library may come with some performance tradeoffs in certain use cases.
Code Examples
Here are a few examples of how to use Ariadne:
- Defining a GraphQL Schema:
from ariadne import QueryType, make_executable_schema
type_defs = """
type Query {
hello: String!
}
"""
query = QueryType()
@query.field("hello")
def resolve_hello(_, info):
return "Hello, world!"
schema = make_executable_schema(type_defs, [query])
- Integrating with a Web Framework (FastAPI):
from fastapi import FastAPI
from ariadne.asgi import GraphQL
app = FastAPI()
schema = make_executable_schema(type_defs, [query])
app.add_route("/graphql", GraphQL(schema, debug=True))
- Implementing Mutations:
from ariadne import MutationType
mutation = MutationType()
@mutation.field("createTodo")
def resolve_create_todo(_, info, text):
todo = Todo(text=text)
todo.save()
return todo
schema = make_executable_schema(type_defs, [query, mutation])
- Using Ariadne Plugins:
from ariadne import QueryType, make_executable_schema
from ariadne.contrib.tracing.apollo import ApolloTracingExtension
query = QueryType()
@query.field("hello")
def resolve_hello(_, info):
return "Hello, world!"
schema = make_executable_schema(type_defs, [query], extensions=[ApolloTracingExtension()])
Getting Started
To get started with Ariadne, you can follow these steps:
- Install Ariadne using pip:
pip install ariadne
- Define your GraphQL schema using the Ariadne API:
from ariadne import QueryType, make_executable_schema
type_defs = """
type Query {
hello: String!
}
"""
query = QueryType()
@query.field("hello")
def resolve_hello(_, info):
return "Hello, world!"
schema = make_executable_schema(type_defs, [query])
- Integrate your Ariadne schema with a web framework, such as FastAPI:
from fastapi import FastAPI
from ariadne.asgi import GraphQL
app = FastAPI()
app.add_route("/graphql", GraphQL(schema, debug=True))
if __:
app.run()
- Start your application and visit the GraphQL playground at
Competitor Comparisons
GraphQL framework for Python
Pros of Graphene
- Graphene has a larger community and more contributors, which can lead to faster development and more support.
- Graphene provides a more comprehensive set of features, including support for mutations, subscriptions, and more.
- Graphene has better documentation and more examples available, which can make it easier to get started.
Cons of Graphene
- Ariadne has a simpler and more lightweight API, which may be preferred by some developers.
- Ariadne is more focused on performance and scalability, which may be important for certain use cases.
- Ariadne has a more modern and flexible architecture, which may be more future-proof.
Code Comparison
Graphene:
import graphene
class Query(graphene.ObjectType):
hello = graphene.String()
def resolve_hello(self, info):
return "World"
schema = graphene.Schema(query=Query)
Ariadne:
from ariadne import QueryType, make_executable_schema
type_defs = """
type Query {
hello: String!
}
"""
query = QueryType()
@query.field("hello")
def resolve_hello(*_):
return "World"
schema = make_executable_schema(type_defs, [query])
A GraphQL library for Python that leverages type annotations 🍓
Pros of Strawberry
- Strawberry provides a more Pythonic and intuitive API for defining GraphQL schemas and resolvers, with the use of Python type annotations and decorators.
- Strawberry has a simpler and more lightweight codebase compared to Ariadne, which can be beneficial for smaller projects or those with specific performance requirements.
- Strawberry's documentation is generally considered to be more user-friendly and easier to navigate.
Cons of Strawberry
- Strawberry has a smaller community and ecosystem compared to Ariadne, which may mean fewer available resources, plugins, and third-party integrations.
- Strawberry's performance may not be as optimized as Ariadne's, especially for larger and more complex GraphQL schemas.
- Strawberry's error handling and debugging capabilities may not be as robust as Ariadne's.
Code Comparison
Ariadne (defining a query resolver):
@query_field("hello")
def resolve_hello(_, info):
return "Hello, world!"
Strawberry (defining a query resolver):
@strawberry.type
class Query:
@strawberry.field
def hello(self) -> str:
return "Hello, world!"
Both examples demonstrate how to define a simple "hello" query resolver, but the Strawberry version uses a more Pythonic approach with type annotations and decorators.
A GraphQL client in Python
Pros of graphql-python/gql
- Supports both GraphQL query and mutation operations
- Provides a simple and intuitive API for interacting with GraphQL servers
- Includes built-in support for handling GraphQL errors and responses
Cons of graphql-python/gql
- Lacks some of the advanced features and flexibility offered by Ariadne
- May have a steeper learning curve for developers unfamiliar with the library
- Limited documentation and community support compared to Ariadne
Code Comparison
Ariadne:
from ariadne import QueryType, make_executable_schema
query = QueryType()
@query.field("hello")
def resolve_hello(_, info):
return "Hello, world!"
schema = make_executable_schema(type_defs, [query])
gql:
from gql import gql, Client
from gql.transport.requests import RequestsHTTPTransport
transport = RequestsHTTPTransport(url="https://your-graphql-server.com/graphql")
client = Client(transport=transport, fetch_schema_from_transport=True)
query = gql(
"""
query {
hello
}
"""
)
result = client.execute(query)
print(result["hello"])
The little ASGI framework that shines. 🌟
Pros of Starlette
- Starlette is a lightweight and fast ASGI web framework, making it a good choice for building high-performance web applications.
- Starlette has a modular design, allowing developers to easily integrate third-party libraries and components.
- Starlette provides built-in support for WebSockets, GraphQL, and other modern web technologies.
Cons of Starlette
- Starlette has a smaller community compared to Ariadne, which may result in fewer available resources and third-party packages.
- Starlette's documentation, while comprehensive, may not be as user-friendly as Ariadne's.
Code Comparison
Starlette:
from starlette.applications import Starlette
from starlette.responses import JSONResponse
app = Starlette()
@app.route("/")
async def homepage(request):
return JSONResponse({"message": "Hello, World!"})
Ariadne:
from ariadne import QueryType, make_executable_schema
type_defs = """
type Query {
hello: String!
}
"""
query = QueryType()
@query.field("hello")
def resolve_hello(*_):
return "Hello, World!"
schema = make_executable_schema(type_defs, query)
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
Ariadne
Ariadne is a Python library for implementing GraphQL servers.
- Schema-first: Ariadne enables Python developers to use schema-first approach to the API implementation. This is the leading approach used by the GraphQL community and supported by dozens of frontend and backend developer tools, examples, and learning resources. Ariadne makes all of this immediately available to you and other members of your team.
- Simple: Ariadne offers small, consistent and easy to memorize API that lets developers focus on business problems, not the boilerplate.
- Open: Ariadne was designed to be modular and open for customization. If you are missing or unhappy with something, extend or easily swap with your own.
Documentation is available here.
Features
- Simple, quick to learn and easy to memorize API.
- Compatibility with GraphQL.js version 15.5.1.
- Queries, mutations and input types.
- Asynchronous resolvers and query execution.
- Subscriptions.
- Custom scalars, enums and schema directives.
- Unions and interfaces.
- File uploads.
- Defining schema using SDL strings.
- Loading schema from
.graphql
,.gql
, and.graphqls
files. - WSGI middleware for implementing GraphQL in existing sites.
- Apollo Tracing and OpenTracing extensions for API monitoring.
- Opt-in automatic resolvers mapping between
camelCase
andsnake_case
, and a@convert_kwargs_to_snake_case
function decorator for convertingcamelCase
kwargs tosnake_case
. - Built-in simple synchronous dev server for quick GraphQL experimentation and GraphQL Playground.
- Support for Apollo GraphQL extension for Visual Studio Code.
- GraphQL syntax validation via
gql()
helper function. Also provides colorization if Apollo GraphQL extension is installed. - No global state or object registry, support for multiple GraphQL APIs in same codebase with explicit type reuse.
- Support for
Apollo Federation
.
Installation
Ariadne can be installed with pip:
pip install ariadne
Ariadne requires Python 3.7 or higher.
Quickstart
The following example creates an API defining Person
type and single query field people
returning a list of two persons. It also starts a local dev server with GraphQL Playground available on the http://127.0.0.1:8000
address.
Start by installing uvicorn, an ASGI server we will use to serve the API:
pip install uvicorn
Then create an example.py
file for your example application:
from ariadne import ObjectType, QueryType, gql, make_executable_schema
from ariadne.asgi import GraphQL
# Define types using Schema Definition Language (https://graphql.org/learn/schema/)
# Wrapping string in gql function provides validation and better error traceback
type_defs = gql("""
type Query {
people: [Person!]!
}
type Person {
firstName: String
lastName: String
age: Int
fullName: String
}
""")
# Map resolver functions to Query fields using QueryType
query = QueryType()
# Resolvers are simple python functions
@query.field("people")
def resolve_people(*_):
return [
{"firstName": "John", "lastName": "Doe", "age": 21},
{"firstName": "Bob", "lastName": "Boberson", "age": 24},
]
# Map resolver functions to custom type fields using ObjectType
person = ObjectType("Person")
@person.field("fullName")
def resolve_person_fullname(person, *_):
return "%s %s" % (person["firstName"], person["lastName"])
# Create executable GraphQL schema
schema = make_executable_schema(type_defs, query, person)
# Create an ASGI app using the schema, running in debug mode
app = GraphQL(schema, debug=True)
Finally run the server:
uvicorn example:app
For more guides and examples, please see the documentation.
Contributing
We are welcoming contributions to Ariadne! If you've found a bug or issue, feel free to use GitHub issues. If you have any questions or feedback, don't hesitate to catch us on GitHub discussions.
For guidance and instructions, please see CONTRIBUTING.md.
Website and the docs have their own GitHub repository: mirumee/ariadne-website
Also make sure you follow @AriadneGraphQL on Twitter for latest updates, news and random musings!
Crafted with â¤ï¸ by Mirumee Software hello@mirumee.com
Top Related Projects
GraphQL framework for Python
A GraphQL library for Python that leverages type annotations 🍓
A GraphQL client in Python
The little ASGI framework that shines. 🌟
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot