接口类型

这是 GraphQL Library 版本 7 的文档。对于长期支持 (LTS) 版本 5,请参考 GraphQL Library 版本 5 LTS

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

创建接口字段

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

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

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

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

type ActedIn @relationshipProperties {
    role: String
}

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

本章其余示例将使用这些类型定义。

查询接口

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

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

以下查询将通过按 typename 过滤,仅返回每个演员标题以“The ”开头的电影

query GetMoviesStartingWithThe {
    actors {
        name
        actedIn(where: { node: { title: { startsWith: "The " }, typename: [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: { eq: "Woody Harrelson" } }
    update: {
      actedIn: {
        connect: {
          where: { node: { title: { eq: "Zombieland" } } }
          connect: {
            actors: { where: { node: { name: { eq: "Emma Stone" } } } }
          }
        }
      }
    }
  ) {
    actors {
      name
      actedIn {
        title
      }
    }
  }
}

上述修改操作

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

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

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

查询接口

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

query GetProductionsStartingWithThe {
  actors {
    name
    actedIn(where: { title: { startsWith: "The " } }) {
      title
      ... on Movie {
        runtime
      }
      ... on Series {
        episodes
      }
    }
  }
}
© . All rights reserved.