Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for Graphql easy/lazy documentation
Yury Troynov
Yury Troynov

Posted on

     

Graphql easy/lazy documentation

Hi, today I wanted to share with you an easy way to visualize your Graphql schema.

Prefer watching to reading, :) here is video for ya :)

When you hear word documentation, you're probably picturing something which reminds you Apollo studio doc page or swagger doc page.

And don’t get me wrong, those kinds of documentation are good enough, but when I think about it, I think that I would like to see something like a bird eye view of your documentation,

where you could quickly jump between different parts of your visualized schema and see what is connected to what.

For example, something like bird eye view VS Code

Image description

or something a little bit closer to what we will have at the end of this tutorial, something like SQL builder in PhpMyAdmin :)

Image description

So what I wanted to share with you today is Voyager.

Image description

There are a few ways to integrate it into your project. Here I will show you how to do it as a standalone application.

I choose standalone version cause:

  1. Don’t want to mix documentation with actual BE or FE code
  2. Don’t want to rebuild it after any changes to schema (kinda build once, use always)
  3. Don’t want to extend CI time even with few extra seconds

But if you want to integrate it to build process, feel free to checkvoyager documentation for more details. Or left a comment that you need it 🙂

So let’s start

mkdirgraphql-voyager-example&&cdgraphql-voyager-example&& npm init--yes
Enter fullscreen modeExit fullscreen mode

Install deps

npminstall @apollo/server graphql serve npm-run-all
Enter fullscreen modeExit fullscreen mode

extend package.json

{//...etc."type":"module","scripts":{"start:gql":"node gql-server/index.js","start:serve":"serve -C voyager","start":"run-p start:gql start:serve"}//otherdependencies}
Enter fullscreen modeExit fullscreen mode

create GQL server

mkdirgql-server&&touchgql-server/index.js&&touchgql-server/schema.js
Enter fullscreen modeExit fullscreen mode

schema.js

typeRoot{query:Querymutation:Mutation}exportconsttypeDefs=`#graphqltypeTodo{id:ID!content:String!completed:Boolean!}typeQuery{todos:[Todo!]!}inputAddTodoMutationInput{content:String!}inputUpdateTodoMutationInput{id:ID!content:Stringcompleted:Boolean}typeMutation{addTodo(input:AddTodoMutationInput!):Todo!updateTodo(input:UpdateTodoMutationInput!):Todo!}`;
Enter fullscreen modeExit fullscreen mode

server index.js

import{ApolloServer}from'@apollo/server';import{startStandaloneServer}from'@apollo/server/standalone';import{typeDefs}from'./schema.js'constserver=newApolloServer({typeDefs,resolvers:{},});const{url}=awaitstartStandaloneServer(server,{listen:{port:4000},});console.log(`🚀Serverreadyat:${url}`);
Enter fullscreen modeExit fullscreen mode

voyager/index.js

constintrospectionProvider=(introspectionQuery)=>fetch('http://localhost:4000',{method:'post',headers:{Accept:'application/json','Content-Type':'application/json',},body:JSON.stringify({query:introspectionQuery,}),}).then((response)=>response.json())// Render <Voyager /> into the body.GraphQLVoyager.init(document.getElementById('voyager'),{introspection:introspectionProvider,displayOptions:{rootType:'Root',}});
Enter fullscreen modeExit fullscreen mode

voyager/index.html

<!DOCTYPE html><html><head><scriptsrc="https://cdn.jsdelivr.net/npm/react@18/umd/react.production.min.js"></script><scriptsrc="https://cdn.jsdelivr.net/npm/react-dom@18/umd/react-dom.production.min.js"></script><linkrel="stylesheet"href="https://cdn.jsdelivr.net/npm/graphql-voyager/dist/voyager.css"/><scriptsrc="https://cdn.jsdelivr.net/npm/graphql-voyager/dist/voyager.min.js"></script></head><style>#voyager{height:95vh;}</style><body><divid="voyager">Loading...</div><scriptsrc="./voyager.js"></script></body></html>
Enter fullscreen modeExit fullscreen mode

You could use a standard approach and install everything through NPM, but I think this approach is enough for such a small project.
Source code (GitHub)

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

  • Location
    limassol, Cyprus
  • Education
    Clark University, MA, USA
  • Work
    Frontend developer at Pay.com
  • Joined

More fromYury Troynov

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp