graphql-python/gql is the python GraphQL client of the Graphene family, which powers the backend (based on dronedeploy/graphene-tornado). There are a few others, not yet reviewed:
from pprint import pprint
from getpass import getpass
from gql import Client, gql
from gql.transport.requests import RequestsHTTPTransport
from IPython.display import JSON, IFrame
import requests # should investigate writing a tornado transport
This has to know where you are. For example for http://localhost:8888/lab
:
URL = "http://localhost:8888/graphql"
Since this is a kernel talking back to the notebook server, you'll need your jupyter notebook
or jupyter lab
token (view source, look for "token"
)
token = getpass()
client = Client(
transport=RequestsHTTPTransport(URL, use_json=True, headers=dict(Authorization=f"token {token}")),
fetch_schema_from_transport=True,
)
query = """{
contents(path: "notebooks/gql.ipynb") {
path
last_modified
... on NotebookContents {
content {
nbformat
nbformat_minor
cells {
edges {
node {
source
}
}
}
}
}
}
}
"""
query_gql = gql(query)
query_gql
Whatever that means. Lets actually run it!
result = client.execute(query_gql)
JSON(result)
Where can you go from here?
With a little work up-front and even less work at query time, the same types from above can be used to power live subscriptions. Right now, only contents are available, but many things in the notebook server and broader ecosystem could become "live".
subscription = f"subscription {query}"
print(subscription)
Go ahead and paste that in the iframe below and hit (▷)!
TODO: fix query param parsing!
IFrame(URL, width="100%", height="400px")