o
    [h'                     @   s   d dl Z d dlZd dlmZ d dlmZ d dlmZ d dlm	Z	 d dl
mZmZmZmZ d dlmZmZmZ zd dlZd dlmZmZ d d	lmZ W n ey[   dZdZdZdZY nw G d
d dZdZdS )    N)status)BackgroundTasks)run_in_threadpool)Request)HTMLResponseJSONResponsePlainTextResponseResponse)ReceiveScopeSend)GraphQLErrorformat_error)AsyncioExecutorc                   @   s~   e Zd Z			ddddejdededdf
d	d
Zdede	de
ddfddZdedefddZ	dddZdedefddZdS )
GraphQLAppNTschemazgraphene.Schemaexecutorexecutor_classgraphiqlreturnc                 C   sT   || _ || _|d u r|| _|| _|d uot|t| _d S || _d | _t|t| _d S )N)r   r   r   r   
issubclassr   is_async
isinstance)selfr   r   r   r    r   x/home/ubuntu/experiments/live_experiments/Pythonexperiments/Otree/venv/lib/python3.10/site-packages/starlette/graphql.py__init__   s   

zGraphQLApp.__init__scopereceivesendc                    sR   | j d u r| jd ur|  | _ t||d}| |I d H }||||I d H  d S )N)r   )r   r   r   handle_graphql)r   r   r   r   requestresponser   r   r   __call__/   s   
zGraphQLApp.__call__r!   c                    s  |j dv r%d|jddv r!| jstdtjdS | |I d H S |j}nC|j dkra|jdd}d	|v r=|	 I d H }n+d
|v rQ|
 I d H }| }d|i}nd|jv rZ|j}ntdtjdS tdtjdS z|d }|d}|d}W n ty   tdtjd Y S w t }	||	d}
| j|||
|dI d H }|jrdd |jD nd }d|ji}|r||d< |jrtjntj}t|||	dS )N)GETHEADz	text/htmlAccept z	Not Found)status_codePOSTzContent-Typezapplication/jsonzapplication/graphqlqueryzUnsupported Media TypezMethod Not Allowed	variablesZoperationNamez%No GraphQL query found in the request)r!   
background)r+   contextoperation_namec                 S   s   g | ]}t |qS r   )format_graphql_error).0errr   r   r   
<listcomp>i   s    z-GraphQLApp.handle_graphql.<locals>.<listcomp>dataerrors)r(   r,   )methodheadersgetr   r   r   ZHTTP_404_NOT_FOUNDhandle_graphiqlZquery_paramsjsonbodydecodeZHTTP_415_UNSUPPORTED_MEDIA_TYPEZHTTP_405_METHOD_NOT_ALLOWEDKeyErrorZHTTP_400_BAD_REQUESTr   executer4   r3   ZHTTP_200_OKr   )r   r!   r3   content_typer:   textr*   r+   r.   r,   r-   resultZ
error_dataZresponse_datar(   r   r   r   r    7   sj   







zGraphQLApp.handle_graphqlc                    sD   | j r| jj|||| jd|dI d H S t| jj||||dI d H S )NT)r+   r.   r   Zreturn_promiser-   )r+   r.   r-   )r   r   r=   r   r   )r   r*   r+   r-   r.   r   r   r   r=   x   s"   	zGraphQLApp.executec                    s    t dt|jj}t|S )Nz{{REQUEST_PATH}})GRAPHIQLreplacer9   dumpsurlpathr   )r   r!   r?   r   r   r   r8      s   zGraphQLApp.handle_graphiql)NNT)NNN)__name__
__module____qualname__typingAnytypeboolr   r   r
   r   r#   r   r	   r    r=   r8   r   r   r   r   r      s(    
B
r   a
  
<!--
 *  Copyright (c) Facebook, Inc.
 *  All rights reserved.
 *
 *  This source code is licensed under the license found in the
 *  LICENSE file in the root directory of this source tree.
