{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Query the notebook ContentsManager with GraphQL\n",
    "[<i class=\"fa fa-github\"></i> graphql-python/gql](https://github.com/graphql-python/gql) is the python GraphQL client of the [Graphene](https://graphene-python.org/) family, which  powers the backend (based on [<i class=\"fa fa-github\"></i> dronedeploy/graphene-tornado](https://github.com/dronedeploy/graphene-tornado)). There are a few others, not yet reviewed:\n",
    "- [<i class=\"fa fa-github\"></i> ariebovenberg/quiz](https://github.com/ariebovenberg/quiz)\n",
    "- [<i class=\"fa fa-github\"></i> prisma/python-graphql-client](https://github.com/prisma/python-graphql-client)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from pprint import pprint\n",
    "from getpass import getpass\n",
    "from gql import Client, gql\n",
    "from gql.transport.requests import RequestsHTTPTransport\n",
    "from IPython.display import JSON, IFrame\n",
    "import requests  # should investigate writing a tornado transport"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Client!"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This has to know where you are. For example for `http://localhost:8888/lab`:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "URL = \"http://localhost:8888/graphql\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "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\"`)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "token = getpass()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "client = Client(\n",
    "    transport=RequestsHTTPTransport(URL, use_json=True, headers=dict(Authorization=f\"token {token}\")),\n",
    "    fetch_schema_from_transport=True,\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Query!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "query = \"\"\"{\n",
    "  contents(path: \"notebooks/gql.ipynb\") {\n",
    "    path\n",
    "    last_modified\n",
    "    ... on NotebookContents {\n",
    "      content {\n",
    "        nbformat\n",
    "        nbformat_minor\n",
    "        cells {\n",
    "          edges {\n",
    "            node {\n",
    "              source\n",
    "            }\n",
    "          }\n",
    "        }\n",
    "      }\n",
    "    }\n",
    "  }\n",
    "}\n",
    "\"\"\"\n",
    "query_gql = gql(query)\n",
    "query_gql"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Whatever that means. Lets actually run it!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "result = client.execute(query_gql)\n",
    "JSON(result)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Where can you go from here? "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Subscribe!\n",
    "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\"."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "subscription = f\"subscription {query}\"\n",
    "print(subscription)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Go ahead and paste that in the iframe below and hit (▷)!\n",
    "> TODO: fix query param parsing!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "IFrame(URL, width=\"100%\", height=\"400px\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}