@@ -73,7 +73,67 @@ async def main():
7373 student_names = []
7474 async for doc in cursor:
7575 student_names.append(doc[" name" ])
76+ ```
77+
78+ Another example with [ graphs] ( https://docs.arangodb.com/stable/graphs/ ) :
7679
80+ ``` python
81+ async def main ():
82+ from arangoasync import ArangoClient
83+ from arangoasync.auth import Auth
84+
85+ # Initialize the client for ArangoDB.
86+ async with ArangoClient(hosts = " http://localhost:8529" ) as client:
87+ auth = Auth(username = " root" , password = " passwd" )
88+
89+ # Connect to "test" database as root user.
90+ db = await client.db(" test" , auth = auth)
91+
92+ # Get the API wrapper for graph "school".
93+ if await db.has_graph(" school" ):
94+ graph = db.graph(" school" )
95+ else :
96+ graph = await db.create_graph(" school" )
97+
98+ # Create vertex collections for the graph.
99+ students = await graph.create_vertex_collection(" students" )
100+ lectures = await graph.create_vertex_collection(" lectures" )
101+
102+ # Create an edge definition (relation) for the graph.
103+ edges = await graph.create_edge_definition(
104+ edge_collection = " register" ,
105+ from_vertex_collections = [" students" ],
106+ to_vertex_collections = [" lectures" ]
107+ )
108+
109+ # Insert vertex documents into "students" (from) vertex collection.
110+ await students.insert({" _key" : " 01" , " full_name" : " Anna Smith" })
111+ await students.insert({" _key" : " 02" , " full_name" : " Jake Clark" })
112+ await students.insert({" _key" : " 03" , " full_name" : " Lisa Jones" })
113+
114+ # Insert vertex documents into "lectures" (to) vertex collection.
115+ await lectures.insert({" _key" : " MAT101" , " title" : " Calculus" })
116+ await lectures.insert({" _key" : " STA101" , " title" : " Statistics" })
117+ await lectures.insert({" _key" : " CSC101" , " title" : " Algorithms" })
118+
119+ # Insert edge documents into "register" edge collection.
120+ await edges.insert({" _from" : " students/01" , " _to" : " lectures/MAT101" })
121+ await edges.insert({" _from" : " students/01" , " _to" : " lectures/STA101" })
122+ await edges.insert({" _from" : " students/01" , " _to" : " lectures/CSC101" })
123+ await edges.insert({" _from" : " students/02" , " _to" : " lectures/MAT101" })
124+ await edges.insert({" _from" : " students/02" , " _to" : " lectures/STA101" })
125+ await edges.insert({" _from" : " students/03" , " _to" : " lectures/CSC101" })
126+
127+ # Traverse the graph in outbound direction, breath-first.
128+ query = """
129+ FOR v, e, p IN 1..3 OUTBOUND 'students/01' GRAPH 'school'
130+ OPTIONS { bfs: true, uniqueVertices: 'global' }
131+ RETURN {vertex: v, edge: e, path: p}
132+ """
133+
134+ async with await db.aql.execute(query) as cursor:
135+ async for doc in cursor:
136+ print (doc)
77137```
78138
79139Please see the [ documentation] ( https://python-arango-async.readthedocs.io/en/latest/ ) for more details.
0 commit comments