@@ -39,7 +39,7 @@ Here is an example showing how **python-arango-async** client can be used:
3939 async for doc in cursor:
4040 student_names.append(doc[" name" ])
4141
42- You may also use the client without a context manager, but you must ensure to close the client when done:
42+ You may also use the client without a context manager, but you must ensure to close the client when done.
4343
4444.. code-block :: python
4545
@@ -61,3 +61,65 @@ You may also use the client without a context manager, but you must ensure to cl
6161
6262 # Close the client when done.
6363 await client.close()
64+
65+ Another example with `graphs `_:
66+
67+ .. _graphs : https://docs.arangodb.com/stable/graphs/
68+
69+ .. code-block :: python
70+
71+ from arangoasync import ArangoClient
72+ from arangoasync.auth import Auth
73+
74+ # Initialize the client for ArangoDB.
75+ async with ArangoClient(hosts = " http://localhost:8529" ) as client:
76+ auth = Auth(username = " root" , password = " passwd" )
77+
78+ # Connect to "test" database as root user.
79+ db = await client.db(" test" , auth = auth)
80+
81+ # Get the API wrapper for graph "school".
82+ if await db.has_graph(" school" ):
83+ graph = db.graph(" school" )
84+ else :
85+ graph = await db.create_graph(" school" )
86+
87+ # Create vertex collections for the graph.
88+ students = await graph.create_vertex_collection(" students" )
89+ lectures = await graph.create_vertex_collection(" lectures" )
90+
91+ # Create an edge definition (relation) for the graph.
92+ edges = await graph.create_edge_definition(
93+ edge_collection = " register" ,
94+ from_vertex_collections = [" students" ],
95+ to_vertex_collections = [" lectures" ]
96+ )
97+
98+ # Insert vertex documents into "students" (from) vertex collection.
99+ await students.insert({" _key" : " 01" , " full_name" : " Anna Smith" })
100+ await students.insert({" _key" : " 02" , " full_name" : " Jake Clark" })
101+ await students.insert({" _key" : " 03" , " full_name" : " Lisa Jones" })
102+
103+ # Insert vertex documents into "lectures" (to) vertex collection.
104+ await lectures.insert({" _key" : " MAT101" , " title" : " Calculus" })
105+ await lectures.insert({" _key" : " STA101" , " title" : " Statistics" })
106+ await lectures.insert({" _key" : " CSC101" , " title" : " Algorithms" })
107+
108+ # Insert edge documents into "register" edge collection.
109+ await edges.insert({" _from" : " students/01" , " _to" : " lectures/MAT101" })
110+ await edges.insert({" _from" : " students/01" , " _to" : " lectures/STA101" })
111+ await edges.insert({" _from" : " students/01" , " _to" : " lectures/CSC101" })
112+ await edges.insert({" _from" : " students/02" , " _to" : " lectures/MAT101" })
113+ await edges.insert({" _from" : " students/02" , " _to" : " lectures/STA101" })
114+ await edges.insert({" _from" : " students/03" , " _to" : " lectures/CSC101" })
115+
116+ # Traverse the graph in outbound direction, breath-first.
117+ query = """
118+ FOR v, e, p IN 1..3 OUTBOUND 'students/01' GRAPH 'school'
119+ OPTIONS { bfs: true, uniqueVertices: 'global' }
120+ RETURN {vertex: v, edge: e, path: p}
121+ """
122+
123+ async with await db.aql.execute(query) as cursor:
124+ async for doc in cursor:
125+ print (doc)
0 commit comments