Source code for raphtory.graphqlserver

  1"""
  2This module contains helper functions and classes for working with the GraphQL server for Raphtory.
  3Calling the run_server function will start the GraphQL server. If run in the background, this will return a
  4GraphQLServer object that can be used to run queries.
  5"""
  6
  7from raphtory import internal_graphql
  8import asyncio
  9import threading
 10import requests
 11import time
 12from raphtory import graphqlclient
 13
 14
[docs] 15class GraphQLServer: 16 """ 17 A helper class that can be used to query the Raphtory GraphQL server. 18 """ 19 20 def __init__(self, port): 21 self.port = port 22
[docs] 23 def query(self, query): 24 """ 25 Runs a GraphQL query on the server. 26 27 :param query(str): The GraphQL query to run. 28 29 :raises Exception: If the query fails to run. 30 31 :return: Returns the json-encoded content of a response, if any. 32 33 """ 34 r = requests.post("http://localhost:" + str(self.port), json={"query": query}) 35 if r.status_code == 200: 36 return r.json() 37 else: 38 raise Exception(f"Query failed to run with a {r.status_code}.")
39
[docs] 40 def wait_for_online(self): 41 """ 42 Waits for the server to be online. This is called automatically when run_server is called. 43 """ 44 while True: 45 try: 46 r = requests.get("http://localhost:" + str(self.port)) 47 if r.status_code == 200: 48 return True 49 except: 50 pass 51 time.sleep(1)
52 53 54async def _from_map_and_directory(graphs, graph_dir, port): 55 await internal_graphql.from_map_and_directory(graphs, graph_dir, port) 56 57 58async def _from_directory(graph_dir, port): 59 await internal_graphql.from_directory(graph_dir, port) 60 61 62async def _from_map(graphs, port): 63 await internal_graphql.from_map(graphs, port) 64 65 66def _run(func, daemon, port): 67 if daemon: 68 69 def _run_in_background(): 70 asyncio.run(func) 71 72 threading.Thread(target=_run_in_background, daemon=True).start() 73 server = GraphQLServer(port) 74 server.wait_for_online() 75 return graphqlclient.RaphtoryGraphQLClient("http://localhost:" + str(port)) 76 else: 77 loop = asyncio.get_event_loop() 78 loop.run_until_complete(func) 79 loop.close() 80 81
[docs] 82def run_server(graphs=None, graph_dir=None, port=1736, daemon=False): 83 """ 84 Runs the Raphtory GraphQL server. 85 86 Args: 87 graphs (dict, optional): A dictionary of graphs to load into the server. Default is None. 88 graph_dir (str, optional): The directory to load graphs from. Default is None. 89 port (int, optional): The port to run the server on. Default is 1736. 90 daemon (bool, optional): Whether to run the server in the background. Default is False. 91 92 Returns: 93 GraphQLServer: A GraphQLServer object that can be used to query the server. (Only if daemon is True) 94 """ 95 96 if graph_dir is not None and graphs is not None: 97 return _run(_from_map_and_directory(graphs, graph_dir, port), daemon, port) 98 elif graph_dir is not None: 99 return _run(_from_directory(graph_dir, port), daemon, port) 100 elif graphs is not None: 101 return _run(_from_map(graphs, port), daemon, port) 102 else: 103 print("No graphs or graph directory specified. Exiting.")