接口类型

本页介绍如何在关系字段上使用和定义接口。

创建接口字段

以下模式定义了一个Actor类型,它具有一个类型为[Production!]!的关系ACTED_INProduction是一个接口类型,具有MovieSeries实现。

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

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

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

type ActedIn @relationshipProperties {
    role: String!
}

type Actor {
    name: String!
    actedIn: [Production!]! @relationship(type: "ACTED_IN", direction: OUT, properties: "ActedIn")
}

这些类型定义将用于本章中的其他示例。

查询接口

以下将返回每个演员的标题以“The”开头的所有制作

query GetProductionsStartingWithThe {
    actors {
        name
        actedIn(where: { node: { title_STARTS_WITH: "The " } }) {
            title
            ... on Movie {
                runtime
            }
            ... on Series {
                episodes
            }
        }
    }
}

以下查询将仅返回标题以“The”开头的电影,方法是通过typename_IN筛选它们

query GetMoviesStartingWithThe {
    actors {
        name
        actedIn(where: { node: { title_STARTS_WITH: "The ", typename_IN: [Movie]  } }) {
            title
            ... on Movie {
                runtime
            }
        }
    }
}

使用接口字段进行创建

以下变异创建一个演员以及他们出演的一些制作

mutation CreateActorAndProductions {
    createActors(
        input: [
            {
                name: "Chris Pratt"
                actedIn: {
                    create: [
                        {
                            edge: {
                                role: "Mario"
                            }
                            node: {
                                Movie: {
                                    title: "Super Mario Bros"
                                    runtime: 90
                                }
                            }
                        }
                        {
                            edge: {
                                role: "Starlord"
                            }
                            node: {
                                Movie: {
                                    title: "Guardians of the Galaxy"
                                    runtime: 122
                                }
                            }
                        }
                        {
                            edge: {
                                role: "Andy"
                            }
                            node: {
                                Series: {
                                    title: "Parks and Recreation"
                                    episodes: 126
                                }
                            }
                        }
                    ]
                }
            }
        ]
    ) {
        actors {
            name
            actedIn {
                title
            }
        }
    }
}

嵌套接口操作

对接口的操作是抽象的,直到您指示它们不要抽象。以下示例说明了这一点

mutation CreateActorAndProductions {
    updateActors(
        where: { name: "Woody Harrelson" }
        connect: {
            actedIn: {
                where: { node: { title: "Zombieland" } }
                connect: { actors: { where: { node: { name: "Emma Stone" } } } }
            }
        }
    ) {
        actors {
            name
            actedIn {
                title
            }
        }
    }
}

上述变异

  1. 查找任何名称为“Woody Harrelson”的Actor节点。

  2. 将“Woody Harrelson”节点连接到标题为“Zombieland”的Production节点。

  3. 将已连接的Production节点连接到任何名称为“Emma Stone”的Actor节点。

查询接口

为了设置查询返回哪些实现,需要应用一个过滤器where。例如,以下查询返回每个演员的标题以“The”开头的所有制作(moviesseries

query GetProductionsStartingWithThe {
    actors {
        name
        actedIn(where: { node: { title_STARTS_WITH: "The " } }) {
            title
            ... on Movie {
                runtime
            }
            ... on Series {
                episodes
            }
        }
    }
}