Source code for raphtory.graphqlclient

  1from gql import Client, gql
  2from gql.transport.requests import RequestsHTTPTransport
  3import raphtory
  4from raphtory import internal_graphql
  5
  6
[docs] 7class RaphtoryGraphQLClient: 8 """ 9 A client for handling GraphQL operations in the context of Raphtory. 10 """ 11 12 def __init__(self, url: str): 13 """ 14 Initialize a GraphQL Client Connection. 15 16 Args: 17 url (str): URL to a server with the port appended to the URL. 18 19 Note: 20 This constructor creates a GraphQL client connection to the given URL. 21 """ 22 transport = RequestsHTTPTransport(url=url, use_json=True) 23 self.client = Client(transport=transport, fetch_schema_from_transport=True) 24 # Below attempts to connect to the server with the url 25 # self.client.connect_sync() 26
[docs] 27 def query(self, query: str, variables: dict = {}): 28 """ 29 Execute a GraphQL query. 30 31 Args: 32 query (str): The GraphQL query string. 33 variables (dict, optional): Variables for the query. Defaults to an empty dictionary. 34 35 Returns: 36 dict: Result of the query. 37 """ 38 query = gql(query) 39 return self.client.execute(query, variables)
40
[docs] 41 def load_graphs_from_path(self, path: str) -> dict: 42 """ 43 Load graphs from a directory of bincode files. 44 45 Args: 46 path (str): Directory containing bincode files. 47 48 Returns: 49 dict: Result after executing the mutation. 50 51 Note: 52 Existing graphs with the same name are overwritten. 53 """ 54 mutation_q = gql( 55 """ 56 mutation LoadGraphsFromPath($path: String!) { 57 loadGraphsFromPath(path: $path) 58 } 59 """ 60 ) 61 result = self.client.execute(mutation_q, variable_values={"path": path}) 62 if len(result["loadGraphsFromPath"]): 63 print("Loaded %i graph(s)" % len(result["loadGraphsFromPath"])) 64 return result 65 else: 66 print("Could not find a graph to load") 67 return result
68
[docs] 69 def load_new_graphs_from_path(self, path: str) -> dict: 70 """ 71 Load new graphs from a directory of bincode files. 72 73 Args: 74 path (str): Directory containing bincode files. 75 76 Returns: 77 dict: Result after executing the mutation. 78 79 Note: 80 Existing graphs will not be overwritten. 81 """ 82 mutation_q = gql( 83 """ 84 mutation LoadNewGraphsFromPath($path: String!) { 85 loadNewGraphsFromPath(path: $path) 86 } 87 """ 88 ) 89 result = self.client.execute(mutation_q, variable_values={"path": path}) 90 91 if len(result["loadNewGraphsFromPath"]): 92 print("Loaded %i graph(s)" % len(result["loadNewGraphsFromPath"])) 93 return result 94 else: 95 print("Could not find a graph to load") 96 return result
97
[docs] 98 def send_graph(self, name: str, graph: raphtory.Graph): 99 """ 100 Upload a graph to the GraphQL Server. 101 102 Args: 103 name (str): Name of the graph. 104 graph (raphtory.Graph): Graph object to be uploaded. 105 106 Returns: 107 dict: Result after executing the mutation. 108 109 Raises: 110 Exception: If there's an error sending the graph. 111 """ 112 encoded_graph = internal_graphql.encode_graph(graph) 113 114 mutation_q = gql( 115 """ 116 mutation SendGraph($name: String!, $graph: String!) { 117 sendGraph(name: $name, graph: $graph) 118 } 119 """ 120 ) 121 result = self.client.execute( 122 mutation_q, variable_values={"name": name, "graph": encoded_graph} 123 ) 124 if "sendGraph" in result: 125 print("Sent graph %s to GraphlQL Server" % len(result["sendGraph"])) 126 return result 127 else: 128 raise Exception("Error Sending Graph %s" % result)