网页 综合 更新时间 2026-11-14 05:21:23

Getting started with Strawberry | 🍓 Strawberry GraphQL

自动蜘蛛 strawberry.rocks 正文约 2908 字
Getting started with Strawberry | 🍓 Strawberry GraphQL
This tutorial will help you:
This tutorial assumes that you are familiar with the command line and Python, and that you have a recent version of Python (3.11+) installed.
Strawberry is built on top of Python’s dataclasses and type hints functionality.
Let’s create a new folder:
After that we need a new virtualenv:
Activate the virtualenv and then install strawberry plus the dev server.
Every GraphQL server uses a schema to define the structure of the data that clients can query. In this example, we will create a server for querying a collection of books by title and author.
In your favorite editor create a file called schema.py , with the following contents:
This will create a GraphQL schema where clients will be able to execute a query named books that will return a list of zero or more books.
Now that we have our structure of your schema, we can define the data itself. Strawberry can work with any data source (for example a database, a REST API, files, etc). For this tutorial we will be using hard-coded data.
Let’s create a function that returns some books.
Since strawberry makes uses of python classes to create the schema, this means that we can also reuse them to create the data objects.
We now have a function that returns some books, but Strawberry doesn’t know it should use it when executing a query. To fix this we need to update our query to specify the resolver for our books. A resolver tells Strawberry how to fetch the data associated with a particular field.
Let’s update our Query:
Using strawberry.field allows us to specify a resolver for a particular field.
We didn’t have to specify any resolver for the Book’s fields, this is because Strawberry adds a default for each field, returning the value of that field.
We have defined our data and query, now what we need to do is create a GraphQL schema and start the server.
To create the schema add the following code:
Then run the following command
This will start a dev server, you should see the following output:
We can now execute GraphQL queries. Strawberry comes with a tool called GraphiQL. To open it go to http://0.0.0.0:8000/graphql
You should see something like this:
The GraphiQL UI includes:
Our server supports a single query named books. Let’s execute it!
Paste the following string into the left area and then click the play button:
You should see the hardcoded data appear on the right side:
GraphQL allows clients to query only the fields they need, go ahead and remove author from the query and run it again. The response should now only show the title for each book.
Well done! You just created your first GraphQL API using Strawberry 🙌!
Check out the following resources to learn more about GraphQL and Strawberry.
Was this page helpful?
Thank you for your feedback! 🙏 It's been posted on our Discord server. Feel free to join and follow up on the conversation!
Something went wrong. Please try again.
访问原文 返回搜索 https://strawberry.rocks/docs
本页为82搜索生成的网页快照,内容来自站内索引;演示数据的原文链接仅作示例。