Skip to main content
Version: 0.4.x

Queries

Access data stored on the network.

Prerequisites

  • Before your node can index your models, you need to deploy your composite.
  • Before your client can perform queries on those models, you need to compile your composite. ComposeClient 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 query
  • postIndex 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 in last: n to get the last n 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.

Model to model relations

For our models, let’s say we have a Post and we extend it by adding Comments.

# 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
}
}
}
}
}
}
}

Account-to-Model

# $id references the account DID string
query ($id: ID!) {
node(id: $id) {
...on CeramicAccount {
postConnection (first: 5) {
edges {
node {
id
title
text
}
}
}
}
}
}

Model-to-Account

# $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 value
  • isViewer: Boolean!: whether the account authenticated to the Ceramic instance matches the id
  • 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 any Node (account or document) by its id
  • 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.

  • 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