关系

这是 GraphQL 库版本 6 的文档。对于长期支持 (LTS) 版本 5,请参阅 GraphQL 库版本 5 LTS

如果没有关系,您的类型定义更像是断开连接的节点集合,价值不大。在您的数据模型中添加关系为您的数据提供了运行跨越图中广泛部分的复杂查询所需的上下文。

此页面描述了如何为简单的连接模型编写类型定义,通过模式插入数据,然后查询它。

类型定义

以以下图为例,其中 Person 类型具有两种不同的关系类型,可以将其连接到 Movie 类型。

relationships
图 1. 示例图

要使用 Neo4j GraphQL 库创建该图,首先需要定义节点并在此模型中定义两种不同的类型

type Person @node {
    name: String!
    born: Int!
}

type Movie @node {
    title: String!
    released: Int!
}

然后,您可以使用 @relationship 指令将这两种类型连接在一起

type Person @node {
    name: String!
    born: Int!
    actedInMovies: [Movie!]! @relationship(type: "ACTED_IN", direction: OUT)
    directedMovies: [Movie!]! @relationship(type: "DIRECTED", direction: OUT)
}

type Movie @node {
    title: String!
    released: Int!
    actors: [Person!]! @relationship(type: "ACTED_IN", direction: IN)
    director: Person! @relationship(type: "DIRECTED", direction: IN)
}

请注意,在此查询中

  • 一个 Person 可以出演导演多部电影,一部 Movie 可以有多个演员。但是,一部 Movie 很少有多个导演,因此您可以在类型定义中建模此基数,以确保数据的准确性。

  • 如果没有导演,Movie 实际上并不是一部 Movie,并且已通过将 director 字段标记为非空来表示。这意味着 Movie**必须**有一个指向它的 DIRECTED 关系才能有效。

  • 要确定 @relationship 指令的 direction 参数应该是 IN 还是 OUT,请像上图一样可视化您的关系,然后建模箭头的方向。

  • @relationship 指令是对 Neo4j 关系的引用,而在模式中,使用短语 edge(s) 与 Relay 使用的一般 API 语言保持一致。

关系属性

您可以通过两个步骤将关系属性添加到示例中

  1. 添加一个使用 @relationshipProperties 指令装饰的类型定义,其中包含所需的关系属性。

  2. @relationship 指令的“两侧”(或仅一侧,如果您愿意)添加一个 properties 参数,该参数指向新定义的接口。

例如,假设您想区分演员在电影中扮演的角色

type Person @node {
    name: String!
    born: Int!
    actedInMovies: [Movie!]! @relationship(type: "ACTED_IN", properties: "ActedIn", direction: OUT)
    directedMovies: [Movie!]! @relationship(type: "DIRECTED", direction: OUT)
}

type Movie @node {
    title: String!
    released: Int!
    actors: [Person!]! @relationship(type: "ACTED_IN", properties: "ActedIn", direction: IN)
    director: Person! @relationship(type: "DIRECTED", direction: IN)
}

type ActedIn @relationshipProperties {
    roles: [String!]
}

@declareRelationship

如果您需要在接口上使用关系,则需要使用新的 @declareRelationship 指令,并在具体类型中定义关系

interface Production {
    title: String!
    actors: [Actor!]! @declareRelationship
}

type Actor @node {
    name: String!
    born: Int!
    actedInMovies: [Movie!]! @relationship(type: "ACTED_IN", properties: "ActedIn", direction: OUT)
}

type Movie implements Production @node {
    title: String!
    released: Int!
    actors: [Actor!]! @relationship(type: "ACTED_IN", properties: "ActedIn", direction: IN)
}

type Series implements Production @node {
    title: String!
    released: Int!
    episodes: Int!
    actors: [Actor!]! @relationship(type: "ACTED_IN", properties: "ActedIn", direction: IN)
}

type ActedIn @relationshipProperties {
    roles: [String!]
}

queryDirection

所有关系都有方向。但是,在查询它们时,可以执行 无向查询。要设置查询关系时的默认行为,可以使用参数 queryDirection

type Person @node {
    name: String!
    born: Int!
    actedInMovies: [Movie!]! @relationship(type: "ACTED_IN", direction: OUT, queryDirection: DEFAULT_DIRECTED)
}

queryDirection 可以具有以下值

  • DIRECTED:只能在此关系上执行有向查询。

  • UNDIRECTED:只能在此关系上执行无向查询。

插入数据

嵌套变异意味着可以通过 GraphQL 模式以多种方式将数据插入数据库。考虑之前提到的规则,即在添加导演之前无法创建 Movie 节点。但是,您可以先创建一个导演节点,然后创建并将其连接到 Movie。另一种选择是在同一个变异中创建 MovieDirector,例如

mutation CreateMovieAndDirector {
    createMovies(input: [
        {
            title: "Forrest Gump"
            released: 1994
            director: {
                create: {
                    node: {
                        name: "Robert Zemeckis"
                        born: 1951
                    }
                }
            }
        }
    ]) {
        movies {
            title
            released
            director {
                name
                born
            }
        }
    }
}

然后,您需要在此示例中创建演员,并将其连接到新的 Movie 节点,并指定他们扮演的角色

mutation CreateActor {
    createPeople(input: [
        {
            name: "Tom Hanks"
            born: 1956
            actedInMovies: {
                connect: {
                    where: {
                        node: { title_EQ: "Forrest Gump" }
                    }
                    edge: {
                        roles: ["Forrest"]
                    }
                }
            }
        }
    ]) {
        movies {
            title
            released
            director {
                name
                born
            }
            actorsConnection {
                edges {
                    roles
                    node {
                        name
                        born
                    }
                }
            }
        }
    }
}

请注意 actorsConnection 字段的选择,以便查询 roles 关系属性。

还要注意,在第二个变异中,返回了整个图。这没有必要,因为您可以将这些变异压缩成一个插入所有所需数据的操作,例如

mutation CreateMovieDirectorAndActor {
    createMovies(input: [
        {
            title: "Forrest Gump"
            released: 1994
            director: {
                create: {
                    node: {
                        name: "Robert Zemeckis"
                        born: 1951
                    }
                }
            }
            actors: {
                create: [
                    {
                        node: {
                            name: "Tom Hanks"
                            born: 1956
                        }
                        edge: {
                            roles: ["Forrest"]
                        }
                    }
                ]
            }
        }
    ]) {
        movies {
            title
            released
            director {
                name
                born
            }
            actorsConnection {
                edges {
                    roles
                    node {
                        name
                        born
                    }
                }
            }
        }
    }
}

认识到这一点可以帮助您一次在一个变异中创建更大的子图,从而提高效率。

获取您的数据

现在您已将 Movie 信息存储在数据库中,您可以如下查询所有内容

query {
    movies(where: { title_EQ: "Forrest Gump" }) {
        title
        released
        director {
            name
            born
        }
        actorsConnection {
            edges {
                roles
                node {
                    name
                    born
                }
            }
        }
    }
}

基数

Neo4j GraphQL 库对“多”关系有类型定义要求。例如

type User @node {
    name: String!
    posts: [Post!]! @relationship(type: "HAS_POST", direction: OUT)
}

type Post @node {
    name: String!
}

User.posts 处的关系被视为“多”关系,这意味着它应始终为 NonNullListTypeNonNullNamedType 类型。换句话说,“多”关系中的数组和其中的类型都应该有一个 !