-->
<!DOCTYPE html>
<html>
  <head>
    <style>
      body {
        height: 100%;
        margin: 0;
        width: 100%;
        overflow: hidden;
      }
      #graphiql {
        height: 100vh;
      }
    </style>
    <!--
      This GraphiQL example depends on Promise and fetch, which are available in
      modern browsers, but can be "polyfilled" for older browsers.
      GraphiQL itself depends on React DOM.
      If you do not want to rely on a CDN, you can host these files locally or
      include them directly in your favored resource bunder.
    -->
    <link href="//cdn.jsdelivr.net/npm/graphiql@0.12.0/graphiql.css" rel="stylesheet"/>
    <script src="//cdn.jsdelivr.net/npm/whatwg-fetch@2.0.3/fetch.min.js"></script>
    <script src="//cdn.jsdelivr.net/npm/react@16.2.0/umd/react.production.min.js"></script>
    <script src="//cdn.jsdelivr.net/npm/react-dom@16.2.0/umd/react-dom.production.min.js"></script>
    <script src="//cdn.jsdelivr.net/npm/graphiql@0.12.0/graphiql.min.js"></script>
  </head>
  <body>
    <div id="graphiql">Loading...</div>
    <script>
      /**
       * This GraphiQL example illustrates how to use some of GraphiQL's props
       * in order to enable reading and updating the URL parameters, making
       * link sharing of queries a little bit easier.
       *
       * This is only one example of this kind of feature, GraphiQL exposes
       * various React params to enable interesting integrations.
       */
      // Parse the search string to get url parameters.
      var search = window.location.search;
      var parameters = {};
      search.substr(1).split('&').forEach(function (entry) {
        var eq = entry.indexOf('=');
        if (eq >= 0) {
          parameters[decodeURIComponent(entry.slice(0, eq))] =
            decodeURIComponent(entry.slice(eq + 1));
        }
      });
      // if variables was provided, try to format it.
      if (parameters.variables) {
        try {
          parameters.variables =
            JSON.stringify(JSON.parse(parameters.variables), null, 2);
        } catch (e) {
          // Do nothing, we want to display the invalid JSON as a string, rather
          // than present an error.
        }
      }
      // When the query and variables string is edited, update the URL bar so
      // that it can be easily shared
      function onEditQuery(newQuery) {
        parameters.query = newQuery;
        updateURL();
      }
      function onEditVariables(newVariables) {
        parameters.variables = newVariables;
        updateURL();
      }
      function onEditOperationName(newOperationName) {
        parameters.operationName = newOperationName;
        updateURL();
      }
      function updateURL() {
        var newSearch = '?' + Object.keys(parameters).filter(function (key) {
          return Boolean(parameters[key]);
        }).map(function (key) {
          return encodeURIComponent(key) + '=' +
            encodeURIComponent(parameters[key]);
        }).join('&');
        history.replaceState(null, null, newSearch);
      }
      // Defines a GraphQL fetcher using the fetch API. You're not required to
      // use fetch, and could instead implement graphQLFetcher however you like,
      // as long as it returns a Promise or Observable.
      function graphQLFetcher(graphQLParams) {
        // This example expects a GraphQL server at the path /graphql.
        // Change this to point wherever you host your GraphQL server.
        return fetch({{REQUEST_PATH}}, {
          method: 'post',
          headers: {
            'Accept': 'application/json',
            'Content-Type': 'application/json',
          },
          body: JSON.stringify(graphQLParams),
          credentials: 'include',
        }).then(function (response) {
          return response.text();
        }).then(function (responseBody) {
          try {
            return JSON.parse(responseBody);
          } catch (error) {
            return responseBody;
          }
        });
      }
      // Render <GraphiQL /> into the body.
      // See the README in the top level of this module to learn more about
      // how you can customize GraphiQL by providing different values or
      // additional child elements.
      ReactDOM.render(
        React.createElement(GraphiQL, {
          fetcher: graphQLFetcher,
          query: parameters.query,
          variables: parameters.variables,
          operationName: parameters.operationName,
          onEditQuery: onEditQuery,
          onEditVariables: onEditVariables,
          onEditOperationName: onEditOperationName
        }),
        document.getElementById('graphiql')
      );
    </script>
  </body>
</html>
)r9   rI   Z	starletter   Zstarlette.backgroundr   Zstarlette.concurrencyr   Zstarlette.requestsr   Zstarlette.responsesr   r   r   r	   Zstarlette.typesr
   r   r   ZgrapheneZgraphql.errorr   r   r/   Z#graphql.execution.executors.asyncior   ImportErrorr   rA   r   r   r   r   <module>   s(    |