GraphQL 中的节点和边是如何连接的?

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 中知道这两个关系是 'linked'?

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 或提及 'The Inferno'。例如,我们怎么知道它没有引用后面的条目,例如:

type Book {
  Title: The Tempest
  personWrote: Person @relationship(type: "WROTE", direction: IN)
}

您可以做的一件事是按以下方式定义架构:

type Book{
 id: ID!
 title: String!
 personWrote: Person!
}

GraphQL 知道如何映射这两者的方式是在解析器中为 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 现在需要一个 Author 类型,并且可以进一步访问它的所有字段。