后端开发经常会面临的一个问题是数据存储,和数据管理。不论你是用的mysql,oracle,cassandra,postgres,mongo等等,要查询这些数据存储介质必须为每一个数据源开发或者集成相对应的中间件。而随着我们的项目增多,或者存储介质的迁移变动时,我们不得不面临代码重构,甚至重做的问题,这其间会产生大量的时间成本和人力成本。GraphQL正是在这种背景下诞生的,它定义了一套schema查询语法,可以很直观的展现数据层级,有着强类型校验,也有着弱类型扩展。它本身只是定义客户端与数据源之间存取数据的协议,通过协议转换最终访问数据存储介质。
使用GraphQL来处理数据查询能给我们的项目管理和迭代功能上带来诸多便利,但真正使用起来确实存在一些痛点,主要有以下几个方面:
通过springboot集成apollo,利用通知回调机制动态加载更新配置,无需重启在线服务。具体集成方案,参考如下链接
Apollo: link.
protocols:
mutation:
createIndex:
params:
- param: fields
type: TABLEDEFINITION
required: true
- param: options
type: JSON
required: true
ret: mutationRet
insert:
params:
- param: fields
type: TABLEDEFINITION
required: true
ret: mutationRet
upsert:
params:
- param: filter
type: JSON
required: true
- param: fields
type: JSON
required: true
ret: mutationRet
update:
params:
- param: filter
type: JSON
required: true
- param: fields
type: JSON
required: true
ret: mutationRet
delete:
params:
- param: filter
type: JSON
required: true
ret: mutationRet
query:
pageFind:
params:
- param: filter
type: JSON
required: true
- param: sort
type: JSON
required: false
- param: batch
type: Integer
required: false
- param: limit
type: Integer
required: false
- param: skip
type: Integer
required: false
- param: scrollId
type: String
required: false
- param: reverseElect
type: Integer
required: false
ret: TABLEDEFINITION_scroll
findOne:
params:
- param: filter
type: JSON
required: true
- param: sort
type: JSON
required: false
- param: reverseElect
type: Integer
required: false
ret: TABLEDEFINITION
aggregate:
params:
- param: pipeline
type: JSONArray
required: true
ret: aggregation
Common:
normal: |
scalar Object
type aggregation {
result: Object
}
type mutationRet {
ok: Boolean
}
scroll: |
type TABLEDEFINITION_scroll {
scrollId: String
data: [TABLEDEFINITION]
}
alias:
mongo-test:
test: MongoTest
mysql-test:
test: MysqlTest
connectors:
redis:
host: localhost
port: 6379
password:
dbnum: 10
enableSchema: false
enableCache: true
mysql-auth:
url: "jdbc:mysql://localhost/datagateway_sandbox"
username: test
password: test
maxConnection: 60
enableSchema: false
mysql-test:
url: "jdbc:mysql://localhost/test"
username: test
password: test
maxConnection: 60
enableSchema: true
mongo-test:
connection: mongodb://localhost:3717
poolSize: "10"
enableSchema: true
test: //数据库名称
testfordata: | //表名
type TABLEDEFINITION {
id: Int
name: String
key: String
}
input TABLEDEFINITION_input {
id: Int
name: String
key: String
}