Skip to main content

Starlette ASGI adapter for Tartiflette

Project description

tartiflette-starlette

travis black python pypi license

Starlette-powered ASGI adapter for Tartiflette, the Python asynchronous GraphQL engine.

⚠️ This package is still under development. Be sure to pin your dependencies!

Installation

Assuming you have already installed Tartiflette, you can install tartiflette-starlette from PyPI:

pip install tartiflette-starlette

Note: tartiflette-starlette is compatible with Starlette 0.12+.

Usage

Getting started with Tartiflette or GraphQL? See Resources.

The TartifletteApp class provided by tartiflette-starlette is an ASGI3-compliant application. As such, it can be served on its own using any ASGI web server, or it can be mounted onto another ASGI application.

Note: GraphQL subscriptions are not supported yet.

Creating a GraphQL app

The following example defines a standalone TartifletteApp which exposes the GraphQL endpoint at /graphql:

# graphql.py
from tartiflette import Resolver

from tartiflette_starlette import TartifletteApp

# Create a Tartiflette resolver for the `hello` field.
@Resolver("Query.hello")
async def resolve_hello(parent, args, context, info):
    name = args["name"]
    return f"Hello, {name}!"

# Define the schema using an SDL string.
# Note: Tartiflette also has support for `.graphql` files.
# See: https://tartiflette.io/docs/api/engine
sdl = """
    type Query {
        hello(name: String): String
    }
"""

app = TartifletteApp(sdl=sdl, path="/graphql")

Serving the app

Since TartifletteApp is an ASGI application, it can be served using any ASGI web server, e.g. uvicorn:

uvicorn graphql:app

Making GraphQL queries

Once the server is running, we're ready to make queries.

As per the GraphQL spec, the query can be passed in various ways:

  • URL query string (methods: GET, POST):
curl 'http://localhost:8000/graphql?query=\{hello(name:"Chuck")\}'
  • JSON-encoded body (methods: POST):
curl \
  -X POST \
  -H "Content-Type: application/json" \
  -d '{"query": "{ hello(name: \"Chuck\") }"}' \
  http://localhost:8000/graphql
  • Raw body (methods: POST):
curl \
  -X POST \
  -H "Content-Type: application/graphql" \
  -d '{ hello(name: "Chuck") }' \
  http://localhost:8000/graphql

All these requests result in the same response:

{
  "data": { "hello": "Hello, Chuck!" },
  "errors": null
}

Furthermore, you can use the built-in GraphiQL client: visit http://localhost:8000/graphql to interactively make queries in the browser. ✨

Mouting onto an ASGI application

Most async web frameworks provide a way to add a route to another ASGI application. This allows you to serve a TartifletteApp along with other endpoints.

The following example mounts the GraphQL app from Creating a GraphQL app onto a Starlette application:

# main.py
from starlette.applications import Starlette
from graphql import app as graphql_app

app = Starlette()
app.mount("/", graphql_app)

You can serve it using uvicorn main:app and make requests at http://localhost:8000/graphql.

Accessing request information

The Starlette Request object is made available in the Tartiflette context which, for example, you can access from resolvers:

@Resolver("Query.whoami")
async def resolve_whoami(parent, args, context, info) -> str:
    request = context["request"]
    user = getattr(request.state, "user", None)
    return "a mystery" if user is None else user

Resources

Once a TartifletteApp is setup, it's just Tartiflette and GraphQL from there!

Here are a few resources you may find useful when getting started:

Happy querying!

API Reference

tartiflette_starlette.TartifletteApp

Parameters

Note: all parameters are keyword-only.

  • engine (Engine): a Tartiflette engine. Required if sdl is not given.
  • sdl (str): a GraphQL schema defined using the GraphQL Schema Definition Language. Required if engine is not given.
  • graphiql (bool): whether to serve the GraphiQL when accessing the endpoint via a web browser. Defaults to True.
  • path (str): the path which clients should make GraphQL queries to. Defaults to "/". A popular alternative is "/graphql".
  • schema_name (str): name of the GraphQL schema from the Schema Registry which should be used — mostly for advanced usage. Defaults to "default".

Methods

  • __call__(scope, receive, send): implementation of the ASGI3 callable interface.

Error responses

Status code Description
405 Method Not Allowed The HTTP method is not one of GET, HEAD or POST.
415 Unsupported Media Type A POST request was made with an unsupported media type.
400 Bad Request The GraphQL query could not be found in the request data.

Contributing

Want to contribute? Awesome! Be sure to read our Contributing guidelines.

Changelog

Changes to this project are recorded in the changelog.

License

MIT

Project details


Download files

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

Source Distribution

tartiflette-starlette-0.1.1.tar.gz (9.6 kB view details)

Uploaded Source

Built Distribution

tartiflette_starlette-0.1.1-py3-none-any.whl (9.5 kB view details)

Uploaded Python 3

File details

Details for the file tartiflette-starlette-0.1.1.tar.gz.

File metadata

  • Download URL: tartiflette-starlette-0.1.1.tar.gz
  • Upload date:
  • Size: 9.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.1

File hashes

Hashes for tartiflette-starlette-0.1.1.tar.gz
Algorithm Hash digest
SHA256 f41834bdd5df86037e66ab13635d6c5b98e502eeb1acc2d9a87d5a63506cf685
MD5 8304d45b369da805e1e4780ff4049dff
BLAKE2b-256 ea971761792258d40ee3921ca530d6fcc677bcfc9e7c739c511248a672c42f8a

See more details on using hashes here.

File details

Details for the file tartiflette_starlette-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: tartiflette_starlette-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 9.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.1

File hashes

Hashes for tartiflette_starlette-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 f00244297e4041ec128f085be71ad7f397a8a2a55f3da0960f09eef67d2ff346
MD5 c1f74a794617a0f432e9c05b8b9ef3bf
BLAKE2b-256 70ec027795e32bad5f70ced071ebf6ae8add51ed0827ca4eda6bea815038ab14

See more details on using hashes here.

Supported by

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