-
-
Notifications
You must be signed in to change notification settings - Fork 543
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Update README.md #3679
base: main
Are you sure you want to change the base?
Update README.md #3679
Changes from 9 commits
323ab87
b318f59
42339c2
7fc7fc4
f8100ab
545bf1f
b57ed59
acc42b6
32c08b1
351e8eb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,24 +2,23 @@ | |
|
||
# Strawberry GraphQL | ||
|
||
> Python GraphQL library based on dataclasses | ||
> Python GraphQL library leveraging modern Python features like dataclasses and type hints to create GraphQL APIs. | ||
|
||
[![CircleCI](https://img.shields.io/circleci/token/307b40d5e152e074d34f84d30d226376a15667d5/project/github/strawberry-graphql/strawberry/main.svg?style=for-the-badge)](https://circleci.com/gh/strawberry-graphql/strawberry/tree/main) | ||
[![Discord](https://img.shields.io/discord/689806334337482765?label=discord&logo=discord&logoColor=white&style=for-the-badge&color=blue)](https://discord.gg/ZkRTEJQ) | ||
[![PyPI](https://img.shields.io/pypi/v/strawberry-graphql?logo=pypi&logoColor=white&style=for-the-badge)](https://pypi.org/project/strawberry-graphql/) | ||
|
||
## Installation ( Quick Start ) | ||
## Quick Start | ||
|
||
The quick start method provides a server and CLI to get going quickly. Install | ||
with: | ||
### Installation | ||
|
||
```shell | ||
pip install "strawberry-graphql[debug-server]" | ||
``` | ||
|
||
## Getting Started | ||
### Basic Example | ||
|
||
Create a file called `app.py` with the following code: | ||
Create a new file `app.py`: | ||
|
||
```python | ||
import strawberry | ||
|
@@ -41,79 +40,189 @@ class Query: | |
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: | ||
Run the debug server: | ||
|
||
```shell | ||
strawberry server app | ||
``` | ||
|
||
Open the debug server by clicking on the following link: | ||
[http://0.0.0.0:8000/graphql](http://0.0.0.0:8000/graphql) | ||
Visit [http://0.0.0.0:8000/graphql](http://0.0.0.0:8000/graphql) to access GraphiQL and explore your API. | ||
|
||
This will open GraphiQL where you can test the API. | ||
## Features | ||
|
||
### Type-checking | ||
### Type Checking with MyPy | ||
HmbleCreator marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
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: | ||
Enable static type checking by adding to your `mypy.ini`: | ||
|
||
```ini | ||
[mypy] | ||
plugins = strawberry.ext.mypy_plugin | ||
``` | ||
|
||
[mypy]: http://www.mypy-lang.org/ | ||
|
||
### Django Integration | ||
|
||
A Django view is provided for adding a GraphQL endpoint to your application. | ||
|
||
1. Add the app to your `INSTALLED_APPS`. | ||
1. Add to `INSTALLED_APPS`: | ||
|
||
```python | ||
INSTALLED_APPS = [ | ||
..., # your other apps | ||
"strawberry.django", | ||
# ... your other apps | ||
] | ||
``` | ||
|
||
2. Add the view to your `urls.py` file. | ||
2. Configure URL routing in `urls.py`: | ||
|
||
```python | ||
from strawberry.django.views import GraphQLView | ||
from .schema import schema | ||
|
||
urlpatterns = [ | ||
..., | ||
path("graphql", GraphQLView.as_view(schema=schema)), | ||
] | ||
``` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion: worth mentioning that they can (on a step 3), optionally, use the official strawberry-django integration, which provides type generation from models, query optimization and other useful utilities. |
||
|
||
## WebSockets | ||
### WebSocket Support | ||
|
||
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: | ||
For GraphQL subscriptions over WebSockets: | ||
|
||
```shell | ||
pip install 'strawberry-graphql[debug-server]' | ||
pip install 'uvicorn[standard]' | ||
``` | ||
|
||
## Examples | ||
## Testing | ||
|
||
Strawberry provides built-in testing utilities through `BaseGraphQLTestClient`. Here are three different implementations using popular HTTP clients: | ||
|
||
### 1. Testing with httpx | ||
|
||
```python | ||
from strawberry.test import BaseGraphQLTestClient | ||
import httpx | ||
|
||
|
||
class HttpxTestClient(BaseGraphQLTestClient): | ||
def __init__(self) -> None: | ||
self.client = httpx.Client(base_url="http://localhost:8000") | ||
|
||
def request(self, body: str, headers=None, files=None): | ||
headers = headers or {} | ||
response = self.client.post( | ||
"/graphql", | ||
json=body, | ||
headers=headers, | ||
files=files, | ||
) | ||
return response.json() | ||
|
||
|
||
def test_query(): | ||
client = HttpxTestClient() | ||
response = client.query( | ||
""" | ||
{ | ||
user { | ||
name | ||
age | ||
} | ||
} | ||
""" | ||
) | ||
assert response.data["user"]["name"] == "Patrick" | ||
HmbleCreator marked this conversation as resolved.
Show resolved
Hide resolved
|
||
assert not response.errors | ||
``` | ||
|
||
### 2. Testing with requests | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. thought: wondering if all those examples should go to the docs instead, and have a single example here to reduce bloat from the README |
||
|
||
```python | ||
from strawberry.test import BaseGraphQLTestClient | ||
from requests import Session | ||
|
||
|
||
class RequestsTestClient(BaseGraphQLTestClient): | ||
def __init__(self) -> None: | ||
self.client = Session() | ||
self.client.base_url = "http://localhost:8000" | ||
|
||
def request(self, body: str, headers=None, files=None): | ||
headers = headers or {} | ||
response = self.client.post( | ||
f"{self.client.base_url}/graphql", | ||
json=body, | ||
headers=headers, | ||
files=files, | ||
) | ||
return response.json() | ||
|
||
|
||
def test_query_with_variables(): | ||
client = RequestsTestClient() | ||
response = client.query( | ||
""" | ||
query GetUser($id: ID!) { | ||
user(id: $id) { | ||
name | ||
age | ||
} | ||
} | ||
""", | ||
variables={"id": "123"}, | ||
) | ||
assert response.data["user"]["name"] == "Patrick" | ||
assert not response.errors | ||
``` | ||
|
||
### 3. Testing with aiohttp (async) | ||
|
||
```python | ||
from strawberry.test import BaseGraphQLTestClient | ||
import aiohttp | ||
import asyncio | ||
|
||
|
||
class AiohttpTestClient(BaseGraphQLTestClient): | ||
def __init__(self) -> None: | ||
self.base_url = "http://localhost:8000" | ||
|
||
async def async_request(self, body: str, headers=None, files=None): | ||
headers = headers or {} | ||
async with aiohttp.ClientSession() as session: | ||
async with session.post( | ||
f"{self.base_url}/graphql", | ||
json=body, | ||
headers=headers, | ||
) as response: | ||
return await response.json() | ||
|
||
def request(self, body: str, headers=None, files=None): | ||
return asyncio.run(self.async_request(body, headers, files)) | ||
|
||
|
||
def test_async_query(): | ||
client = AiohttpTestClient() | ||
response = client.query( | ||
""" | ||
{ | ||
user { | ||
name | ||
age | ||
} | ||
} | ||
""" | ||
) | ||
assert response.data["user"]["name"] == "Patrick" | ||
assert not response.errors | ||
``` | ||
|
||
## Examples & Resources | ||
|
||
* [Various examples on how to use Strawberry](https://github.com/strawberry-graphql/examples) | ||
* [Full stack example using Starlette, SQLAlchemy, Typescript codegen and Next.js](https://github.com/jokull/python-ts-graphql-demo) | ||
* [Quart + Strawberry tutorial](https://github.com/rockyburt/Ketchup) | ||
- [Official Examples Repository](https://github.com/strawberry-graphql/examples) | ||
- [Full-stack Demo (Starlette + SQLAlchemy + TypeScript + Next.js)](https://github.com/jokull/python-ts-graphql-demo) | ||
- [Quart Integration Tutorial](https://github.com/rockyburt/Ketchup) | ||
|
||
## Contributing | ||
## Development | ||
|
||
We use [poetry](https://github.com/sdispater/poetry) to manage dependencies, to | ||
get started follow these steps: | ||
### Setting Up Development Environment | ||
|
||
```shell | ||
git clone https://github.com/strawberry-graphql/strawberry | ||
|
@@ -122,31 +231,21 @@ poetry install --with integrations | |
poetry run pytest | ||
``` | ||
|
||
For all further detail, check out the [Contributing Page](CONTRIBUTING.md) | ||
|
||
|
||
### Pre commit | ||
|
||
We have a configuration for | ||
[pre-commit](https://github.com/pre-commit/pre-commit), to add the hook run the | ||
following command: | ||
### Pre-commit Hooks | ||
|
||
```shell | ||
pre-commit install | ||
``` | ||
|
||
## Links | ||
## Community & Support | ||
|
||
- Project homepage: https://strawberry.rocks | ||
- Repository: https://github.com/strawberry-graphql/strawberry | ||
- Issue tracker: https://github.com/strawberry-graphql/strawberry/issues | ||
- In case of sensitive bugs like security vulnerabilities, please contact | ||
[email protected] directly instead of using the issue tracker. We | ||
value your effort to improve the security and privacy of this project! | ||
- Documentation: [https://strawberry.rocks](https://strawberry.rocks) | ||
- GitHub Repository: [https://github.com/strawberry-graphql/strawberry](https://github.com/strawberry-graphql/strawberry) | ||
- Issue Tracker: [https://github.com/strawberry-graphql/strawberry/issues](https://github.com/strawberry-graphql/strawberry/issues) | ||
- Security Issues: Contact [email protected] directly | ||
HmbleCreator marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
## Licensing | ||
## License | ||
|
||
The code in this project is licensed under MIT license. See [LICENSE](./LICENSE) | ||
for more information. | ||
This project is licensed under the MIT License - see the [LICENSE](./LICENSE) file for details. | ||
|
||
![Recent Activity](https://images.repography.com/0/strawberry-graphql/strawberry/recent-activity/d751713988987e9331980363e24189ce.svg) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is our tagline, I'd prefer to leave it as-is