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
@strawberry.type
class User:
name: str
age: int
@strawberry.type
class Query:
@strawberry.field
def user(self, info) -> User:
return User(name="emka", age=20)
schema = strawberry.Schema(query=Query)
[/sourcecode]
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.