Skip to content

Commit 07cb48b

Browse files
authored
Update Ariadne example, add ariadne-codegen to clients (graphql#1373)
1 parent 64901da commit 07cb48b

File tree

2 files changed

+71
-15
lines changed

2 files changed

+71
-15
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
---
2+
name: Ariadne Codegen
3+
description: Generate fully typed Python GraphQL client from any schema and queries.
4+
url: https://github.com/mirumee/ariadne-codegen
5+
github: mirumee/ariadne-codegen
6+
---
7+
8+
Install Ariadne Codegen:
9+
10+
```
11+
$ pip install ariadne-codegen
12+
```
13+
14+
Create `queries.graphql` file:
15+
16+
```graphql
17+
mutation CreateToken($username: String!, $password: String!) {
18+
createToken(username: $username, password: $password) {
19+
token
20+
errors {
21+
field
22+
message
23+
}
24+
}
25+
}
26+
```
27+
28+
Add `[ariadne-codegen]` section to your `pyproject.toml`:
29+
30+
```
31+
[ariadne-codegen]
32+
queries_path = "queries.graphql"
33+
remote_schema_url = "http://example.com/graphql/"
34+
```
35+
36+
Generate client:
37+
38+
```
39+
$ ariadne-codegen
40+
```
41+
42+
And use it in your Python projects:
43+
44+
```python
45+
from graphql_client import Client
46+
47+
with Client("http://example.com/graphql/") as client:
48+
result = client.create_token(username="Admin", password="Example123)
49+
50+
if result.errors:
51+
error = result.errors[0]
52+
raise ValidationError({error.field: error.message})
53+
54+
auth_token = result.token
55+
```

src/content/code/language-support/python/server/ariadne.md

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,36 +8,37 @@ github: mirumee/ariadne
88
Ariadne can be installed with pip:
99

1010
```bash
11-
pip install ariadne
11+
$ pip install ariadne
1212
```
1313

14-
It ships with many GraphQL server implementations, enabling easy experimentation:
14+
Minimal "Hello world" server example:
1515

1616
```python
17-
from ariadne import ObjectType, QueryType, gql, make_executable_schema
17+
from ariadne import ObjectType, gql, make_executable_schema
1818
from ariadne.asgi import GraphQL
19-
# Define types using Schema Definition Language (https://graphql.org/learn/schema/)
20-
# Wrapping string in gql function provides validation and better error traceback
21-
type_defs = gql("""
19+
20+
type_defs = gql(
21+
"""
2222
type Query {
2323
hello: String!
2424
}
25-
""")
26-
# Bind resolver functions to Query's fields using QueryType
27-
query_type = QueryType()
28-
# Resolvers are simple python functions
25+
"""
26+
)
27+
28+
query_type = ObjectType("Query")
29+
2930
@query_type.field("hello")
3031
def resolve_hello(*_):
3132
return "Hello world!"
32-
# Create executable GraphQL schema
33+
3334
schema = make_executable_schema(type_defs, query_type)
34-
# Create an ASGI app using the schema, running in debug mode
35+
3536
app = GraphQL(schema, debug=True)
3637
```
3738

38-
Above server can be ran with uvicorn:
39+
Run the server with uvicorn:
3940

4041
```
41-
pip install uvicorn
42-
uvicorn example:app
42+
$ pip install uvicorn
43+
$ uvicorn example:app
4344
```

0 commit comments

Comments
 (0)