strawberry-graphql / strawberry

A GraphQL library for Python that leverages type annotations πŸ“

Home Page:https://strawberry.rocks

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Strawberry GraphQL

Python GraphQL library based on dataclasses

CircleCI Discord PyPI

Installation ( Quick Start )

The quick start method provides a server and CLI to get going quickly. Install with:

pip install "strawberry-graphql[debug-server]"

Getting Started

Create a file called app.py with the following code:

import strawberry


@strawberry.type
class User:
    name: str
    age: int


@strawberry.type
class Query:
    @strawberry.field
    def user(self) -> User:
        return User(name="Patrick", age=100)


schema = strawberry.Schema(query=Query)

This will create a GraphQL schema defining a User type and a single query field user that will return a hardcoded user.

To run the debug server run the following command:

strawberry server app

Open the debug server by clicking on the following link: http://0.0.0.0:8000/graphql

This will open GraphiQL where you can test the API.

Type-checking

Strawberry comes with a mypy plugin that enables statically type-checking your GraphQL schema. To enable it, add the following lines to your mypy.ini configuration:

[mypy]
plugins = strawberry.ext.mypy_plugin

Django Integration

A Django view is provided for adding a GraphQL endpoint to your application.

  1. Add the app to your INSTALLED_APPS.
INSTALLED_APPS = [
    ...,  # your other apps
    "strawberry.django",
]
  1. Add the view to your urls.py file.
from strawberry.django.views import GraphQLView
from .schema import schema

urlpatterns = [
    ...,
    path("graphql", GraphQLView.as_view(schema=schema)),
]

WebSockets

To support graphql Subscriptions over WebSockets you need to provide a WebSocket enabled server. The debug server can be made to support WebSockets with these commands:

pip install 'strawberry-graphql[debug-server]'
pip install 'uvicorn[standard]'

Examples

Contributing

We use poetry to manage dependencies, to get started follow these steps:

git clone https://github.com/strawberry-graphql/strawberry
cd strawberry
poetry install --with integrations
poetry run pytest

For all further detail, check out the Contributing Page

Pre commit

We have a configuration for pre-commit, to add the hook run the following command:

pre-commit install

Links

Licensing

The code in this project is licensed under MIT license. See LICENSE for more information.

Recent Activity

About

A GraphQL library for Python that leverages type annotations πŸ“

https://strawberry.rocks

License:MIT License


Languages

Language:Python 99.4%Language:HTML 0.3%Language:TypeScript 0.3%Language:Dockerfile 0.0%Language:Shell 0.0%