我更新了graphql-js依赖项已经有一年了。我现在看到有一个简化模式生成的实用程序:buildSchema
。此函数以GraphQL的形式将整个模式(作为字符串)作为arg。那真棒,但有没有办法模块化这个?我的架构不是很小,并且很难塞进一个.graphql
文件。是否有某种实用程序或模式用于将每个类型定义存储在自己的文件中,例如?
答案 0 :(得分:5)
如果你有graphql-tools
个软件包,可以使用makeExecutableSchema
模块化你的架构,如下所示:
const schema = makeExecutableSchema({
typeDefs: [schema1, schema2, schema3, ...],
resolvers: resolvers,
});
这样每种类型都可以在自己的文件中定义。
答案 1 :(得分:0)
您可以使用merge-graphql-schemas
包进一步改善架构模块性。
这是一个模块化的graphql服务器种子 - graphql-server-seed
项目结构允许您将类型和解析器分隔为多个文件。希望它有所帮助!
答案 2 :(得分:0)
如果你正在使用Apollo的graphql-tools,那么我发现构建你的架构的最佳方法是使用schemaglue.js(免责声明:我' ve建立了这个):
08-10 04:55:00.681 791-791/com.example.asusnb.myapplication D/DatabaseHelper: updateName: query: UPDATE people_table SET oran = '45' AND boya = '#139F98' WHERE ID = '13' AND oran = '50 AND boya = '#27fbf0'
08-10 04:55:00.681 791-791/com.example.asusnb.myapplication D/DatabaseHelper: updateName: Setting name to 45
08-10 04:55:00.681 791-791/com.example.asusnb.myapplication E/SQLiteLog: (1) near "#27fbf0": syntax error
08-10 04:55:00.681 791-791/com.example.asusnb.myapplication D/AndroidRuntime: Shutting down VM
08-10 04:55:00.691 791-791/com.example.asusnb.myapplication E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.asusnb.myapplication, PID: 791
android.database.sqlite.SQLiteException: near "#27fbf0": syntax error (code 1): , while compiling: UPDATE people_table SET oran = '45' AND boya = '#139F98' WHERE ID = '13' AND oran = '50 AND boya = '#27fbf0'
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:1093)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:670)
at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:59)
at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1795)
at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1726)
at com.example.asusnb.myapplication.DatabaseHelper.updateName(DatabaseHelper.java:106)
at com.example.asusnb.myapplication.AnaMenu$2.onClick(AnaMenu.java:299)
at android.support.v7.app.AlertController$ButtonHandler.handleMessage(AlertController.java:161)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5832)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
然后,您将能够使用类似的内容构建您的架构和解析器:
const { makeExecutableSchema } = require('graphql-tools')
const { glue } = require('schemaglue')
const { schema, resolver } = glue('src/graphql')
const executableSchema = makeExecutableSchema({
typeDefs: schema,
resolvers: resolver
})
我已经写了一篇关于这个here的详细文章。