Add and Remove Content

Collection: Add and Remove Content

User Intent

"I want to add or remove content from collections"

Operation

  • SDK Method: graphlit.addContentsToCollections() or graphlit.removeContentsFromCollection()

  • GraphQL: addContentsToCollections or removeContentsFromCollection mutation

  • Entity Type: Collection

  • Common Use Cases: Organize content, manage collection membership, dynamic categorization

TypeScript (Canonical)

import { Graphlit } from 'graphlit-client';
import {
  ContentTypes,
  EntityReferenceInput,
  FileTypes,
} from 'graphlit-client/dist/generated/graphql-types';

const graphlit = new Graphlit();

// Assumes you have a collection ID from creating a collection:
// const collection = await graphlit.createCollection({ name: "My Collection" });
// const collectionId = collection.createCollection.id;
const collection: EntityReferenceInput = { id: 'collection-id' };
const contents: EntityReferenceInput[] = [
  { id: 'content-id-1' },
  { id: 'content-id-2' },
  { id: 'content-id-3' },
];

// Add content to collection
await graphlit.addContentsToCollections(contents, [collection]);

console.log(`Added ${contents.length} items to collection`);

// Remove content from collection
await graphlit.removeContentsFromCollection([{ id: 'content-id-1' }], collection);

console.log('Removed 1 item from collection');

// Verify collection contents
const membership = await graphlit.queryContents({
  collections: [collection]
});

console.log(`Collection now has ${membership.contents.results.length} items`);

Add content to collection (snake_case)

await graphlit.client.add_contents_to_collections( contents=[{"id": content_id} for content_id in content_ids], collections=[{"id": collection_id}] )

print(f"Added {len(content_ids)} items to collection")

Remove content from collection

await graphlit.client.remove_contents_from_collection( contents=[{"id": "content-id-1"}], collection={"id": collection_id} )

print("Removed 1 item from collection")

Parameters

addContentsToCollections

  • id (string): Collection ID

  • contents (string[]): Array of content IDs to add

removeContentsFromCollection

  • collection (EntityReferenceInput): Target collection reference

  • contents (EntityReferenceInput[]): Content references to remove

Response

Developer Hints

Add vs Ingestion

Two ways to add content to collections:

Batch Operations

Move Content Between Collections

Content Can Be in Multiple Collections

Variations

1. Add Single Item

Add one content item:

2. Add Multiple Items

Batch add:

3. Remove Items

Remove from collection:

4. Add All Content from Query

Add filtered content:

5. Reorganize Collections

Move content based on criteria:

6. Clear Collection

Remove all content:

Common Issues

Issue: Collection not found error Solution: Verify collection ID is correct using queryCollections().

Issue: Content not found error Solution: Verify content IDs exist. Content may have been deleted.

Issue: Content appears in multiple collections Solution: This is by design. Content can belong to multiple collections. Remove from unwanted collections.

Issue: Changes not visible immediately Solution: Changes are immediate. Re-query to see updates.

Production Example

Dynamic content organization:

Tagging system with collections:

Last updated

Was this helpful?