使用本地环境将上一节内容走一遍,做一次小总结
如何使用Neo4j GraphQL Library(二)
https://www.jianshu.com/p/4027668df850
1. 启动本地Neo4j Server
C:\Users\bing.yao>neo4j console
打开谷歌浏览器,本地访问Neo4j Browers
http://localhost:7474/browser/
2. 启动本地Apollo Server
参见:
如何使用Neo4j GraphQL Library(一)
https://www.jianshu.com/p/cabc3e54d808
先将Type(Book、Author等等)添加到index.js文件中;
const typeDefs = `
type Order {
orderID: ID! @id
placedAt: DateTime @timestamp
shippingCost: Float
shipTo: Address @relationship(type: "SHIPS_TO", direction: OUT)
customer: Customer @relationship(type: "PLACED", direction: IN)
books: [Book] @relationship(type: "CONTAINS", direction: OUT)
}
type Customer {
username: String
orders: [Order] @relationship(type: "PLACED", direction: OUT)
reviews: [Review] @relationship(type: "WROTE", direction: OUT)
}
type Address {
address: String
location: Point
order: Order @relationship(type: "SHIPS_TO", direction: IN)
}
type Book {
isbn: ID!
title: String
price: Float
description: String
reviews: [Review] @relationship(type: "REVIEWS", direction: IN)
authors: [Author] @relationship(type: "AUTHOR_OF", direction: IN)
subjects: [Subject] @relationship(type: "ABOUT", direction: OUT)
}
type Review {
rating: Int
text: String
createdAt: DateTime @timestamp
book: Book @relationship(type: "REVIEWS", direction: OUT)
author: Customer @relationship(type: "WROTE", direction: IN)
}
type Author {
name: String
book: [Book] @relationship(type: "AUTHOR_OF", direction: OUT)
}
type Subject {
name: String
book: [Book] @relationship(type: "ABOUT", direction: IN)
}
`;
然后启动Apollo。
C:\Users\bing.yao\course01>node index.js
打开谷歌浏览器,本地访问
http://localhost:4000/
3. 导入bookstore示例数据
playground
mutation {
createBooks(
input: [
{
isbn: "1492047686"
title: "Graph Algorithms"
price: 37.48
description: "Practical Examples in Apache Spark and Neo4j"
}
{
isbn: "1119387507"
title: "Inspired"
price: 21.38
description: "How to Create Tech Products Customers Love"
}
{
isbn: "190962151X"
title: "Ross Poldark"
price: 15.52
description: "Ross Poldark is the first novel in Winston Graham's sweeping saga of Cornish life in the eighteenth century."
}
]
) {
books {
title
}
}
createCustomers(
input: [
{
username: "EmilEifrem7474"
reviews: {
create: {
rating: 5
text: "Best overview of graph data science!"
book: { connect: { where: { isbn: "1492047686" } } }
}
}
orders: {
create: {
books: { connect: { where: { title: "Graph Algorithms" } } }
shipTo: {
create: {
address: "111 E 5th Ave, San Mateo, CA 94401"
location: {
latitude: 37.5635980790
longitude: -122.322243272725
}
}
}
}
}
}
{
username: "BookLover123"
reviews: {
create: [
{
rating: 4
text: "Beautiful depiction of Cornwall."
book: { connect: { where: { isbn: "190962151X" } } }
}
]
}
orders: {
create: {
books: {
connect: [
{ where: { title: "Ross Poldark" } }
{ where: { isbn: "1119387507" } }
{ where: { isbn: "1492047686" } }
]
}
shipTo: {
create: {
address: "Nordenskiöldsgatan 24, 211 19 Malmö, Sweden"
location: { latitude: 55.6122270502, longitude: 12.99481772774 }
}
}
}
}
}
]
) {
customers {
username
}
}
}
mutation {
createAuthors(
input: [
{
name: "Marty Cagan"
book: { connect: { where: { title: "Inspired" } } }
}
{
name: "Winston Graham"
book: { connect: { where: { title: "Ross Poldark" } } }
}
{
name: "Mark Needham"
book: { connect: { where: { title: "Graph Algorithms" } } }
}
{
name: "Amy E. Hodler"
book: { connect: { where: { title: "Graph Algorithms" } } }
}
]
) {
authors {
name
book {
title
}
}
}
}
mutation {
createSubjects(
input: [
{
name: "Product management"
book: { connect: { where: { title: "Inspired" } } }
}
{
name: "Design"
book: { connect: { where: { title: "Inspired" } } }
}
{
name: "Historical fiction"
book: { connect: { where: { title: "Ross Poldark" } } }
}
{
name: "Cornwall"
book: { connect: { where: { title: "Ross Poldark" } } }
}
{
name: "Graph theory"
book: { connect: { where: { title: "Graph Algorithms" } } }
}
{
name: "Neo4j"
book: { connect: { where: { title: "Graph Algorithms" } } }
}
]
) {
subjects {
name
book {
title
}
}
}
}
4. 查询数据
Graph Query 代码
{
books{
title
price
authors {
name
}
reviews {
rating
}
subjects {
name
}
}
}
返回信息
{
"data": {
"books": [
{
"title": "Graph Algorithms",
"price": 37.48,
"authors": [
{
"name": "Mark Needham"
},
{
"name": "Amy E. Hodler"
}
],
"reviews": [
{
"rating": 5
}
],
"subjects": [
{
"name": "Neo4j"
},
{
"name": "Graph theory"
}
]
},
{
"title": "Inspired",
"price": 21.38,
"authors": [
{
"name": "Marty Cagan"
}
],
"reviews": [],
"subjects": [
{
"name": "Design"
},
{
"name": "Product management"
}
]
},
{
"title": "Ross Poldark",
"price": 15.52,
"authors": [
{
"name": "Winston Graham"
}
],
"reviews": [
{
"rating": 4
}
],
"subjects": [
{
"name": "Cornwall"
},
{
"name": "Historical fiction"
}
]
}
]
}
}
在本地Neo4j Browers中输入Cypher查询书籍、作者、主题信息
match (a:Book),(b:Author),(c:Subject) return a,b,c
5. 对象或节点之间有关系就能查询吗?
下面代码是通过订单信息Order查询书籍Book信息,可以执行
{
orders{
orderID
books {
title
}
}
}
下面代码是通过书籍Book信息查询订单信息Order,不能执行,错误提示:
GraphQLError: Cannot query field "orders" on type "Book".
{
books{
title
orders {
orderID
}
}
}
原因是在Type Book中没有定义与Type Order的关系。
如果在Type Book中添加了与Order 的关系,则可以执行该代码:
orders: [Order] @relationship(type: "CONTAINS", direction: IN)
输出结果如下:
{
"data": {
"books": [
{
"title": "Graph Algorithms",
"orders": [
{
"orderID": "da0ee3ee-2c21-4acd-9191-f161917fb512"
},
{
"orderID": "b64b69c1-8373-4c35-a20c-f0b01fd7cd02"
}
]
},
{
"title": "Inspired",
"orders": [
{
"orderID": "b64b69c1-8373-4c35-a20c-f0b01fd7cd02"
}
]
},
{
"title": "Ross Poldark",
"orders": [
{
"orderID": "b64b69c1-8373-4c35-a20c-f0b01fd7cd02"
}
]
}
]
}
}
基于GraphQL从一个节点(或称之为对象、Tpye)出发,是否能够查询出另一个节点的信息,需要看在该节点的Type中是否定义了指向另一个节点的关系@relationship。
6. 回顾用到的技术
1)Neo4j Graph Database Server,图数据库服务,用来存储图数据,可以通过Neo4j Browers执行Cypher代码来操作图数据。
2)Node.js,管理js库,提供js服务器端运行(即可以基于js提供web服务,js不只是写前端)
3)GraphQL,FB开源的一种用于API的查询语言,也是一种规范,同时也是一个运行环境
4)Neo4j GraphQL Library,js库,使得连接GraphQL和Neo4j Server更容易,更高效
5)Apollo Server,GraphQL后端服务集成,提供了Playground,通过浏览器输入GraphQL操作图数据
当我们需要基于图数据库开发Web应用,满足图数据的查询/修改等操作,以及数据展示。
整个项目可以分成三部分:
1)图数据
2)Web服务
3)前端数据展示和用户交互
图数据库,可以是Neo4j,也可以是其他的图数据库
Web服务,可以有更多的选择:Node.js+GraphQL+Apollo;Python+Flask;Java也可以
前端,jquery, bootstrap, d3.js,重点是能够以图的形式展示数据和交互