![](/img/trans.png)
[英]Find most suitable nodes from a starting node, start and end nodes are indirectly connected and edges are weighted
[英]How are nodes and edges connected in GraphQL?
我正在使用https://arrows.app/创建一个基本网络:
它创建的结果 GraphQL 是:
type Person {
Name: Dante Alighieri
wroteBook: Book @relationship(type: "WROTE", direction: OUT)
}
type Book {
Title: The Inferno
personWrote: Person @relationship(type: "WROTE", direction: IN)
personReviewed: Person @relationship(type: "REVIEWED", direction: IN)
personRead: Person @relationship(type: "READ", direction: IN)
}
type Person {
Name: Harold Bloom
reviewedBook: Book @relationship(type: "REVIEWED", direction: OUT)
reviewedBook: Book @relationship(type: "REVIEWED", direction: OUT)
}
...
我们如何在 GraphQL 中知道这两个关系是“链接”的?
type Person {
Name: Dante Alighieri
wroteBook: Book @relationship(type: "WROTE", direction: OUT)
}
type Book {
Title: The Inferno
personWrote: Person @relationship(type: "WROTE", direction: IN)
}
GraphQL 中的顺序排序是否暗示存在关系,因为在“Person[@Name=Dante Alighieri].wroteBook”条目中没有 ID 或提及“地狱”。 例如,我们怎么知道它没有引用后面的条目,例如:
type Book {
Title: The Tempest
personWrote: Person @relationship(type: "WROTE", direction: IN)
}
您可以做的一件事是通过以下方式定义您的架构:
type Book{
id: ID!
title: String!
personWrote: Person!
}
GraphQL 知道如何 map 这两个方法是通过在解析器中为 Book 类型创建解析器:
export default {
Query: {
...
},
Mutation: {
...
},
Book: {
personWrote: async (parent, __, context) => {
const bookId = parent.id
//fetch the author who wrote the book from your database
const bookAuthor = Authors.findBookById(id)
return bookAuthor
}
}
}
这样,GraphQL 现在期待一个作者类型,并且可以进一步访问其所有字段。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.