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..d78f317829 --- /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 ```