GraphQL Voyager

Represent any GraphQL API as an interactive graph

README

GraphQL Voyager


Represent any GraphQL API as an interactive graph. It's time to finally see the graph behind GraphQL.
You can also explore number of public GraphQL APIs from our list.

With graphql-voyager you can visually explore your GraphQL API as an interactive graph. This is a great tool when designing or discussing your data model. It includes multiple example GraphQL schemas and also allows you to connect it to your own GraphQL endpoint. What are you waiting for, explore your API!




voyager demo

Features


- Quick navigation on graph
- Left panel which provides more detailed information about every type
- "Skip Relay" option that simplifies graph by removing Relay wrapper classes
- Ability to choose any type to be a root of the graph

Usage


GraphQL Voyager exports Voyager React component and helper init function. If used without
module system it is exported as GraphQLVoyager global variable.

Properties


Voyager component accepts the following properties:

- introspection [object] - the server introspection response. If function is provided GraphQL Voyager will pass introspection query as a first function parameter. Function should return Promise which resolves to introspection response object.
- displayOptions _(optional)_
  - displayOptions.skipRelay [boolean, default true] - skip relay-related entities
  - displayOptions.skipDeprecated [boolean, default true] - skip deprecated fields and entities that contain only deprecated fields.
  - displayOptions.rootType [string] - name of the type to be used as a root
  - displayOptions.sortByAlphabet [boolean, default false] - sort fields on graph by alphabet
  - displayOptions.showLeafFields [boolean, default true] - show all scalars and enums
  - displayOptions.hideRoot [boolean, default false] - hide the root type
- allowToChangeSchema [boolean, default false] - allow users to change schema
- hideDocs [boolean, default false] - hide the docs sidebar
- hideSettings [boolean, default false] - hide settings panel
- hideVoyagerLogo [boolean, default true] - hide voyager logo

init function


The signature of the init function:

  1. ```js
  2. (hostElement: HTMLElement, options: object) => void
  3. ```

- hostElement - parent element
- options - is the JS object with properties ofVoyager component

Using pre-bundled version


You can get GraphQL Voyager bundle from the following places:

- jsDelivr
  - some exact version - https://cdn.jsdelivr.net/npm/graphql-voyager/v1.0.0-rc.15/voyager.min.js
  - latest version - https://cdn.jsdelivr.net/npm/graphql-voyager/dist/voyager.min.js
- from dist folder of the npm package graphql-voyager

The HTML with minimal setup (see the full example)

  1. ```html
  2. <!DOCTYPE html>
  3. <html>
  4.   <head>
  5.     <script src="https://cdn.jsdelivr.net/npm/react@16/umd/react.production.min.js"></script>
  6.     <script src="https://cdn.jsdelivr.net/npm/react-dom@16/umd/react-dom.production.min.js"></script>
  7.     <link
  8.       rel="stylesheet"
  9.       href="https://cdn.jsdelivr.net/npm/graphql-voyager/dist/voyager.css"
  10.     />
  11.     <script src="https://cdn.jsdelivr.net/npm/graphql-voyager/dist/voyager.min.js"></script>
  12.   </head>
  13.   <body>
  14.     <div id="voyager">Loading...</div>
  15.     <script>
  16.       // do a call to server using voyagerIntrospectionQuery provided
  17.       const query = GraphQLVoyager.voyagerIntrospectionQuery;
  18.       const introspection = fetch('<server url>', {
  19.         method: 'post',
  20.         headers: {
  21.           Accept: 'application/json',
  22.           'Content-Type': 'application/json',
  23.         },
  24.         body: JSON.stringify({ query }),
  25.         // ...
  26.       }).then((response) => response.json());
  27.       // or just return pre-fetched introspection
  28.       // Render <Voyager />
  29.       GraphQLVoyager.init(document.getElementById('voyager'), {
  30.         introspection: introspection,
  31.       });
  32.     </script>
  33.   </body>
  34. </html>
  35. ```

Using as a dependency


You can install lib using npm or yarn:

    npm i --save graphql-voyager
    yarn add graphql-voyager

And then use it:

  1. ```js
  2. import * as React from 'react';
  3. import * as ReactDOM from 'react-dom';
  4. import { Voyager, voyagerIntrospectionQuery } from 'graphql-voyager';

  5. const introspection = fetch(window.location.origin + '/graphql', {
  6.   method: 'post',
  7.   headers: { 'Content-Type': 'application/json' },
  8.   body: JSON.stringify({ query: voyagerIntrospectionQuery }),
  9. }).then((response) => response.json());

  10. ReactDOM.render(
  11.   <Voyager introspection={introspection} />,
  12.   document.getElementById('voyager'),
  13. );
  14. ```

Build for the web with webpack (example) or

Middleware


Graphql Voyager has middleware for the next frameworks:

Properties


Middleware supports the following properties:

- endpointUrl [string] - the GraphQL endpoint url.
- displayOptions [object] - same as here
- `headersJS` [`string`, default `"{}"`] - object of headers serialized in string to be used on endpoint url
  Note: You can also use any JS expression which results in an object with header names as keys and strings as values e.g. { Authorization: localStorage['Meteor.loginToken'] }

Express


  1. ```js
  2. import express from 'express';
  3. import { express as voyagerMiddleware } from 'graphql-voyager/middleware';

  4. const app = express();

  5. app.use('/voyager', voyagerMiddleware({ endpointUrl: '/graphql' }));

  6. app.listen(3001);
  7. ```

Hapi


Version 17+


  1. ```js
  2. import hapi from 'hapi';
  3. import { hapi as voyagerMiddleware } from 'graphql-voyager/middleware';

  4. const server = new Hapi.Server({
  5.   port: 3001,
  6. });

  7. const init = async () => {
  8.   await server.register({
  9.     plugin: voyagerMiddleware,
  10.     options: {
  11.       path: '/voyager',
  12.       endpointUrl: '/graphql',
  13.     },
  14.   });

  15.   await server.start();
  16. };

  17. init();
  18. ```

Koa


  1. ```js
  2. import Koa from 'koa';
  3. import KoaRouter from 'koa-router';
  4. import { koa as voyagerMiddleware } from 'graphql-voyager/middleware';

  5. const app = new Koa();
  6. const router = new KoaRouter();

  7. router.all(
  8.   '/voyager',
  9.   voyagerMiddleware({
  10.     endpointUrl: '/graphql',
  11.   }),
  12. );

  13. app.use(router.routes());
  14. app.use(router.allowedMethods());
  15. app.listen(3001);
  16. ```

Credits


This tool is inspired by graphql-visualizer project.