Queries
Type definitions
Quries on this page assume the following type definitions:
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
}
For which the following query fields are generated:
type Query {
posts(where: PostWhere, options: PostOptions): [Post!]!
postsAggregate(where: PostWhere): PostAggregationSelection!
users(where: UserWhere, options: UserOptions): [User!]!
usersAggregate(where: UserWhere): UserAggregationSelection!
}
Writing queries
Based on the type definitions, here are two examples of how to write queries for reading or retrieving values:
query {
users {
id
name
}
}
query {
users(where: { name: "Jane Smith" }) {
id
name
posts {
content
}
}
}
Undirected queries
All relationships are created with a direction from one node to another.
By default, all queries follow the direction defined in the relationship.
However, in some cases it is necessary to query for all related nodes, regardless of the direction of the relationship.
This can be achieved with the argument directed: false
.
For example, the following query should return all User friends, regardless of the direction of the relationship "FRIENDS_WITH"
:
query {
users {
name
friends: friends(directed: false) {
name
}
}
}
In addition, undirected relationships can also be used in the same fashion with connections. For instance, this query is asking for a list of users and their friends' names with an undirected friendship connection:
query Query {
users {
friendsConnection(directed: false) {
edges {
node {
name
}
}
}
}
}
Keep in mind that undirected relationships are only supported in queries.
The type definitions for a relationship may define a different behavior, so the directed
option may not be available in some cases.