How to Use GraphQL with Python

In this tutorial, we will try to use the GraphQL library in python, Strawberry GraphQL. Python GraphQL library based on dataclasses.

to install Strawberry GraphQL:

pip install strawberry-graphql

Example sourcecode:
[sourcecode language=”python”]
import strawberry

class User:
name: str
age: int

class Query:
def user(self, info) -> User:
return User(name="emka", age=20)

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:

This will open GraphiQL where you can test the API.