Skip to content

Latest commit

 

History

History
174 lines (126 loc) · 3.58 KB

File metadata and controls

174 lines (126 loc) · 3.58 KB

import Tabs from '@theme/Tabs' import TabItem from '@theme/TabItem' import DocCardList from '@theme/DocCardList';

JavaScript Client

APIs to interact with ComposeDB from JavaScript, TypeScript, or React.

Prerequisites


Installation


Install the ComposeDB client package:

<Tabs defaultValue="pnpm" groupId="npm-or-pnpm-or-yarn" values={[ {label: 'npm', value: 'npm'}, {label: 'pnpm', value: 'pnpm'}, {label: 'yarn', value: '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:

<Tabs defaultValue="pnpm" groupId="npm-or-pnpm-or-yarn" values={[ {label: 'npm', value: 'npm'}, {label: 'pnpm', value: 'pnpm'}, {label: 'yarn', value: '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

:::tip 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:

<Tabs defaultValue="with-sessions" groupId="with-or-without-sessions" values={[ {label: 'With Sessions', value: 'with-sessions'}, {label: 'Without Sessions', value: '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: