2017-07-31 13 views
2

モジュール化されたgraphqlスキーマをjsonスキーマに変換するためのスタンドアロンツールはありますか?サーバーを起動せずにjsonスキーマを生成しますか?

私は、apollo-graphqlとgraphql-tools makeExecutableSchemaを使用しているgraphqlサーバーを持っています。パターンがhere

// schema.js 
import { makeExecutableSchema } form 'graphql-tools'; 
const Author = `type Author { ... }`; 
const Post = `type Post { ... }`; 
const Query = `type Query { ... }`; 

export const typeDefs = [Author, Post, Query]; 

export const schema = makeExecutableSchema({ 
    typeDefs: typeDefs, 
    resolvers: { ... }, 
}); 

どのように私はschema.jsonフォームtypeDefsまたはschemaのいずれかを作成することができますを説明し、次の?


私はrelay-compilerapollo-codegenを使用するJSONスキーマを必要としています。 apollo-codegenは... graphqlサーバーからスキーマを作成するには、このスクリプトを含んで

apollo-codegen introspect-schema http://localhost:8080/graphql --output schema.json 

...しかし、私は、スキーマを作成し、自動ビルドでは、アポロ・コード生成を実行したいです。私はサーバーを作りたくありません。


私は答えとしてこれを提出するだろうが、質問がオフトピック¯マークされている\ _(ツ)_ /¯

ダニエル・rearden @からの答えは私を指摘正しい方向に。 makeExecutableSchemaGraphQLSchemaを返します。graphqlprintSchemaintrospectionQueryを使用して、スキーマのjsonまたはgraphql言語表現を取得できます。

// export.js 
import { schema } from './schema.js' 
import { graphql, introspectionQuery, printSchema } from 'graphql'; 

// Save json schema 
graphql(schema, introspectionQuery).then(result => { 
    fs.writeFileSync(
    `${yourSchemaPath}.json`, 
    JSON.stringify(result, null, 2) 
); 
}); 

// Save user readable type system shorthand of schema 
fs.writeFileSync(
    `${yourSchemaPath}.graphql`, 
    printSchema(schema) 
); 

答えて

1

graphql-to-jsonがあります。私は、それを行うためのCLIツールがあると信じています。

また、独自のスクリプトを作成し、nodeで実行することもできます。クエリを実行するためにサーバーをスピンアップする必要はなく、スキーマが必要で、クエリを直接実行することもできます。あなたはhereの例を見ることができます。

+0

'makeExecutableSchema'はGraphQLSchemaを返します。 [relays docs](https://facebook.github.io/relay/docs/guides-babel-plugin.html#schema-json)の例を参考にしてください。 – everett1992

関連する問題