graphql-kit / graphql-voyager

🛰️ Represent any GraphQL API as an interactive graph
https://graphql-kit.com/graphql-voyager/
MIT License
7.69k stars 504 forks source link

How to use a schema instead of an introspection query in index.html? #355

Open zleonov opened 10 months ago

zleonov commented 10 months ago

Hi Team, first let me say what a great tool. Sorry if this belongs in the Discussions, but I didn't see any conversations there, so I am not sure if Discussions are being used.

What I am trying to do is create static documentation for our GraphQL API as part of our build and deployment process. I am using the SpectaQL tool and so far it's working great. I would also love to add an option to visualize our schema with GraphQL Voyager.

So let's make this simple. As proof of concept, I modified the index.html example in the following manner:

<!doctype html>
<html>
  <head>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/graphql-voyager/dist/voyager.css" />
    <script src="https://cdn.jsdelivr.net/npm/graphql-voyager/dist/voyager.standalone.js"></script>
  </head>
  <body>
    <div id="voyager" />
    <script>
        const data = ...

        GraphQLVoyager.renderVoyager(document.getElementById('voyager'), {
            introspection: JSON.parse(data)
        });
    </script>
  </body>
</html>

Where data is the result of executing an introspection query against our schema.

Works great! But during our build process I can't execute an introspection query. The only thing I have access to is our schema file.

I read some older issues and I see this seems to be a common question. I enabled allowToChangeSchema and played around with the tool, so I know it's possible to use an SDL string instead of the introspection query to render the schema. But I can't figure out how to do it (my knowledge of JS is limited).

I am trying to do something like this: const data = introspectionFromSchema(buildSchema(sdlText)) but it doesn't seem that buildSchema and introspectionFromSchema functions are available in voyager.standalone.js.

Are they minified? I am lost. Assuming I have const schema = ... how do I convert it to a JSON object representing the result of the introspection query?

Thanks a lot.

LunaticMuch commented 10 months ago

Your logic is correct, but the function introspectionFromSchema is not a Voyager function. It's a function provided by the GraphQL library, so you need to have it ready too. Maybe you could try from https://www.jsdelivr.com/package/npm/graphql

turnabout commented 9 months ago

Hi. I'm sorry if I'm hijacking this issue, but I'm having the exact same problematic. It's not for a build process specifically, but I'm trying to use Voyager on a static HTML page, using an SDL string as the input.

To gain access to the introspectionFromSchema and buildSchema functions, I'm importing them from a local file that itself imports them from the 'graphql' library and sets them to window.

But when launching the page, I just have an infinitely loading screen. No errors are logged in the console, and I'm a bit at a loss as to what I'm doing wrong or how to go about debugging this.

EDIT: I made a demo reproducing the issue on this repo: graphql-voyager-sdl-static-html-demo

It can be seen at this URL: https://turnabout.github.io/graphql-voyager-sdl-static-html-demo/

This may not be the optimal way to go about doing it, but right now I'm just trying to get something working. I'm not able to send an introspection query, all I have access to is an SDL string just like the OP.