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/

posted on   荣锋亮  阅读(577)  评论(0编辑  收藏  举报

编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)
历史上的今天:
2018-05-20 Stream Processing 101: From SQL to Streaming SQL in 10 Minutes
2018-05-20 13 Stream Processing Patterns for building Streaming and Realtime Applications
2018-05-20 Siddhi cep java 集成简单使用
2014-05-20 About Apache Cordova
2014-05-20 ASP.NET Identity 2新增双重认证、帐号锁定、防伪印章功能并修复了一些bug
2014-05-20 ASP.NET vNext:微软下一代云环境Web开发框架
2014-05-20 .net 拆分字符串成数数组 包含使用空格 逗号 回车 换行符等

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示