从模式对象生成 GraphQL schema.json

Generate GraphQL schema.json from a schema object

我正在尝试在没有服务器的情况下针对 GQL 模式生成内省查询的结果。我能够创建架构:

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
    }
`);

但我不确定如何生成 json 表示。

您可以手动执行任何查询,包括内省查询:

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

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