DEV Community

Robert Matyszewski
Robert Matyszewski

Posted on

GraphQL library for Python - Strawberry

I recently came across an interesting library for GraphQL. Strawberry is a new GraphQL library for Python 3, inspired by dataclasses. An initial version of Strawberry has been released on GitHub. To follow the project, you can sign up to get updates on the Strawberry site or github. Links below:

https://strawberry.rocks/

Strawberry is created by @patrick91 who is also an organizer of @pyconit. It was originally announced during Python Pizza Berlin.

PyPi release and demo

Alpha version on PyPI: https://pypi.org/project/strawberry-graphql
Demo on Heroku using Starlette: https://demo.strawberry.rocks/graphql

Installation

pip install strawberry-graphql
Enter fullscreen mode Exit fullscreen mode

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, info) -> User:
        return User(name="Patrick", age=100)


schema = strawberry.Schema(query=Query)
Enter fullscreen mode Exit fullscreen mode

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

To run the debug server run the following command:

strawberry run server app
Enter fullscreen mode Exit fullscreen mode

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

This will open a GraphQL playground where you can test the API.

GitHub logo strawberry-graphql / strawberry

A GraphQL library for Python that leverages type annotations 🍓

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]"
Enter fullscreen mode Exit fullscreen mode

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)
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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

This will open GraphiQL where you…

Top comments (1)

Collapse
 
michaeltharrington profile image
Michael Tharrington

Whoever designed the Strawberry logo gets +1000 cool points! 🍓