File tree 2 files changed +71
-15
lines changed
src/content/code/language-support/python 2 files changed +71
-15
lines changed Original file line number Diff line number Diff line change
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
+ ```
Original file line number Diff line number Diff line change @@ -8,36 +8,37 @@ github: mirumee/ariadne
8
8
Ariadne can be installed with pip:
9
9
10
10
``` bash
11
- pip install ariadne
11
+ $ pip install ariadne
12
12
```
13
13
14
- It ships with many GraphQL server implementations, enabling easy experimentation :
14
+ Minimal "Hello world" server example :
15
15
16
16
``` python
17
- from ariadne import ObjectType, QueryType, gql, make_executable_schema
17
+ from ariadne import ObjectType, gql, make_executable_schema
18
18
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
+ """
22
22
type Query {
23
23
hello: String!
24
24
}
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
+
29
30
@query_type.field (" hello" )
30
31
def resolve_hello (* _ ):
31
32
return " Hello world!"
32
- # Create executable GraphQL schema
33
+
33
34
schema = make_executable_schema(type_defs, query_type)
34
- # Create an ASGI app using the schema, running in debug mode
35
+
35
36
app = GraphQL(schema, debug = True )
36
37
```
37
38
38
- Above server can be ran with uvicorn:
39
+ Run the server with uvicorn:
39
40
40
41
```
41
- pip install uvicorn
42
- uvicorn example:app
42
+ $ pip install uvicorn
43
+ $ uvicorn example:app
43
44
```
You can’t perform that action at this time.
0 commit comments