一、多个.graphqls文件探索
1.1 说明
我们项目如果有Book和Author的查询的话,那么我们应该不会把配置都放到一个root.graphqls和schema.graphqls文件中吧,这样不利于团队配合开发,那么是否可以book放到book.graphqls中,author放到author.graphqls中呐?
1.2 建立多个.grphqls文件
建立/resources/graphql/author.graphqls,内容如下:
-
type Author {
-
id: Int!
-
name: String
-
photo: String
-
}
-
extend type Query{
-
findAuthorById(id: Int!):Author
-
}
说明:这里使用extend继承Root Query,如果不使用extend的话,多个文件的话就不能都生效了。另外对于类型的声明也移到了一起统一管理。
建立/resources/graphql/book.graphqls,内容如下:
-
type Book {
-
id: Int!
-
name: String
-
isbn: String
-
}
-
extend type Query{
-
findBookById(id: Int!):Book
-
}
对于root.graphqls和schema.graphqls文件内容为空或者删除掉文件。
1.3 运行测试
我们这时候启动运行测试,会报一个异常信息:
SchemaClassScannerError: Type definition for root query type 'Query' not found!
意思我们没有定义Root Query,解决的方式很简单在root.graphqls文件中天极爱一个空的Query即可:
-
#定义查询的方法
-
type Query {}
这时候在运行应该是可以正常运行的,在右侧Query进入可以看到两个Query:
-
findAuthorById(id: Int!): Author
-
findBookById(id: Int!): Book
。。。。。。。。。。。。。。。。。
版权原因,完整文章,请参考如下:GraphQL的探索之路 – SpringBoot集成GraphQL之Query篇三 - 第316篇