Schema First
Schema First
Describe your GraphQL API using Schema Definition Language and connect your business logic using a minimal amount of Python boilerplate.
Simple
Simple
A small and easy-to-learn Pythonic API with simplicity as the guiding force behind its design.
Open Design
Open Design
Easily add new features to the library, and replace or extend existing ones. Integrate with any web framework you like.
Define schema using SDL
Enable frontend and backend teams to cooperate effectively. Ariadne taps into the leading approach in the GraphQL community and opens up hundreds of developer tools, examples, and learning resources.
Ariadne provides out of the box utilities for loading schema from GraphQL files or Python strings.
from ariadne import gql, load_schema_from_path
# load schema from file...
schema = load_schema_from_path("schema.graphql")
# ...directory containing graphql files...
schema = load_schema_from_path("schema")
# ...or inside Python files
schema = gql("""
type Query {
user: User
}
type User {
id: ID
username: String!
}
""")
Add business logic to schema with minimal boilerplate
Use specialized objects that connect business logic to the schema. Replace whatever you like and roll out your own implementations to fit your team’s needs.
from ariadne import ObjectType
# Ariadne uses dedicated objects
query = ObjectType("Query")
# Map resolvers to fields in Query type using decorator syntax...
@query.field("hello")
def resolve_hello(_, info):
request = info.context["request"]
user_agent = request.get("HTTP_USER_AGENT", "guest")
return "Hello, %s!" % user_agent
# ...or plain old setters
query.set_field("hello", resolve_hello)
Fully asynchronous
Use asynchronous query execution and ASGI to speed up your API with minimal effort.
If your stack is not yet ready for async
, don't worry - synchronous query execution is also available.
from ariadne import QueryType
from .models import Category, Promotion
query = QueryType()
@query.field("categories")
async def resolve_categories(*_):
return await Category.query.where(Category.depth == 0)
@query.field("promotions")
async def resolve_promotions(*_):
return await Promotion.query.all()
Serve your API however you want
Ariadne provides WSGI and ASGI apps enabling easy implementation of custom GraphQL services, and full interoperability with popular web frameworks.
Even if your technology has no resources for adding GraphQL to your stack, use the simple guide to create new integrations with Ariadne.
# As standalone ASGI or WSGI app...
from ariadne.asgi import GraphQL
from .schema import schema
app = GraphQL(schema, debug=True)
# ...or add GraphQL API to your favorite web framework
from ariadne.contrib.django.views import GraphQLView
from django.urls import include, path
from .schema import schema
urlpatterns = [
...
path('graphql/', GraphQLView.as_view(schema=schema), name='graphql'),
]