Skip to content

Commit

Permalink
Graphql hotfix
Browse files Browse the repository at this point in the history
  • Loading branch information
daviddavo committed Sep 8, 2022
1 parent cb272f4 commit 82c4677
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 17 deletions.
19 changes: 12 additions & 7 deletions cache_scripts/common/api_requester.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<[email protected]>
"""

from gql import Client
from gql import Client, gql
from gql.dsl import DSLField, DSLQuery, DSLSchema, DSLType, dsl_gql
from gql.transport.requests import RequestsHTTPTransport
import re
Expand Down Expand Up @@ -80,33 +80,38 @@ def complete(self):
class GQLRequester:
ELEMS_PER_CHUNK: int = 1000

def __init__(self, endpoint: str, pbar_enabled: bool=True) -> None:
def __init__(self, endpoint: str, pbar_enabled: bool=True, introspection=True) -> None:
self.__transport = RequestsHTTPTransport(endpoint)
self.__client: Client = Client(transport=self.__transport, fetch_schema_from_transport=True)
self.__client: Client = Client(transport=self.__transport, fetch_schema_from_transport=introspection)
self.pbar = IndexProgressBar if pbar_enabled else RequestProgressSpinner

logging.debug(f"Invoked ApiRequester with endpoint: {endpoint}")

def get_schema(self) -> DSLSchema:
with self.__client as _:
with self.__client:
assert(self.__client.schema is not None)
return DSLSchema(self.__client.schema)

def request(self, query: Union[DSLQuery, DSLField]) -> Dict:
def request(self, query: DSLQuery | DSLField | str) -> Dict:
"""
Requests data from endpoint.
"""
if isinstance(query, DSLField):
query = DSLQuery(query)

logging.debug(f"Requesting: {query}")
result = self.__client.execute(dsl_gql(query))

if isinstance(query, DSLQuery):
result = self.__client.execute(dsl_gql(query))
else:
result = self.__client.execute(gql(query))

if "errors" in result:
raise GQLQueryException(result["errors"])

return result

def request_single(self, q: Union[DSLQuery, DSLField]) -> Dict:
def request_single(self, q: DSLQuery | DSLField | str) -> Dict:
result = self.request(q)
if result and len(result.values()) == 1:
return next(iter(result.values()))
Expand Down
23 changes: 13 additions & 10 deletions cache_scripts/common/graphql.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,19 @@ def wrapper(**kwargs):
def checkSubgraphHealth(endpoint: str, network: str = None):
subgraphName = '/'.join(endpoint.split('/')[-2:])

requester = GQLRequester(endpoint=ENDPOINTS['_theGraph']['index-node'])
ds = requester.get_schema()
q = ds.Query.indexingStatusForCurrentVersion(subgraphName=subgraphName).select(
ds.SubgraphIndexingStatus.health,
ds.SubgraphIndexingStatus.synced,
ds.SubgraphIndexingStatus.node,
ds.SubgraphIndexingStatus.chains.select(
ds.ChainIndexingStatus.network
)
)
requester = GQLRequester(endpoint=ENDPOINTS['_theGraph']['index-node'], introspection=False)
q = f"""
{{
indexingStatusForCurrentVersion(subgraphName: "{subgraphName}") {{
health,
synced,
node,
chains {{
network
}}
}}
}}
"""

r = requester.request_single(q)

Expand Down

0 comments on commit 82c4677

Please sign in to comment.