Query Collections
Collection: Query Collections
User Intent
"I want to list all my collections or find a specific collection by name"
Operation
SDK Method:
graphlit.queryCollections()orgraphlit.getCollection()GraphQL:
queryCollectionsorgetCollectionqueryEntity Type: Collection
Common Use Cases: List collections, find collection by name, check collection contents
TypeScript (Canonical)
import { Graphlit } from 'graphlit-client';
import { ContentTypes, EntityState } from 'graphlit-client/dist/generated/graphql-types';
const graphlit = new Graphlit();
// Query all collections
const collections = await graphlit.queryCollections();
console.log(`Total collections: ${collections.collections.results.length}`);
collections.collections.results.forEach(coll => {
console.log(`- ${coll.name} (${coll.state})`);
});
// Search by name
const searchResults = await graphlit.queryCollections({
search: 'Documentation'
});
console.log(`\nFound ${searchResults.collections.results.length} documentation collections`);
// Get specific collection
// Replace with ID from creating a collection or from queryCollections results above
const collectionId = 'collection-id-here';
const collection = await graphlit.getCollection(collectionId);
console.log(`\nCollection: ${collection.collection.name}`);
console.log(`Created: ${collection.collection.creationDate}`);
// Count content in collection
const contents = await graphlit.queryContents({
collections: [{ id: collectionId }]
});
console.log(`Content items: ${contents.contents.results.length}`);Query all collections (snake_case)
collections = await graphlit.queryCollections()
print(f"Total collections: {len(collections.collections.results)}")
for coll in collections.collections.results: print(f"- {coll.name} ({coll.state})")
Search by name
search_results = await graphlit.queryCollections( filter=input_types.CollectionFilter( search="Documentation" ) )
Get specific collection
collection = await graphlit.getCollection(collection_id) print(f"Collection: {collection.collection.name}")
**C#**:
```csharp
using Graphlit;
var client = new Graphlit();
// Query all collections (PascalCase)
var collections = await graphlit.QueryCollections();
Console.WriteLine($"Total collections: {collections.Collections.Results.Count}");
foreach (var coll in collections.Collections.Results)
{
Console.WriteLine($"- {coll.Name} ({coll.State})");
}
// Search by name
var searchResults = await graphlit.QueryCollections(new CollectionFilter {
Search = "Documentation"
});
// Get specific collection
var collection = await graphlit.GetCollection(collectionId);
Console.WriteLine($"Collection: {collection.Collection.Name}");Parameters
queryCollections (Optional Filter)
search(string): Search by collection namestates(EntityState[]): Filter by stateENABLED,DISABLED
getCollection (Required)
id(string): Collection ID
Response
{
collections: {
results: Collection[];
}
}
interface Collection {
id: string;
name: string;
state: EntityState;
creationDate: Date;
}Developer Hints
Collections Don't Store Content Count
Important: Collections don't have a built-in content count. Query contents to count.
// Get collection
const collection = await graphlit.getCollection(collectionId);
// Count items in collection
const contents = await graphlit.queryContents({
collections: [{ id: collectionId }]
});
console.log(`${collection.collection.name}: ${contents.contents.results.length} items`);Find Collection by Name
const results = await graphlit.queryCollections({
search: 'Product Docs'
});
if (results.collections.results.length > 0) {
const collection = results.collections.results[0];
console.log(`Found: ${collection.name} (${collection.id})`);
} else {
console.log('Collection not found');
}Collection Inventory
const collections = await graphlit.queryCollections();
console.log('=== COLLECTION INVENTORY ===\n');
for (const coll of collections.collections.results) {
const contents = await graphlit.queryContents({
collections: [{ id: coll.id }],
limit: 1 // Just get count
});
console.log(`${coll.name}:`);
console.log(` Items: ${contents.contents.results.length}`);
console.log(` State: ${coll.state}\n`);
}Variations
1. List All Collections
Get all collections:
const collections = await graphlit.queryCollections();
console.log(`You have ${collections.collections.results.length} collections`);2. Search by Name
Find specific collections:
const results = await graphlit.queryCollections({
search: 'Documentation'
});
console.log('Documentation collections:');
results.collections.results.forEach(coll => {
console.log(`- ${coll.name}`);
});3. Get Collection Details
Retrieve specific collection:
const collection = await graphlit.getCollection(collectionId);
console.log(`Collection: ${collection.collection.name}`);
console.log(`ID: ${collection.collection.id}`);
console.log(`Created: ${collection.collection.creationDate}`);
console.log(`State: ${collection.collection.state}`);4. Collection with Content Count
Show content counts:
const collections = await graphlit.queryCollections();
for (const coll of collections.collections.results) {
const contents = await graphlit.queryContents({
collections: [{ id: coll.id }]
});
console.log(`${coll.name}: ${contents.contents.results.length} items`);
}5. Filter Active Collections
Only enabled collections:
const active = await graphlit.queryCollections({
states: [EntityState.Enabled]
});
console.log(`Active collections: ${active.collections.results.length}`);Common Issues
Issue: Collection not found error
Solution: Verify collection ID is correct. Collection may have been deleted.
Issue: Search returns no results Solution: Search is case-sensitive. Try partial matches.
Issue: Can't see content count
Solution: Use queryContents() with collection filter to count items.
Production Example
Collection dashboard:
const collections = await graphlit.queryCollections();
console.log('=== COLLECTION DASHBOARD ===\n');
for (const coll of collections.collections.results) {
const contents = await graphlit.queryContents({
collections: [{ id: coll.id }]
});
const fileCount = contents.contents.results.filter(
c => c.type === ContentTypes.File
).length;
const pageCount = contents.contents.results.filter(
c => c.type === ContentTypes.Page
).length;
console.log(` ${coll.name}`);
console.log(` Total: ${contents.contents.results.length} items`);
console.log(` Files: ${fileCount}, Pages: ${pageCount}`);
console.log(` State: ${coll.state}\n`);
}Last updated
Was this helpful?