Giter Site home page Giter Site logo

hasura / sphinx-graphiql Goto Github PK

View Code? Open in Web Editor NEW
57.0 7.0 9.0 707 KB

Sphinx plugin that adds a GraphiQL directive so that you can embed an interactive GraphQL query explorer in your docs

License: MIT License

Python 0.11% JavaScript 97.44% CSS 2.32% HTML 0.13%

sphinx-graphiql's Introduction

Sphinx GraphiQL

This is a GraphiQL plugin for Sphinx that lets you make GraphQL queries from your docs.

We built this for documenting Hasura GraphQL engine's API. Check it out in action here. (Note: In our docs we have added custom css overrides to make GraphiQL look as per our needs)

example

Usage

To insert a GraphiQL component inside your .rst doc, use the declarative:

.. graphiql::
   :query:
      query {
         author {
            id
            name
         }
      }

View only

If you want to make GraphiQL view-only (ie: disable execution), you just have to add another option :view_only:. For example:

.. graphiql::
   :view_only:
   :query:
      query {
         author {
            id
            name
         }
      }

Show a dummy response

Sometimes you will want to show the response along with the query without executing it. You can do that by adding a :response: option.

.. graphiql::
   :view_only:
   :query:
      query {
         author {
            id
            name
         }
      }
   :response:
      {
         "data": {
            "author": [
               {
                  "id": 1
                  "name": "Justin",
               },
               {
                  "id": 2
                  "name": "Beltran",
               },
               {
                  "id": 3
                  "name": "Sidney",
               }
           ]
        }
     }

Custom endpoint

By default, the GraphQL endpoint is picked up from an environment variable as described here. In case you want to explicitly set an endpoint for a query, you can do so by adding an :endpoint: option.

.. graphiql::
   :endpoint: http://localhost:8080/v1/graphql
   :query:
      query {
         author {
            id
            name
         }
      }

Installation

Step 1: Install the plugin

$ pip install sphinx_graphiql

Step 2: Mention the plugin as an extension in conf.py

You might be using other extensions in your docs. Append sphinx_graphiql to the list of extensions.

extensions.append('sphinx_graphiql')

Step 3: Add the required scripts to your template HTML

Add the following tags inside the <head></head> of your template html file (typically layout.html).

<!-- GraphiQL -->
<script src="//cdn.jsdelivr.net/react/15.4.2/react.min.js"></script>
<script src="//cdn.jsdelivr.net/react/15.4.2/react-dom.min.js"></script>
<script src="https://rawgit.com/hasura/sphinx_graphiql/master/static/graphiql/graphiql.min.js"></script>
<link href="https://rawgit.com/hasura/sphinx_graphiql/master/static/graphiql/graphiql.css" rel="stylesheet">
<link href="https://rawgit.com/hasura/sphinx_graphiql/master/static/styles.css" rel="stylesheet">
<script type="text/javascript">
  // graphql query fetcher
  const graphQLFetcher = function(endpoint) {
    endpoint = endpoint || "{{ GRAPHIQL_DEFAULT_ENDPOINT }}";
    return function(graphQLParams) {
      const params = {
        method: 'post',
        headers: {
          'Accept': 'application/json',
          'Content-Type': 'application/json'
        },
        body: JSON.stringify(graphQLParams),
        credentials: 'include'
      };
      return fetch(endpoint, params)
        .then(function (response) {
          return response.text();
        })
        .then(function (responseBody) {
          try {
            return JSON.parse(responseBody);
          } catch (error) {
            return responseBody;
          }
        });
    }
  };
   
  // create GraphiQL components and embed into HTML
  const setupGraphiQL = function() {
    if (typeof(React) === 'undefined' || typeof(ReactDOM) === 'undefined' || typeof(GraphiQL) === 'undefined') {
      return;
    }
   
    const targets = document.getElementsByClassName('graphiql');
    for (let i = 0; i < targets.length; i++) {
      const target = targets[i];
      const endpoint = target.getElementsByClassName("endpoint")[0].innerHTML.trim();
      const query = target.getElementsByClassName("query")[0].innerHTML.trim();
      const response = target.getElementsByClassName("response")[0].innerHTML.trim();
      const graphiQLElement = React.createElement(GraphiQL, {
        fetcher: graphQLFetcher(endpoint),
        schema: null, // TODO: Pass undefined to fetch schema via introspection
        query: query,
        response: response
      });
      ReactDOM.render(graphiQLElement, target);
    }
  };
                                       
  // if graphiql elements present, setup graphiql
  if (document.getElementsByClassName('graphiql').length > 0) {
    setupGraphiQL();
  }
</script>

You can find these tags at static/static.html of the root directory.

Configuration

Default GraphQL Endpoint

You have to set the GraphQL endpoint as an environment variable in your sphinx configuration file (typically conf.py at the root your your project).

For example:

GRAPHIQL_DEFAULT_ENDPOINT = "https://graphql.my-graphql-app.io/v1/graphql"

Auto-completion

GraphiQL uses the GraphQL schema to auto complete as you type in queries and mutations.

If your GraphQL endpoint supports introspection, just pass undefined as the schema variable and auto-completion will work out of the box.

const graphiQLElement = React.createElement(GraphiQL, {
  fetcher: graphQLFetcher(endpoint),
  schema: undefined, // the schema will be fetched using introspection
  query: query,
  response: response
});

sphinx-graphiql's People

Contributors

coco98 avatar rikinsk avatar wawhal avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

sphinx-graphiql's Issues

What is the template html file?

The docs mention that it is typically layout.html, but can you be a bit more specific of what precisely we need to modify.

GraphiQL component isn't rendering

Hi! Thank you for this tool. I'm having a little problem trying to make it work.
It isn't rendering the GraphiQL component, it's only showing "loading".

  • I made the changes from #1 and #3 which were a problem with my python version.
  • I'm using the read the docs theme for sphinx which is properly configured.
  • Changed correctly the head of the theme template which is being correctly rendered. (using the dev tools I can see that everything is fine, including the js and css files.
  • I changed the url of react because it was looking in my local (just added https://)
  • Downloaded thet js and css files because rawgit is no longer working
  • Manually installed fett in my venv

I'm using:

Django 2.2.4
Python 3.7.3
Sphinx 2.1.2
sphinx-rtd-theme 0.4.3

exception: cannot import name 'SphinxGraphiQL'

Hiya, thanks for putting this together - this looks exactly what I need but I've followed the instructions on the README and I get

Could not import extension sphinx_graphiql (exception: cannot import name 'SphinxGraphiQL')

when I try and generate my docs.
I'm using Python 3 and Sphinx 1.8.1

I noticed there were a couple of pull requests (#1, #2) that may relate to this.

Thanks in advance

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.