Guig
Guig

Reputation: 10169

Generate GraphQL schema.json from a schema object

I'm trying to generate the result of the introspection query against a GQL schema, without having a server. I'm able to create the schema:

var { buildSchema } = require('graphql');

var schema = buildSchema(`
    schema {
        query: QueryType
    }

    type QueryType {
        hero(episode: Episode): Character
        human(id : String) : Human
        droid(id: ID!): Droid
        charactersInEpisod(episode: Episode): [Character!]!
    }

    enum Episode {
        NEWHOPE
        EMPIRE
        JEDI
    }

    interface Character {
        id: ID!
        name: String!
        friends: [Character]
        appearsIn: [Episode]!
    }

    type Human implements Character {
        id: ID!
        name: String!
        friends: [Character]
        appearsIn: [Episode]!
        homePlanet: String
    }

    type Droid implements Character {
        id: ID!
        name: String!
        friends: [Character]
        appearsIn: [Episode]!
        primaryFunction: String
    }
`);

But I'm then unsure about how to generate the json representation.

Upvotes: 0

Views: 721

Answers (1)

Daniel Rearden
Daniel Rearden

Reputation: 84657

You can manually execute any query, including the introspection query:

const { buildSchema, getIntrospectionQuery, graphql } = require('graphql')

const schema = buildSchema(...)
const { data } = await graphql(schema, getIntrospectionQuery())

Upvotes: 1

Related Questions