查询
类型定义
此页面上的查询假定以下类型定义
type Post {
id: ID! @id
content: String!
creator: User! @relationship(type: "HAS_POST", direction: IN, properties: "PostedAt")
createdAt: DateTime!
}
type User {
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, options: PostOptions): [Post!]!
postsAggregate(where: PostWhere): PostAggregationSelection!
users(where: UserWhere, options: UserOptions): [User!]!
usersAggregate(where: UserWhere): UserAggregationSelection!
}
编写查询
基于类型定义,以下是如何编写用于读取或检索值的查询的两个示例
返回所有 User 节点及其 ID 和名称
query {
users {
id
name
}
}
查询名为“Jane Smith”的用户及其帖子
query {
users(where: { name: "Jane Smith" }) {
id
name
posts {
content
}
}
}
无向查询
所有 关系 都是从一个节点到另一个节点创建的,带方向。默认情况下,所有查询都遵循关系中定义的方向。但是,在某些情况下,需要查询所有相关节点,而不管关系的方向如何。这可以通过参数 directed: false
实现。
例如,以下查询应返回所有 User 的朋友,而不管关系 "FRIENDS_WITH"
的方向如何
query {
users {
name
friends: friends(directed: false) {
name
}
}
}
此外,无向关系也可以以相同的方式与连接一起使用。例如,此查询正在请求用户列表及其朋友的姓名,以及无向的朋友关系连接
query Query {
users {
friendsConnection(directed: false) {
edges {
node {
name
}
}
}
}
}
请记住,**无向关系仅在查询中受支持**。关系的 类型定义 可能会定义不同的行为,因此在某些情况下 directed
选项可能不可用。