Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
zleonov opened this issue Aug 17, 2023 · 3 comments
Open

Comments

@zleonov
Copy link

zleonov commented Aug 17, 2023

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.

@zleonov zleonov changed the title How to use a schema string instead of an introspection query in index.html? How to use a schema instead of an introspection query in index.html? Aug 17, 2023
@LunaticMuch
Copy link
Collaborator

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
Copy link

turnabout commented Sep 27, 2023

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.

@Lajcik
Copy link

Lajcik commented Sep 12, 2024

I wanted to do the same thing and ran into the same problem. What helped me figure it out was looking at the demo files: https://github.com/graphql-kit/graphql-voyager/blob/main/demo/presets/swapi_introspection.json

In short, the renderVoyager introspection parameter expects the actual introspection nested in a 'data' field:

    const data =  { "data" : introspectionFromSchema(buildSchema(sdl)) };

    // Render <Voyager /> into the body.
    GraphQLVoyager.renderVoyager(document.getElementById('voyager'), {
        introspection : data,
    });

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants