Skip to content

mstachniuk/graphql-schema-from-introspection-generator

Repository files navigation

GraphQL Schema from Introspection generator

MIT License Build Status Maven Central Bintray codecov

This library helps you generate GraphQL Schema (also called GraphQL DSL or SDL) based on Introspection Query response. It's useful when you use graphql-java and Code First approach and want to migrate to Schema First approach.

How to use it?

  1. Download Command Line Tool from releases page.

  2. Run java -jar graphql-schema-from-introspection-generator-cli-X.X.X.jar input.json output.graphqls

    File input.json should contain the output of GrpahQL Introspection query. If you don't have this file yet, you can use one from: core/src/test/resources/testdata/

  3. In output.graphqls you will find generated GraphQL Schema.

How get Introspection Query result?

  1. Run your application.

  2. Run

    Introspection Query
        query IntrospectionQuery {
          __schema {
            queryType { name }
            mutationType { name }
            subscriptionType { name }
            types {
              ...FullType
            }
            directives {
              name
              description
              locations
              args {
                ...InputValue
              }
            }
          }
        }
      
        fragment FullType on __Type {
          kind
          name
          description
          fields(includeDeprecated: true) {
            name
            description
            args {
              ...InputValue
            }
            type {
              ...TypeRef
            }
            isDeprecated
            deprecationReason
          }
          inputFields {
            ...InputValue
          }
          interfaces {
            ...TypeRef
          }
          enumValues(includeDeprecated: true) {
            name
            description
            isDeprecated
            deprecationReason
          }
          possibleTypes {
            ...TypeRef
          }
        }
      
        fragment InputValue on __InputValue {
          name
          description
          type { ...TypeRef }
          defaultValue
        }
      
        fragment TypeRef on __Type {
          kind
          name
          ofType {
            kind
            name
            ofType {
              kind
              name
              ofType {
                kind
                name
                ofType {
                  kind
                  name
                  ofType {
                    kind
                    name
                    ofType {
                      kind
                      name
                      ofType {
                        kind
                        name
                      }
                    }
                  }
                }
              }
            }
          }
        }
    

    This query based on Introspection Queries in graphql-java and GraphiQL projects.

  3. Store result in a file and use Command Line tool for generating the schema (See: How to use it?).

Release Notes

Release notes: docs/release-notes.md

How to build project?

  1. Clone repo
  2. Run ./gradlew build
  3. You can find Command Line Tool in cli/build/libs
  4. You can find core library in core/build/libs

Another usage

You can use the core library in your projects if you want. Just add a dependency (in Gradle):

compile group: 'io.github.mstachniuk', name: 'graphql-schema-from-introspection-generator-core'

How to contribute?

Please Send PR's, issues and feedback via GitHub.

Alternatives

During finishing this project I found that similar tool already exists in graphql-java project, see IntrospectionResultToSchema class.

Another possibility is to use graphql-js and this code snippet (NodeJS):

const graphql = require("graphql");
const schema = require("path/to/schema.json");

const clientSchema = graphql.buildClientSchema(schema.data);
const schemaString = graphql.printSchema(clientSchema);
console.log(schemaString)

Unfortunately, I didn't know that before :-(