From 545bf1f5f590c92bd349fdd905b04854439ea7a6 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 24 Oct 2024 11:35:05 +0000 Subject: [PATCH] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- README.md | 89 ++++++++++++++++++++++++++++--------------------------- 1 file changed, 46 insertions(+), 43 deletions(-) diff --git a/README.md b/README.md index 0572fd6f33..5c5a61af62 100644 --- a/README.md +++ b/README.md @@ -23,17 +23,20 @@ Create a new file `app.py`: ```python 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) ``` @@ -98,32 +101,31 @@ Strawberry provides built-in testing utilities through `BaseGraphQLTestClient`. from strawberry.test import BaseGraphQLTestClient import httpx + class HttpxTestClient(BaseGraphQLTestClient): def __init__(self): 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 - ) + 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 - } + + response = client.query( + """ + { + user { + name + age + } } - """) - + """ + ) + assert response.data["user"]["name"] == "Patrick" assert not response.errors ``` @@ -134,36 +136,35 @@ def test_query(): from strawberry.test import BaseGraphQLTestClient from requests import Session + class RequestsTestClient(BaseGraphQLTestClient): def __init__(self): 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 + 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 - } + query GetUser($id: ID!) { + user(id: $id) { + name + age + } } """, - variables={"id": "123"} + variables={"id": "123"}, ) - + assert response.data["user"]["name"] == "Patrick" assert not response.errors ``` @@ -175,35 +176,37 @@ from strawberry.test import BaseGraphQLTestClient import aiohttp import asyncio + class AiohttpTestClient(BaseGraphQLTestClient): def __init__(self): 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 + 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 - } + + response = client.query( + """ + { + user { + name + age + } } - """) - + """ + ) + assert response.data["user"]["name"] == "Patrick" assert not response.errors ```