graphql-voyager icon indicating copy to clipboard operation
graphql-voyager copied to clipboard

Using GraphQL Voyager without the endpoint

Open isha-talegaonkar opened this issue 2 years ago • 5 comments

My project does not have a graphql endpoint as such, but a bunch of input graphql schema files that I am getting using an API. I need to visualize the file that I am getting as input. Is there any way to do so?

isha-talegaonkar avatar Jul 13 '22 17:07 isha-talegaonkar

@isha-talegaonkar you can build an introspection from file and use it. If you check the demo, you can get some clues on how to make it.

LunaticMuch avatar Jul 13 '22 20:07 LunaticMuch

Hi, I am still unsure on how I can go about this. Can you please point me towards some files I can look at to understand better?

Thanks, Isha

On Wed, Jul 13, 2022 at 1:08 PM Stefano @.***> wrote:

@isha-talegaonkar https://github.com/isha-talegaonkar you can build an introspection from file and use it. If you check the demo, you can get some clues on how to make it.

— Reply to this email directly, view it on GitHub https://github.com/IvanGoncharov/graphql-voyager/issues/238#issuecomment-1183629654, or unsubscribe https://github.com/notifications/unsubscribe-auth/AH3FGXFODVXGXTGM4DESOETVT4O4NANCNFSM53PQNQ5A . You are receiving this because you were mentioned.Message ID: @.***>

isha-talegaonkar avatar Jul 14 '22 18:07 isha-talegaonkar

Is this possible with the standalone version? It's probably easiest to use sdlToIntrospection and then give the result as introspection object to the init function.... But I guess sdlToIntrospection isn't exposed?!

jobaeurle avatar Jun 13 '23 05:06 jobaeurle

@isha-talegaonkar you might need to provide more info regarding your project.

You can always build and dump a complete SDL or get an introspection. Almost any library can do it.

LunaticMuch avatar Jun 13 '23 06:06 LunaticMuch

Here is an example on how you can do this without an endpoint:

import {
    Source,
    parse,
    execute,
    buildSchema,
} from 'graphql'
import type {
    DocumentNode,
} from 'graphql'
import { Voyager, voyagerIntrospectionQuery } from 'graphql-voyager';


var schema = buildSchema(`
  type Account {
    Id: String
    Name: String
  }

  type Objects {
    Account: Account
  }

  type UIAPI {
    query: Objects
  }

  type Query {
    Account(Id: String): Account
    uiapi: UIAPI
  }
`)

async function retrieveDemoIntrospection(introspectionQuery){
    let documentAST: DocumentNode | undefined;
    try {
        //const params = await getGraphQLParams(request)
        documentAST = parse(new Source(introspectionQuery, 'GraphQL request'));
    } catch (syntaxError: unknown) {
        // Return 400: Bad Request if any syntax errors exist.
        //documentAST = new DocumentNode()
        throw new Error("failed")
    }

    return execute({
        schema,
        document: documentAST,
        rootValue: {},
        contextValue: {},
        variableValues: {},
        operationName: "IntrospectionQuery",
        fieldResolver: undefined,
        typeResolver: undefined,
    });
}

You can extend field and type resolver which can be very helpful when you schema is dynamic.

nicolas-despres avatar Oct 31 '23 05:10 nicolas-despres