2017-07-31 106 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形式要麼typeDefsschema的格局?


我需要一個JSON模式使用relay-compilerapollo-codegenapollo-codegen包括這個腳本從graphql服務器創建一個模式...

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

...但我想創建模式和運行阿波羅代碼生成,在一個自動構建。我不想創建一個服務器。


我想提出這樣一個答案,但問題已被標記題外話¯\ _(ツ)_ /¯

從@丹尼爾 - 里爾登答案指出我在正確的方向。 makeExecutableSchema返回GraphQLSchema,因此可以使用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

There's graphql-to-json。我相信有一個CLI工具可以做到這一點。

或者,您可以編寫自己的腳本,並使用node執行它。您不必旋轉服務器來運行查詢,只需要一個模式,並且您可以直接針對它運行查詢。你可以查看一個例子here

+0

好吧,它看起來像'makeExecutableSchema'返回一個GraphQLSchema。你的例子來自[relaydocs](https://facebook.github.io/relay/docs/guides-babel-plugin.html#schema-json)例子可能會起作用。 – everett1992

相關問題