From b8d1d6d1bf35b48d8a80f03bf1c63625308e5d57 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafa=C5=82=20Pito=C5=84?= Date: Tue, 21 Feb 2023 18:41:30 +0100 Subject: [PATCH 1/2] Update Ariadne example, add ariadne-codegen to clients --- .../python/client/ariadne-codegen.md | 55 +++++++++++++++++++ .../language-support/python/server/ariadne.md | 31 ++++++----- 2 files changed, 71 insertions(+), 15 deletions(-) create mode 100644 src/content/code/language-support/python/client/ariadne-codegen.md diff --git a/src/content/code/language-support/python/client/ariadne-codegen.md b/src/content/code/language-support/python/client/ariadne-codegen.md new file mode 100644 index 0000000000..78580c540d --- /dev/null +++ b/src/content/code/language-support/python/client/ariadne-codegen.md @@ -0,0 +1,55 @@ +--- +name: Ariadne Codegen +description: Generate fully typed Python GraphQL client from any schema and queries. +url: https://github.com/mirumee/ariadne-codegen +github: mirumee/ariadne-codegen +--- + +Install Ariadne Codegen: + +``` +$ pip install ariadne-codegen +``` + +Create `queries.graphql` file: + +```graphql +mutation CreateToken($username: String!, $password: String!) { + createToken(username: $username, password: $password) { + token + errors { + field + message + } + } +} +``` + +Add `[ariadne-codegen]` section to your `pyproject.toml`: + +``` +[ariadne-codegen] +queries_path = "queries.graphql" +remote_schema_url = "http://example.com/graphql/" +``` + +Generate client: + +``` +$ ariadne-codegen +``` + +And use it in your Python projects: + +```python +from graphql_client import Client + +with Client("http://example.com/graphql/") as client: + result = client.create_token(username="Admin", password="Example123) + + if result.errors: + error = result.errors[0] + raise ValidationError({error.field: error.message }) + + auth_token = result.token +``` \ No newline at end of file diff --git a/src/content/code/language-support/python/server/ariadne.md b/src/content/code/language-support/python/server/ariadne.md index aa6f3ca0c1..92e9654527 100644 --- a/src/content/code/language-support/python/server/ariadne.md +++ b/src/content/code/language-support/python/server/ariadne.md @@ -8,36 +8,37 @@ github: mirumee/ariadne Ariadne can be installed with pip: ```bash -pip install ariadne +$ pip install ariadne ``` -It ships with many GraphQL server implementations, enabling easy experimentation: +Minimal "Hello world" server example: ```python -from ariadne import ObjectType, QueryType, gql, make_executable_schema +from ariadne import ObjectType, gql, make_executable_schema from ariadne.asgi import GraphQL -# Define types using Schema Definition Language (https://graphql.org/learn/schema/) -# Wrapping string in gql function provides validation and better error traceback -type_defs = gql(""" + +type_defs = gql( + """ type Query { hello: String! } -""") -# Bind resolver functions to Query's fields using QueryType -query_type = QueryType() -# Resolvers are simple python functions + """ +) + +query_type = ObjectType("Query") + @query_type.field("hello") def resolve_hello(*_): return "Hello world!" -# Create executable GraphQL schema + schema = make_executable_schema(type_defs, query_type) -# Create an ASGI app using the schema, running in debug mode + app = GraphQL(schema, debug=True) ``` -Above server can be ran with uvicorn: +Run the server with uvicorn: ``` -pip install uvicorn -uvicorn example:app +$ pip install uvicorn +$ uvicorn example:app ``` From 5d4c5ef51a2b0c642c21111d70c437f72b458068 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafa=C5=82=20Pito=C5=84?= Date: Tue, 21 Feb 2023 18:58:31 +0100 Subject: [PATCH 2/2] Tweak code example --- .../code/language-support/python/client/ariadne-codegen.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/content/code/language-support/python/client/ariadne-codegen.md b/src/content/code/language-support/python/client/ariadne-codegen.md index 78580c540d..d78f317829 100644 --- a/src/content/code/language-support/python/client/ariadne-codegen.md +++ b/src/content/code/language-support/python/client/ariadne-codegen.md @@ -49,7 +49,7 @@ with Client("http://example.com/graphql/") as client: if result.errors: error = result.errors[0] - raise ValidationError({error.field: error.message }) + raise ValidationError({error.field: error.message}) auth_token = result.token ``` \ No newline at end of file