JavaScript Client
APIs to interact with ComposeDB from JavaScript, TypeScript, or React.
Prerequisites
Installation
Install the ComposeDB client package:
- npm
- pnpm
- yarn
npm install @composedb/client
pnpm add @composedb/client
yarn add @composedb/client
If you’re using TypeScript, you may also need to install ComposeDB Types:
- npm
- pnpm
- yarn
npm install -D @composedb/types
pnpm add -D @composedb/types
yarn add -D @composedb/types
Configuration
Create a client instance by passing your server URL and your compiled composite:
// Import ComposeDB client
import { ComposeClient }from '@composedb/client'
// Import your compiled composite
import { definition }from './__generated__/definition.js'
// Create an instance of ComposeClient
// Pass the URL of your Ceramic server
// Pass reference to your compiled composite
const compose = new ComposeClient({ ceramic: 'http://localhost:7007', definition })
More details: ComposeClient
Queries
Executing Queries
Execute GraphQL Queries using the schema that is auto-generated from your compiled composite:
// Get account of authenticated user
await compose.executeQuery(`
query {
viewer {
id
}
}
`)
More details: executeQuery
Mutations
Enabling Mutations
Before enabling mutations you must authenticate the user.
After you have an authenticated user, enable mutations by setting their authenticated account on the ComposeDB client:
- With Sessions
- Without Sessions
// Assign the authorized did from your session to your client
compose.setDID(session.did)
// Call setDID method on ComposeClient instance
// Using authenticated did instance
compose.setDID(did)
Executing mutations
In your client, you can execute GraphQL mutations using the schema that is auto-generated from your compiled composite. Follow examples in the mutations guide.
Next Steps
Learn how to Authenticate Users →
Related Guides
ComposeDB’s JavaScript client optionally works with popular GraphQL clients:
📄️ Using Apollo
Apollo is a popular GraphQL client for React and other platforms.
📄️ Using Relay
Relay is a popular GraphQL client for React.