如何使用Neo4j GraphQL Library(三)

使用本地環(huán)境將上一節(jié)內容走一遍,做一次小總結

如何使用Neo4j GraphQL Library(二)
http://www.itdecent.cn/p/4027668df850

1. 啟動本地Neo4j Server

C:\Users\bing.yao>neo4j console

打開谷歌瀏覽器,本地訪問Neo4j Browers
http://localhost:7474/browser/

2. 啟動本地Apollo Server

參見:
如何使用Neo4j GraphQL Library(一)
http://www.itdecent.cn/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示例數(shù)據(jù)

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. 查詢數(shù)據(jù)

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"
          }
        ]
      }
    ]
  }
}
zj01.JPG

在本地Neo4j Browers中輸入Cypher查詢書籍、作者、主題信息

match (a:Book),(b:Author),(c:Subject) return a,b,c
zj02.JPG

5. 對象或節(jié)點之間有關系就能查詢嗎?

下面代碼是通過訂單信息Order查詢書籍Book信息,可以執(zhí)行

{
  orders{
    orderID
    books {
      title
    }    
  }
}

下面代碼是通過書籍Book信息查詢訂單信息Order,不能執(zhí)行,錯誤提示:
GraphQLError: Cannot query field "orders" on type "Book".

{
  books{
    title
    orders {
      orderID
    }    
  }
}

原因是在Type Book中沒有定義與Type Order的關系。
如果在Type Book中添加了與Order 的關系,則可以執(zhí)行該代碼:

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從一個節(jié)點(或稱之為對象、Tpye)出發(fā),是否能夠查詢出另一個節(jié)點的信息,需要看在該節(jié)點的Type中是否定義了指向另一個節(jié)點的關系@relationship。

6. 回顧用到的技術

1)Neo4j Graph Database Server,圖數(shù)據(jù)庫服務,用來存儲圖數(shù)據(jù),可以通過Neo4j Browers執(zhí)行Cypher代碼來操作圖數(shù)據(jù)。

2)Node.js,管理js庫,提供js服務器端運行(即可以基于js提供web服務,js不只是寫前端)

3)GraphQL,F(xiàn)B開源的一種用于API的查詢語言,也是一種規(guī)范,同時也是一個運行環(huán)境

4)Neo4j GraphQL Library,js庫,使得連接GraphQL和Neo4j Server更容易,更高效

5)Apollo Server,GraphQL后端服務集成,提供了Playground,通過瀏覽器輸入GraphQL操作圖數(shù)據(jù)

當我們需要基于圖數(shù)據(jù)庫開發(fā)Web應用,滿足圖數(shù)據(jù)的查詢/修改等操作,以及數(shù)據(jù)展示。
整個項目可以分成三部分:
1)圖數(shù)據(jù)
2)Web服務
3)前端數(shù)據(jù)展示和用戶交互

圖數(shù)據(jù)庫,可以是Neo4j,也可以是其他的圖數(shù)據(jù)庫
Web服務,可以有更多的選擇:Node.js+GraphQL+Apollo;Python+Flask;Java也可以
前端,jquery, bootstrap, d3.js,重點是能夠以圖的形式展示數(shù)據(jù)和交互

?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容