Queries
Access data stored on the network.
Prerequisites
- Installed ComposeDB client
- A compiled composite
- A deployed composite
The ComposeDB Client automatically generates a GraphQL Schema from your compiled composite.
Basic queries
For our model, let’s say we have a Post
Data Model:
# Post model
type Post @createModel(accountRelation: LIST, description: "A simple text post") {
author: DID! @documentAccount
title: String! @string(minLength: 10, maxLength: 100)
text: String! @string(maxLength: 500)
}
Now, query for posts:
# Get first 2 posts
query{
postIndex(first:2){
edges{
node{
title
text
id
}
}
}
}
Where:
query
instructs GraphQL to perform a querypostIndex
is a built-in binding of your runtime composite. This binding enables us to look through the indexed data for a specific model and retrieve it.(first: 2)
grabs the first 2 items that are indexed. You can also pass inlast: n
to get the lastn
number of items.- The remainder of the query is standard GraphQL to retrieve the desired fields
Querying relations
In Relations you learned how to write models with relationships to other models or accounts. Here we demonstrate how to query those relations. Unlike basic queries, when querying relations allows you to specify fields in a related model to be returned as fields of the original model.
Querying model-to-model relations
For our models, let’s say we have a Post
model and we extend it by adding a Comments
model.
# Post model
type Post @createModel(accountRelation: LIST, description: "A simple text post") {
author: DID! @documentAccount
title: String! @string(minLength: 10, maxLength: 100)
text: String! @string(maxLength: 500)
}
# Extend post model with comment relations
type Comment @loadModel(id: "...") {
id
}
type Post @loadModel(id: "kjzl6hvfrbw6c99mdfpjx1z3fue7sesgua6gsl1vu97229lq56344zu9bawnf96") {
comments: [Comment] @relationFrom(model: "Comment", property: "postID")
}
Query for comments on posts:
# Get first 5 posts
# Get first 5 comments per post
query {
postIndex(first: 5) {
edges {
node {
title
text
commentsCount
commentsIndex(first: 5) {
edges {
node {
text
}
}
}
}
}
}
}
Querying account-to-model relations
# $id references the account DID string
query ($id: ID!) {
node(id: $id) {
...on CeramicAccount {
postConnection (first: 5) {
edges {
node {
id
title
text
}
}
}
}
}
}
Querying model-to-account relations
# $id references a Post document stream ID
query ($id: ID!) {
node(id: $id) {
...on Post {
author {
id
postConnection (first: 5) {
edges {
node {
id
title
text
}
}
}
}
}
}
}
Things to Know
ComposeDB client automatically generates a GraphQL Schema from a compiled composite. It includes two objects: a CeramicAccount
object and a root Query
object used as an entrypoint to access the graph.
CeramicAccount
Object
The CeramicAccount
object replaces DID
scalars in your composite with the following fields:
id: ID!
: the DID string valueisViewer: Boolean!
: whether the account authenticated to the Ceramic instance matches theid
- Other fields will be generated based on the models present in the definition
Query
Object
The Query
object provides entrypoints for accessing data in the graph, using the following fields:
node(id: ID!): Node
: loads anyNode
(account or document) by itsid
viewer: CeramicAccount
: the account attached to the Ceramic instance, if authenticated- Other fields will be generated based on the models present in the definition, providing entry-points by querying the index
Next Steps
Head to the next section, Mutations, to learn how to write or modify data on the network.
Related Guides
- For additional context on the types of account and model relations, see the Relations guide
- To learn how to compile and deploy a composite, see Composites