Skip to content

Update Ariadne example, add ariadne-codegen to clients #1373

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

Merged
merged 2 commits into from
Feb 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions src/content/code/language-support/python/client/ariadne-codegen.md
Original file line number Diff line number Diff line change
@@ -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
```
31 changes: 16 additions & 15 deletions src/content/code/language-support/python/server/ariadne.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```