查询

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

类型定义

此页面上的查询假定以下类型定义

type Post @node {
    id: ID! @id
    content: String!
    creator: User! @relationship(type: "HAS_POST", direction: IN, properties: "PostedAt")
    createdAt: DateTime!
}

type User @node {
    id: ID! @id
    name: String!
    age: Int!
    posts: [Post!]! @relationship(type: "HAS_POST", direction: OUT, properties: "PostedAt")
    friends: [User!]! @relationship(type: "FRIENDS_WITH", direction: OUT)
}

type PostedAt @relationshipProperties {
    date: DateTime
}

为其生成以下查询字段

type Query {
    posts(where: PostWhere, sort: [PostSort!]!, limit: Int, offset: Int,): [Post!]!
    postsAggregate(where: PostWhere): PostAggregationSelection!

    users(where: UserWhere, sort: [UserSort!]!, limit: Int, offset: Int,): [User!]!
    usersAggregate(where: UserWhere): UserAggregationSelection!
}

编写查询

根据类型定义,以下是编写用于读取或检索值的查询的两个示例

从其 ID 和姓名返回所有 User 节点
query {
    users {
        id
        name
    }
}
查询名为“Jane Smith”的用户及其帖子
query {
    users(where: { name_EQ: "Jane Smith" }) {
        id
        name
        posts {
            content
        }
    }
}