GraphQL Voyager

Represent any GraphQL API as an interactive graph

README

GraphQL Voyager


graphql-voyager logo

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 or function: (query: string) => Promise] - 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
- hideDocs [boolean, default false] - hide the docs sidebar
- hideSettings [boolean, default false] - hide settings panel
- workerURI [string] _(optional)_ - absolute or relative path to Voyager web worker. By default it will try to load it from voyager.worker.js.
- `loadWorker` [function: `(path: string, relative: boolean) => Promise`] _(optional)_ - If provided GraphQL Voyager will use this function to load the worker. By default it will use the internal callback in `utils/index.ts`

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

Important: for the latest two options make sure to copy voyager.worker.js to the same
folder as voyager.min.js.

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.       function introspectionProvider(introspectionQuery) {
  17.         // ... do a call to server using introspectionQuery provided
  18.         // or just return pre-fetched introspection
  19.       }
  20.       // Render <Voyager />
  21.       GraphQLVoyager.init(document.getElementById('voyager'), {
  22.         introspection: introspectionProvider,
  23.       });
  24.     </script>
  25.   </body>
  26. </html>
  27. ```

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 } from 'graphql-voyager';

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

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

Build for the web with webpack (example) or

Important: make sure to copy voyager.worker.js from node_modules/graphql-voyager/dist to the same folder as your main bundle or use [workerURI](#properties) property to specify other path.

NOTE if you use it with create-react-app, copy worker file to public folder and use workerURI property like this:

  1. ``` js
  2. <Voyager
  3.   // ...
  4.   workerURI={process.env.PUBLIC_URL + '/voyager.worker.js'}
  5.   // ...
  6. />
  7. ```

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.