dgraph native graphal 试用
dgraph 已经直接支持native graphlq 的应用开发,以下是一个简单的学习
环境准备
- docker-compose 文件
version: "3"
services:
graphql:
image: dgraph/standalone
ports:
- "8000:8000"
- "8080:8080"
- "9080:9080"
- 启动服务
docker-compose up -d
graphql 定义
schema.graphql 文件
type Product {
productID: ID!
name: String @search(by: [term])
reviews: [Review] @hasInverse(field: about)
}
type Customer {
username: String! @id @search(by: [hash, regexp])
reviews: [Review] @hasInverse(field: by)
}
type Review {
id: ID!
about: Product!
by: Customer!
comment: String @search(by: [fulltext])
rating: Int @search
}
upload schema
curl -X POST localhost:8080/admin/schema -d '@schema.graphql'
查看定义
可以直接试用graphql playground
数据操作
- mutation
mutation {
addProduct(input: [
{ name: "GraphQL on Dgraph"},
{ name: "Dgraph: The GraphQL Database"}
]) {
product {
productID
name
}
}
addCustomer(input: [{ username: "Michael"}]) {
customer {
username
}
}
}
// 后边执行
mutation {
addReview(input: [{
by: {username: "Michael"},
about: { productID: "0x3"},
comment: "Fantastic, easy to install, worked great. Best GraphQL server available",
rating: 10}])
{
review {
comment
rating
by { username }
about { name }
}
}
}
效果
- query
query {
queryProduct {
productID
name
reviews {
id
rating
comment
}
}
}
效果
说明
dgraph 的native graphql 能力还是很不错的,同时也支持对于现有的数据进行映射(类似orm),但是目前对于数据schema 的定义可能会
影响dgraph 的schema,所以还是需要注对于已有数据的处理,后期试用下集群模式的
参考资料
https://graphql.dgraph.io/docs/quick-start/
https://graphql.dgraph.io/docs/dgraph/